使用printf时, 记住使用哪个字符格式化输出很麻烦, 常用的int/long还好记, 遇到int_32/size_t这种就麻爪了. 每次都要网上查下,
C99标准的§7.19.6.1 (7)有定义, 但是每次翻pdf好麻烦, 而且显示的也不是那么易懂. 这里推荐cppreference, 使用图表格式, 十分易懂.
Conversion specifier | Explanation | Argument type | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
length modifier | hh (C99) | h | (none) | l | ll (C99) | j (C99) | z (C99) | t (C99) | L | |
% | writes literal % . The full conversion specification must be %% . | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A |
c | writes a single character. The argument is first converted to unsigned char. If the l modifier is used, the argument is first converted to a character string as if by %ls with a wchar_t[2]argument. | N/A | N/A | int | wint_t | N/A | N/A | N/A | N/A | N/A |
s | writes a character string The argument must be a pointer to the initial element of an array of characters. Precision specifies the maximum number of bytes to be written. If Precision is not specified, writes every byte up to and not including the first null terminator. If the l specifier is used, the argument must be a pointer to the initial element of an array of wchar_t, which is converted to char array as if by a call to wcrtomb with zero-initialized conversion state. | N/A | N/A | char* | wchar_t* | N/A | N/A | N/A | N/A | N/A |
d i | converts a signed integer into decimal representation [-]dddd. Precision specifies the minimum number of digits to appear. The default precision is 1. | signed char | short | int | long | long long | signed size_t | N/A | ||
o | converts a unsigned integer into octal representation oooo. Precision specifies the minimum number of digits to appear. The default precision is 1. If both the converted value and the precision are 0 the conversion results in no characters. In the alternative implementationprecision is increased if necessary, to write one leading zero. In that case if both the converted value and the precision are 0, single 0 is written. | unsigned char | unsigned short | unsigned int | unsigned long | unsigned long long | unsigned version of ptrdiff_t | N/A | ||
x X | converts an unsigned integer into hexadecimal representation hhhh. For the | N/A | ||||||||
u | converts an unsigned integer into decimal representation dddd. Precision specifies the minimum number of digits to appear. The default precision is 1. If both the converted value and the precision are 0 the conversion results in no characters. | N/A | ||||||||
f F | converts floating-point number to the decimal notation in the style [-]ddd.ddd. Precision specifies the minimum number of digits to appear after the decimal point character. The default precision is 6. In the alternative implementationdecimal point character is written even if no digits follow it. For infinity and not-a-number conversion style see notes. | N/A | N/A | double | double (C99) | N/A | N/A | N/A | N/A | long double |
e E | converts floating-point number to the decimal exponent notation. For the | N/A | N/A | N/A | N/A | N/A | N/A | |||
a A (C99) | converts floating-point number to the hexadecimal exponent notation. For the | N/A | N/A | N/A | N/A | N/A | N/A | |||
g G | converts floating-point number to decimal or decimal exponent notation depending on the value and the precision. For the
Unless alternative representation is requested the trailing zeros are removed, also the decimal point character is removed if no fractional part is left. For infinity and not-a-number conversion style see notes. | N/A | N/A | N/A | N/A | N/A | N/A | |||
n | returns the number of characters written so far by this call to the function. The result is written to the value pointed to by the argument. The specification may not contain any flag, field width, or precision. | signed char* | short* | int* | long* | long long* | signed size_t* | N/A | ||
p | writes an implementation defined character sequence defining a pointer. | N/A | N/A | void* | N/A | N/A | N/A | N/A | N/A | N/A |
https://en.cppreference.com/w/c/io/fprintf
这里提供两个C99标准文件地址供大家参考(正式官方标准不在官网提供, 需要购买. 你可以在"http://www.open-std.org/jtc1/sc22/wg14/ "找到准正式版本):
http://www.dii.uchile.cl/~daespino/files/Iso_C_1999_definition.pdf
http://www.open-std.org/jtc1/sc22/wg14/www/C99RationaleV5.10.pdf