C99/Cpp 使用printf 时format大全

使用printf时, 记住使用哪个字符格式化输出很麻烦, 常用的int/long还好记, 遇到int_32/size_t这种就麻爪了. 每次都要网上查下, 

C99标准的§7.19.6.1 (7)有定义, 但是每次翻pdf好麻烦, 而且显示的也不是那么易懂. 这里推荐cppreference, 使用图表格式, 十分易懂.

Conversion
specifier
ExplanationArgument type
length modifierhh

(C99)

h(none)lll

(C99)

j

(C99)

z

(C99)

t

(C99)

L
%writes literal %. The full conversion specification must be %%.N/AN/AN/AN/AN/AN/AN/AN/AN/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/AN/A

int

wint_t

N/AN/AN/AN/AN/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/AN/A

char*

wchar_t*

N/AN/AN/AN/AN/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.
If both the converted value and the precision are ​0​ the conversion results in no characters.

signed char

short

int

long

long long

intmax_t

signed size_t

ptrdiff_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

uintmax_t

size_t

unsigned version of ptrdiff_t

N/A
x
X

converts an unsigned integer into hexadecimal representation hhhh.

For the x conversion letters abcdef are used.
For the X conversion letters ABCDEF are used.
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 implementation 0x or 0Xis prefixed to results if the converted value is nonzero.

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/AN/A

double

double (C99)

N/AN/AN/AN/A

long double

e
E

converts floating-point number to the decimal exponent notation.

For the e conversion style [-]d.ddde±dd is used.
For the E conversion style [-]d.dddE±dd is used.
The exponent contains at least two digits, more digits are used only if necessary. If the value is ​0​, the exponent is also ​0​. 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/AN/AN/AN/AN/AN/A
a
A

(C99)

converts floating-point number to the hexadecimal exponent notation.

For the a conversion style [-]0xh.hhhp±d is used.
For the A conversion style [-]0Xh.hhhP±d is used.
The first hexadecimal digit is not 0 if the argument is a normalized floating point value. If the value is ​0​, the exponent is also ​0​. Precision specifies the minimum number of digits to appear after the decimal point character. The default precision is sufficient for exact representation of the value. In the alternative implementation decimal point character is written even if no digits follow it. For infinity and not-a-number conversion style see notes.

N/AN/AN/AN/AN/AN/A
g
G

converts floating-point number to decimal or decimal exponent notation depending on the value and the precision.

For the g conversion style conversion with style e or f will be performed.
For the G conversion style conversion with style E or F will be performed.
Let P equal the precision if nonzero, 6 if the precision is not specified, or 1 if the precision is ​0​. Then, if a conversion with style E would have an exponent of X:

  • if P > X ≥ −4, the conversion is with style f or F and precision P − 1 − X.
  • otherwise, the conversion is with style e or E and precision P − 1.

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/AN/AN/AN/AN/AN/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 flagfield width, or precision.

signed char*

short*

int*

long*

long long*

intmax_t*

signed size_t*

ptrdiff_t*

N/A
pwrites an implementation defined character sequence defining a pointer.N/AN/Avoid*N/AN/AN/AN/AN/AN/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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值