The printf function is called the format output function, and the last letter F of its keyword is the meaning of format. The function is to display the specified data to the display screen according to the user-specified format. We have used this function many times in the previous example.
1. Call format printf ("< format string >", < parameter table >);
Where the formatted string consists of two parts : some are normal characters , these characters will be output as-is, and the other part is the formatting of the specified character , starting with "%", followed by one or several specified characters , used to determine the output content format. The parameter table is a series of parameters that need to be output, the number must be as many as the number of output parameters as indicated by the formatted string , the parameters are separated by "," and the order one by one corresponds , otherwise unexpected errors will occur.
2. Formatting characters
%d decimal signed integer
%u decimal unsigned integer
%f floating Point
%s string
%c single character
%p the value of the pointer
Floating-point numbers in the form of%e indices
%x,%x unsigned integer in hexadecimal notation
%0 unsigned integer represented in octal
%g automatic selection of appropriate representations
Description:
(1). You canInserting a number between "%" and a letter indicates the maximum field width.For example:%3d indicates output3-bit integer numberEnough3-bit right-aligned.%9.2F indicates that the output field width is9 floating-point numbers, where the decimal digit is2, the integer digits are6, the decimal point occupies one, not enough 9-bit right-aligned.%8s indicates the outputA 8-character stringEnough8 characters aligns aligned.If the length of the string, or the number of integers exceeds the description of the field width, it will be output at its actual length.But for floating-point numbers, if the integer number of bits exceeds the width of the indicated integer digits, it will be output by the actual integer digits; If the fractional number of digits exceeds the width of the indicated decimal place, the output is rounded by the width of the description. Other than that, if you want to add a bit before the output value0, you should add one before the present width0.For example:%04D indicates that an output is less thanWhen a 4-bit value, the previous0 make the total width of the4 bits. If the output format of a character or integer is represented by a floating-point number, the number after the decimal point represents the maximum width , and the number before the decimal point represents the minimum width. For example ,%6.9s indicates that a string with a length of not less than 6 and not greater than 9 is displayed . If it is greater than 9, then the content after the first 9 characters will be deleted.
(2). You can add a lowercase letter L between "%" and the letter , indicating that the output is a long form. For example ,%ld indicates the output of a long integer, and%LF represents the output double floating-point number.
(3). You can control the output left or right alignment , that is, "%" and the letter by adding a "-" sign to indicate that the output is left-justified , otherwise it is right-justified. For example :%-7d represents the output 7-bit integer left-aligned,%-10s represents the output of 10 characters aligns aligned.
General form of printf function call
The printf function is a standard library function whose function prototype is in the header file "Stdio.h". However, as a special case, it is not required to include the Stdio.h file before using the printf function. The general form of the printf function call is:
printf ("Format control string", Output table column), the function return value is integral type. If successful, returns the number of characters in the output, and returns a negative output error.
Where the format control string is used to specify the output format. The format control string can consist of both a format string and a non-formatted string. A format string is a string that begins with a% followed by a variety of format characters to indicate the type, form, length, scale, etc. of the output data. Such as:
- "%d" means output by decimal integer;
- "%ld" indicates the output by decimal length integer;
- "%c" means output by character type and so on.
The non-formatted string is output as a hint 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 one by one on both the quantity and the type.
Example of "Example 4-3" printf function
- #include <stdio.h>
- int main(void){
- int a=N, b=the;
- 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);
- return 0;
- }
In this example, the values of a and B are output four times, but the output is not the same due to the different format control strings. The output statement in line 3rd controls the string with a space (not a format character) between the two format string%d, so there is a space between the A and B values of the output. The printf statement format in line 4th controls the non-formatted character comma added to the string, so a comma is added between the values A and B of the output. The format string for line 5th requires a, B value to be output by character type. The non-format string is added in line 6th in order to prompt the output.
format string
The general form of the format string in Turbo C is:
Flag [Output min. width] [. Accuracy] [Length] type.
The items in square brackets [] are optional.
The meanings of the items are described below.
1) Type
The type character is used to represent the type of the output data, and its format character and meaning are shown in the following table:
format Characters |
meaning |
D |
Output signed integers in decimal form (positive numbers do not output symbols) |
O |
Output unsigned integers in eight binary form (not output prefix 0) |
X,x |
Output unsigned integers in 16 binary form (does not output prefix ox) |
U |
Output unsigned integers in decimal form |
F |
Output single, double-precision real numbers in decimal form |
E,e |
Output single, double-precision real numbers in exponential form |
G,g |
Output single, double-precision real numbers in a short output width of%f or%e |
C |
Output a single character |
S |
Output string |
2) logo
The flag characters are--, +, #, and four of the spaces, and their meanings are shown in the following table:
logo |
meaning |
- |
The result is left-aligned and the right-fill grid |
+ |
Output symbol (plus or minus) |
Space |
The output value is a positive-time space, minus the negative. |
# |
No influence on C, S, D, U class; For the O class, the output is prefixed with o; For the X class, the output is prefixed with 0x; For E, G, F class, the decimal point is given when the result has a decimal number. |
3) Output Minimum width
A decimal integer that represents the minimum number of digits for the output. If the actual number of digits is greater than the defined width, the actual number of digits is output, or if the actual number of digits is less than the defined width, a space or 0 is provided.
4) Accuracy
Precision format characters with "." Begins with a decimal integer followed by. The meaning of this item is: If the output number, then the number of decimal places, if the output is a character, the number of output characters, if the actual number of digits is greater than the defined number of precision, the section is truncated.
5) Length
The length format character is H, l Two, h means output by a short integer, l means output by a long integer quantity.
"Example 4-4"
- #include <stdio.h>
- int main(void){
- int a=;
- long float b=123.1234567;
- //original B is defined as float b=123.1234567;
- //By Reader @ Deng feedback, I found overflow, in front of a long
- 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", a, a, a, a); //% can be output%
- printf("a=%f\ n", b);
- printf("B (%%f) =%f, B (%%LF) =%LF, B (%%5.4LF) =%5.4LF, B (%%e) =%e\ n", b, b , B, b );
- printf("c=%f\ n", C);
- printf("C (%%LF) =%LF, C (%%f) =%f, C (%%8.4LF) =%8.4lf\ n", C, C, C);
- printf("d=%c\ n", D);
- printf("D (%%c) =%c, D (%%8c) =%8c\ n", D, D);
- return 0;
- }
Operation Result:
In this example:
- The 11th line outputs the value of the integer variable A in four formats, where "%5d" requires an output width of 5, and a value of 15 has two digits so it is three spaces.
- Line 14th outputs the value of the real amount B in four formats. The output of the "%f" and "%LF" formats is the same, stating that the "L" character has no effect on the "F" type. "%5.4LF" Specifies the output width of 5, the precision is 4, because the actual length of more than 5 should be based on the actual number of outputs, the number of decimal digits more than 4 parts are truncated.
- The 17th line outputs a double-precision real number, and "%8.4LF" is truncated to more than 4 bits because the specified precision is 4 bits.
- The 20th line outputs the character D, where "%8c" specifies an output width of 8 so that 7 spaces are added before the output character p.
One more thing to note when using the printf function is the order of evaluation in the output table column. Different compilation systems are not necessarily the same, either from left to right or from right to left. Turbo c is done from right to left. Take a look at the following two examples.
"Example 4-5" in a printf () output
- #include <stdio.h>
- int main(void){
- int i=8;
- printf("The raw value:i=%d\ n", I);
- printf("++i=%d \++i=%d \--i=%d \--i=%d\ n", ++i, ++i, I.,--i);
- return 0;
- }
Operation Result:
The Raw value:i=8
++i=8
++i=7
--i=6
--i=7
"Example 4-6" is output in multiple printf ()
- #include <stdio.h>
- int main(void){
- int i=8;
- printf("The raw value:i=%d\ n", I);
- printf("++i=%d\ n", ++i);
- printf("++i=%d\ n", ++i);
- printf("--i=%d\ n", i.);
- printf("--i=%d\ n", i.);
- return 0;
- }
Operation Result:
The Raw value:i=8
++i=9
++i=10
--i=9
--i=8
The difference between the two programs is output with a printf statement and multiple printf statements. But from the results can be seen is different. Why would the results be different? This is because the printf function evaluates the values in the output table in the order from right to left.
It is important to note, though, that the order of evaluation is from right to left, but the output order is still left to right, so the result is the above output.
Note: After the reader @ Dragon Totem Hao Little feedback, we found that the original example in the VC6.0 under the complexity of the calculation process, can not be intuitive to explain the order of evaluation from right to left. The above code is modified, and the date of modification is 2014-07-05.
Here is the old code and analysis:
"Example 4-5"
Main () {
int i=8;
printf ("%d\n%d\n%d\n%d\n%d\n%d\n", ++i,--i,i++,i--,-i++,-i--);
}
"Example 4-6"
Main () {
int i=8;
printf ("%d\n", ++i);
printf ("%d\n",--i);
printf ("%d\n", i++);
printf ("%d\n", i--);
printf ("%d\n",-i++);
printf ("%d\n",-i--);
}
The difference between the two programs is output with a printf statement and multiple printf statements. But from the results can be seen is different. Why would the results be different? This is because the printf function evaluates the values in the output table in the order from right to left. In the first example, the last item "-i--" is evaluated first, the result is-8, then I is 7 after 1. Again the "-i++" item is worth 7, then I 1 after the increment is 8. Then the "i--" item is worth 8, then I again 1 after the minus 7. Then the "i++" item was 7, then I was increased by 1 after 8. Then the "-I" Item, I first self-minus 1 after the output, the output value is 7. Finally, the first item in the Output table column is "++i", when I increment 1, I output 8.
C language Format output function printf () detailed