Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - C language problem, about the output of printf statement
C language problem, about the output of printf statement
The printf function is a standard library function, and its function prototype is in the header file "stdio.h". However, as a special case, you don't need to include the stdio.h file before using the printf function. The general form of printf function call is:

Printf ("format control string", output table column), where the format control string is used to specify the output format. Format control strings can be composed of format strings and unformatted strings. A format string is a string that begins with%, followed by various format characters to explain the type, form, length, decimal places, etc. Output data. For example:

"%d" means output in decimal integer;

"%ld" means output in decimal long integer;

"%c" means character-based output, etc.

Unformatted strings are output as they are, which plays a prompt role in the display. Each output item is given in the output table column, which requires that the format string and each output item should correspond to each other in number and type.

Please look at the following example:

# include & ltstdio.h & gt

int main(void)

{

int a=88,b = 89

printf("%d %d\n ",a,b);

printf("%d,%d\n ",a,b);

printf("%c,%c\n ",a,b);

printf("a=%d,b=%d ",a,b);

Returns 0;

}

In this example, the values of a and b are output four times, but the output results are different due to different format control strings. In the output statement format control string in line 3, a space (unformatted character) is added between the two format strings %d, so there is a space between the output A and B values. The unformatted character comma is added to the format control string of the printf statement on line 4, so a comma is added between the output A and B values. The format string in line 5 requires that the values of a and b be output according to the character type. In line 6, an unformatted string is added to prompt the output result.

The general form of format string in Turbo C is [logo] [minimum output width] [. Accuracy] [Length] type.

Items in square brackets [] are optional.

1) Type: Type characters are used to indicate the type of output data, and their format symbols and meanings are shown in the following table:

Format character meaning

d? Output signed integers in decimal form (positive numbers do not output symbols)

o? Output unsigned integers in octal form (prefix 0 is not output)

X, x outputs unsigned integers in hexadecimal form (prefix Ox is not output).

u? Output unsigned integers in decimal form.

f? Output single-precision and double-precision real numbers in decimal form.

E,E? Output single-precision and double-precision real numbers in exponential form.

G,G? Output single-precision and double-precision real numbers, and use %f or% e to indicate the shorter output width.

Output a single character

Output string of

2) Logo: Logo characters include-,+,# and spaces, and their meanings are shown in the following table:

Implied meaning

-The result is aligned to the left and filled with spaces to the right.

+ ? Output symbol (plus or minus sign)

When the spatial output value is positive, it is preceded by a space, and when it is negative, it is preceded by a negative sign.

(It has no influence on Class C, Class S, Class D and Class U;

? For class o, prefix o when outputting;

# For class X, the output prefix is 0x;

? For classes E, G and F, decimal points are given only when the results have decimal points. )

3) Minimum output width: Decimal integer is used to represent the minimum number of output digits. If the actual number of digits is greater than the defined width, output according to the actual number of digits; If the actual number of digits is less than the defined width, it will be supplemented with spaces or zeros.

4) Precision: precision format characters begin with "."followed by a decimal integer. The meaning of this item is: if the number is output, it means the number of decimal places; If the output is a character, it indicates the number of characters output; If the actual number of digits is greater than the defined precision, the excess will be truncated.

5) The format symbols of 5)Length:Length are H and L, where H represents short integer output and L represents long integer output.

Please look at the following example:

# include & ltstdio.h & gt

int main(void){

int a = 15;

Long floating point b =123.1234567;

Double c =12345678.1234567;

char d = ' p

printf("a=%d\n ",a);

printf("a(%%d)=%d,a(%%5d)=%5d,a(%%o)=%o,a(%%x)=%x\n\n ",a,a,a,a,a); ? //%%can output%

printf("a=%f\n ",b);

printf("b(%%f)=%f,b(%%lf)=%lf,b(%%5.4lf)=%5.4lf,b(%%e)=%e\n\n ",b,b,b,b,b;

printf("c=%f\n ",c);

printf("c(%%lf)=%lf,c(%%f)=%f,c(%%8.4lf)=%8.4lf\n\n ",c,c,c);

printf("d=%c\n ",d);

printf("d(%%c)=%c,d(%%8c)=%8c\n ",d,d);

Returns 0;

}

The running results are as follows:

In this example, line 1 1 outputs the value of the integer variable A in four formats, where "%5d" requires an output width of 5, and the value of A is 15, with only two digits, so three spaces are filled.

Line 14 outputs the value of the actual quantity b in four formats. The output of "%f" and "%lf" formats is the same, which shows that the "l" symbol has no influence on the "f" type. "%5.4lf" specifies an output width of 5 and an accuracy of 4. Because the actual length exceeds 5, it should be output according to the actual number of digits, and the part with decimal digits exceeding 4 is truncated.

Line 17 outputs a double-precision real number. The specified precision of "%8.4lf" is 4 bits, so the part exceeding 4 bits will be truncated.

Line 20 outputs the number of characters d, where "%8c" specifies that the output width is 8, so 7 spaces are added before the output character p. ..