PrintfFormat String :% [Mark] [[*] [Data width] [.Precision]Type
+ |
- |
0 |
# |
Minimum Width |
Precision |
Type |
display symbol |
left alignment ( right-aligned by default ) |
data field 0 (right aligned) |
8 hexadecimal addition 0 16 base Addition 0x |
10-digit integer or * NO. |
example: . 2 |
|
- |
Y |
- |
- |
Y |
- |
C |
- |
Y |
- |
- |
Y |
- |
S |
Y |
Y |
Y |
- |
Y |
- |
d , I |
- |
Y |
Y |
- |
Y |
- |
U |
- |
Y |
Y |
Y |
Y |
- |
O |
- |
Y |
Y |
Y |
Y |
- |
x |
Y |
Y |
Y |
- |
Y |
Y |
F,E,G,A |
-This option is not required for this type.
As shown in the preceding table, only the data width and alignment are supported by all types. The right alignment is performed by default when the width is specified.
Only when the minimum width is specified can we use the-, 0 Sign. In this case, alignment and fill make sense. In fact, we only need to specify the minimum width, if the width of the data is insufficient, the data will be filled with blank characters or filled in the front of the data or in the back of the data (depending on the alignment) 0 indicates filling in front of the data with 0. This is to make the Data Alignment look nice. It must be noted that although many types support left alignment and fill with 0, but in fact, these two options are conflicted. The left alignment cannot be filled with 0, and the left alignment cannot be filled with 0. The left alignment means filling with blank spaces after the data.
Examples
"% 12D" The data width must be at least 12 characters wide. If the data is less than 12 characters long, the data is right aligned.
"% +-12D" 12D indicates the same as above. The difference is that the print symbol and data are left aligned.
"% # X" Data 3A is printed as 0x3a
"%. 4f" Data retains the precision of four digits after the decimal point
In fact, the format string of printf is the same as that of sprintf.
Char buffer [100];
Sprintf (buffer, "% 10d", 12 );
Printf ("the string is % d bytes \ n", strlen (buffer ));
Output the string is 10 bytes