发布网友 发布时间:2024-10-23 23:35
共2个回答
热心网友 时间:2024-11-07 09:30
The general format of the conversion specifications used in the printf functions is as follows:
%[flags][field width][.precision]specifier
The flags consist of one or more of the characters +, ' ' (space), - , 0 , or #. Their meanings are:
+
The plus sign is prefixed to positive numbers.
' ' (space)
A leading space is prefixed to positive numbers.
-
The output is left-justified in the field.
0
The field is filled with leading zeroes to the left of the number.
#
Alternate conversion rules are used as follows: If specifier is A(*), a(*), E, e, G, or g, floating-point numbers are formatted with a decimal point. If specifier is X, x, or o, hexadecimal integers are formatted with the 0X or 0x prefix, and octal integers with the 0 prefix.
The field width is a positive integer that fixes the length of the field occupied by the given conversion specification in the output string. If the flags include a minus sign, the converted value appears left-justified in the field; otherwise, it is right-justified. The excess field length is filled with space characters. If the output string is longer than the field width, the field width is increased as necessary to print the string in its entirety.
An asterisk (*) may also be specified for the field width. In this case, the field width is determined by an additional argument of type int, which immediately precedes the argument to be converted in the argument list.
.precision determines the number of decimal places printed in the output of floating-point numbers, when specifier is f or e. If specifier is g, .precision determines the number of significant digits. Rounding is performed if necessary. For floating-point numbers, the default value for .precision is 6.
For integers, .precision indicates the minimum number of digits to be printed. Leading zeroes are prefixed as necessary. For integers, the default value for .precision is 1.
If the argument to be converted is a string, then .precision indicates the maximum number of characters of the string that should appear.
specifier is the conversion specifier, indicating how the given argument is to be interpreted and converted. Note that specifier must correspond to the actual type of the argument to be converted. The possible conversion specifiers are listed in Table 1-24.
Table 1-24. Conversion specifiers for formatted output
Specifier
Argument types
Output format
d, i
int
Decimal
u
unsigned int
Decimal
o
unsigned int
Octal
x
unsigned int
Hexadecimal with a, b, c, d, e, f
X
unsigned int
Hexadecimal with A, B, C, D, E, F
f
float/double
Floating-point number, decimal
e, E
float/double
Exponential notation, decimal
a, A
float/double
Exponential notation, hexadecimal(*)
g, G
float/double
Floating-point or exponential notation, whichever is shorter
c
char / int
Single character
s
string
The string terminated by '\0' or truncated to the number of characters specified by .precision.
N
int *
The number of characters printed up to this point is stored in the given location
P
pointer
The corresponding address, hexadecimal
%
none
The character %
The letter l (that's an ell) can be prefixed to the c or s conversion specifiers to indicate a wide character or a wide string.
The letters l or ll(*) can also be prefixed to the conversion specifiers d , i , u , o , x , and X to indicate an argument of type long or long long(*). Similarly, h or hh can be prefixed to the same conversion specifiers to indicate an argument of type short or char.
An argument of type long double can be converted by using the prefix L with the conversion specifier f , e , E , g , G , a, or A.
Furthermore, ANSI C99 has introduced the following extensions:
· The new conversion specifiers A and a can be used to print a number of type double in hexadecimal exponential notation (0Xh.hhhhP±d or 0xh.hhhhp±d). This conversion uses FLOAT_RADIX, which is generally defined as 2, as the base. If no precision is specified, the number is printed with as many decimal places as necessary for exact representation.
· Arguments of type intmax_t(*) or uintmax_t(*) can be converted by prefixing the letter j to the conversion specifiers d, i, o, u, x, or X. Similarly, the argument type size_t is indicated by the prefix z, and the type ptrdiff_t by the prefix t.
· For the integer types defined in the header file stdint.h(*) (such as int16_t and int_least32_t), there are separate conversion specifiers for use in printf() format strings. These conversion specifiers are defined as macros in the header file inttypes.h(*). The macro names for the conversion specifiers corresponding to d, i, o, x, and X begin with the prefixes PRId, PRIi, PRIo, PRIu, PRIx, and PRIX. For example, the macro names beginning with PRId are:
PRIdN PRIdLEASTN PRIdFASTN PRIdMAX PRIdPTR
where N is the width in bits (usually 8, 16, 32, or ). For example:
intmax_t i = INTMAX_MAX;
printf("Largest integer value: %20" PRIdMAX "\n",i );
热心网友 时间:2024-11-07 09:30
输出数据类型整数:d,占2个字符宽