C Language notes-printf () function, C Language notes printf
First look at the example:
printf("This line has only controlling strings.\n");
'Printf ("I am % d years old and % f inch tall", 21,132 );
Parameters
The parameters of printf () are divided into two parts: Control string and parameter list, which are separated by commas (,) and must be separated by commas. The control string is a String constant. If other parameters are not required, the text in the double quotation marks will be printed to the screen in the original text. In general, what is in double quotes and what is output, including escape characters.
However, in this way, no matter what we want to print, we can only manually write it inside the double quotation marks, and thus cannot implement automatic printing. For example, how does one output the value of a variable in a program? We can't change the value of a variable once. Can we manually rewrite the printf () function? That violates the original intention of writing a program.
Therefore, there must be a way for the function to print the value of the variable. However, writing the variable name directly into the double quotation marks is not feasible, because the variable name becomes a part of the String constant --Characters in double quotation marks are considered as plain text.
Therefore, a special character '%' is introduced into the control string. This character is combined with some of the following characters and will be escaped, so it will not be considered as common text, but for other purposes. For example, % d in the preceding example indicates that the position is printed with a decimal number instead of % d. The actual value is provided by the variable or constant corresponding to the string. % Is called a placeholder, indicating that it occupies a fixed position first and then adds content to it. d is a format specifier, indicating in which format the corresponding parameters are printed. % D is a format placeholder or conversion specifier.
It can be seen that the parameter can be a variable, a constant, or an expression. However, note that,Parameters must match the number of conversion specifiers in the control string in the same order. In addition, the type of each parameter must match the corresponding conversion specifiers. For example, % d should not be used to print the parameters.The following describes how to use various conversion specifiers.
Conversion specifier
What does the above-mentioned format conversion mean? We need to know that any data is stored in binary form in the computer. The bucket corresponding to a variable or constant may be a string of binary numbers: 01000001, take it as a hexadecimal number or print it out in the hexadecimal format of 0x41; print it out in decimal format of 65; the uppercase letter 'A' is printed as an ASCII character '.* Therefore, the essence of format conversion is to translate binary numbers into corresponding forms. The format specifiers specify the translation rules. *
The following describes the format specifiers one by one.
Integer type
% D or % I: signed decimal integers, which have the same effect, as shown below:
printf("this is %d and %i, same.\n", 10, 10);
Output: this is 10 and 10, same.
% U: Unsigned decimal INTEGER:
printf("this is %d and %u.\n", -65, -65);
Output: this is-65 and 4294967231.
We can see that the number-65 is stored in the same binary format, but there is a variation in the interpretation. For the principle, see the previous article.
% X or % X: hexadecimal unsigned integer:
printf("this is %d and %x.\n", -65, -65);
printf("this is %d and %X.\n", -65, -65);
Output: this is-65 and ffffffbf.
This is-65 and ffffbf.
% O: octal unsigned integer:
printf("this is %d and %o.\n", -65, -65);
Floating Point Type
- % F: Floating Point Number of the decimal Notation:
printf("this is %f\n", 12.62543219);
Output: this is 12.625432
It can be seen that % f round the number after the decimal point, that is, the precision is 6.
% E or % E: e or E-note floating point:
printf("this is %e\n", 0.625);
printf("this is %e\n", 9000876.54);
Output: this is 6.2520.e-01
This is 9.000877e + 06
It can be seen that % e or % E can only retain the six digits after the decimal point.
% G or % G: At this time, printf expresses a floating point number as an e-note, and only keeps six valid digits. Then, observe the exponent. If the exponent is between-4 and ~ + % F is automatically selected in the range of 5, and % e or % E is selected in reverse mode (it seems complicated, so let's determine it by yourself ):
printf("this is %g and %g\n", 0.000089, 0.00089);
printf("this is %G and %G\n", 900876.44, 9000876.44);
Output: this is 8.9e-05 and 0.00089
This is 900876 and 9.00088E + 06
- % A or % A: hexadecimal floating point number of p or P notation (I don't know what this is ):
printf("this is %a and %A\n", 3.75, -3.75);
Output: this is 0x1. ep + 1 and-0X1. EP + 1
Other Types
- % C: one character:
printf("this is %c \n", 66);
Output: this is B
- % S: String:
printf("this is %s \n", "string");
Output: this is string
- % P: pointer:
printf("this is %p \n", "string");
Output: this is 0x8048576
- %: Percent itself, because a single % has been "requisitioned" by the compiler and converted to a placeholder, so to print % itself, we have to replace it with two %:
printf("this is %% \n");
Output: this is %
Conversion Modifier
You can insert various modifiers between % and format specifiers to further modify the final print result, such as the print width, precision, and symbol.
'Number': used to specify the minimum width of a printed field. If the width cannot accommodate the result, a wider field is used when the limit is exceeded:
printf("this is %6d \n", 65);
Output: this is 65
'. Digit ': Specifies the precision of the floating point, that is, the number of digits printed on the right of the decimal point. % s, specify the maximum number of characters to print. For integer format, specify the minimum number of digits to print (integer precision ?), If it is not enough, use 0 for leading filling. If there is no number after the decimal point, the default value is 0, which is an integer:
printf("this is /%.2f/\n", 65.3);
printf("this is /%.f/\n", 65.3);
printf("this is /%.3d/\n", 65);
Output: this is/65.30/
This is/65/
This is/065/
'-': Minus sign, which is used to specify the alignment mode. From the preceding result, we can see that the default alignment mode is right alignment. If '-' is used, the alignment is left:
printf("this is /%6d/\n", 65);
printf("this is /%-6d/\n", 65);
Output: this is/65/
This is/65/
'+': Print the digit, whether it is a negative number. Generally, it is used only with signed format specifiers, such as % + d, % + f; but % + u, % + c, and so on, an error is reported:
printf("this is %+c \n", 65);
Output: this is + 65
'': This is a space, and its function is similar to '+'. The difference is that the printed '+' is replaced by a space. If it is used together with '+, because of its low priority, the effect will be overwritten by '+:
printf("this is /% -6d/\n", 65);
Output: this is/65/
There is a space in front of 65.
'0': For All numeric formats, leading 0 instead of spaces are used to fill the white space. If the integer precision or '-' is specified, the flag is invalid:
printf("this is /%05d/\n", 65);
'#': It is mainly used to print the leading symbol of octal or hexadecimal notation, and occasionally works with floating point type:
printf("this is /%#5x/\n", 65);
printf("this is /%#5o/\n", 65);
Output: this is/0x41/
This is/0101/
Modifiers used for conversion of different numeric types. They are used together with the numeric type specifiers:
H: indicates a short or unsigned short, such as % hd.
Hh: indicates a signed char or unsigned char, such as % hhu % hhx
L: indicates a long or unsigned long, such as % lo, % ld
Ll: long or unsigned long, % llx
L: long double
T: it can only be used with the integer type to indicate the type corresponding to the difference between pointers. Note that it is not the pointer type itself, % td
Z: only the integer type can be used to represent a size_t value, that is, the type of the sizeof returned value. % zd