In Turbo C, the format string is generally [flag] [minimum output width] [. Precision] [length] type. The items in square brackets [] are optional. The meanings of each item are described as follows:
1. Type type characters are used to indicate the type of output data. The format characters and meanings are shown in the following table:
Meaning of Characters in the output format
D. Output signed integers in decimal form (positive numbers do not output symbols)
O outputs unsigned integers in octal form (no prefix O is output)
X outputs an unsigned integer in hexadecimal format (the OX prefix is not output)
U outputs an unsigned integer in decimal format
F outputs Single and Double Precision Real Numbers in decimal form
E outputs Single and Double Precision Real Numbers in exponential form
G outputs Single and Double precision real numbers with a shorter output width in % f % e
C Outputs a single character
S output string
2. Flag
The following table lists the four types of Logo characters:-, +, #, and space:
Logo format character logo meaning
-The result is left aligned with a space on the right.
+ Space of the output symbol (positive or negative). The output value is a positive value with a space and a negative value with a negative value.
# This parameter has no effect on Class c, s, d, and u. For Class o, add the prefix to the output.
Class x with an o prefix of 0x in the output; Class e, g, and f with a decimal point in the result
3. Minimum output width
The minimum number of digits of the output is expressed by a decimal integer. If the actual number of digits is greater than the defined width, the actual number of digits is output. If the actual number of digits is less than the defined width, spaces or 0 are supplemented.
4. Precision
The precision format character starts with "." and is followed by a decimal integer. The meaning of this item is: if a number is output, it indicates the number of digits in decimal places. If the output is a character, it indicates the number of output characters. If the actual number of digits is greater than the defined precision, the part that exceeds the limit is truncated.
5. Length
The length format is h and l. h indicates output by short integer, and l indicates output by long integer.
Void main (){
Int a = 15;
Float B = 138.3576278;
Double c = 35648256.3645687;
Char d = 'P ';
Printf ("a = % d, % 5d, % o, % x \ n", );
Printf ("B = % f, % lf, % 5.4lf, % e \ n", B );
Printf ("c = % lf, % f, % 8.4lf \ n", c );
Printf ("d = % c, % 8c \ n", d, d );
} A <-- 15
B <-- 138.3576278.
C <-- 35648256.3645687.
D <-- 'P' main ()
{
Int a = 29;
Float B = 1243.2341;
Double c = 24212345.24232;
Char c = 'H'
Printf ("a = % d, % 5d, % o, % x \ n", );
Printf ("B = % f, % lf, % 5.4lf, % e \ n", B );
Printf ("c = % lf, % f, % 8.4lf \ n", c );
Printf ("d = % c, % 8c \ n", d, d );
}
In this example, the value of the integer variable a is output in four formats in Row 7. "% 5d" requires that the output width be 5, and the value of a is 15 and there are only two spaces. The eighth line outputs the value of the actual volume B in four formats. The output format of "% f" and "% lf" is the same, indicating that "l" has no effect on the "f" type. "% 5.4lf" specifies that the output width is 5 and the precision is 4. Because the actual length exceeds 5, the output should be based on the actual number of digits. The four-digit decimal places are truncated. The ninth row outputs double-precision real numbers. "% 8.4lf" truncates more than four digits because the specified precision is 4 bits. The output character count is d in row 10, where "% bc" specifies that the output width is 8. Therefore, add seven spaces before the output character p.
When using the printf function, pay attention to the order of values in the output table column. Different compilation systems may not be the same, either from left to right or from right to left. Turbo C runs from right to left. Rewrite example 2.13 in the following format:
Void main (){
Int I = 8;
Printf ("% d \ n", ++ I, -- I, I --, I ++,-I --);
} I <-- 8
Compared with example 2.13, this program only changes multiple printf statements to one printf statement output. However, the results are different. Why are the results different? This is because the order in which the printf function calculates the values in the output table is from right to left. In the formula, first evaluate the last "-I --", the result is-8, and then I minus 1 and then 7. Calculate the value-7 for the "-I ++" item, and then the value after I increases by 1 is 8. Calculate "I --" and then I minus 1 and then 7. Then calculate 7 for "I ++", and then 8 for "I plus 1. Calculate the "-- I" item, I first subtract 1 and then output, the output value is 7. Finally, the first item "++ I" in the output table column is obtained. At this time, I increases by 1 and then outputs 8. However, you must note that although the order of value is from right to left, the order of output is from left to right. Therefore, the result is the above output result.
From the meiyuli Column