printf 家族

来源:互联网 发布:阿里云香港速度 编辑:程序博客网 时间:2024/04/29 05:37

Table Of Contents

  • printf 家族
    • 函数成员
    • 格式代码
      • 转换类型 type
      • 格式标志[flags]
      • 字段宽度[width]
      • 字段精度[.precision]
      • 修改符[{hh | h | l | ll | j | z | t | L}]
        • C99
        • MS VC++

Previous topic

浮点型 float double

Next topic

scanf 家族

printf 家族

printf函数家族用于创建格式化的输出。标准: [1] 7.19.6.1

Tip

printf函数家族的格式代码和scanf函数家族的格式代码用法不同。所以必须小心谨慎,防止误用。 两者的格式代码中的有些可选字段看上去是相同的,这使得问题变得更为困难。 《C和指针》

函数成员

printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf,

wprintf, fwprintf, swprintf, vwprintf, vfwprintf, vswprintf,

格式代码

%[flags] [width] [.precision] [{hh | h | l | ll | j | z | t | L}]type

格式代码由一个百分号开头,后面跟 [2]

  1. 零个或者多个 标志字符[flags],用于修改有些转换的执行方式 [3]
  2. 一个可选的 最小字段宽度[width] [5]
  3. 一个可选的 精度[.precision] [4]
  4. 一个可选的 修改符[{hh | h | l | ll | j | z | t | L}] [6]
  5. 转换类型 type [7]

转换类型 type

CharacterTypeOutput formatc,Cint用于printf时,为单字节;用于wprintf时,为宽字节.d,iint有符号十进制数.u,o,x,Xunsigned无符号数值. 十进制:u; 八进制:o; 十六进制:x,X, 两者区别, x是abcdef, X使用ABCDEF.e,Edouble指数形式输出.如:6.023000e23是使用e;6.023000E23是使用E. 小数点后面的位数由精度字段决定,默认为6f,Fdouble常规浮点数格式输出.如:[–]dddd.dddd. 小数点后面的位数由精度字段决定,默认为6g,Gdouble自动选择使用%f,%e,%E. 如果指数大于等于-4但小于精度字段使用%f,否则使用%e(g->e, G->E)a,Adouble有符号的十六进制双精度浮点值[?]0xh.hhhh p±dd, 其中h.hhhh为十六进制的尾数, dd为指数schar*用于printf时,为单字节字符串;用于wprintf时,为宽字节字符串. 长度为字符串长度或者精度.(wcrtomb转码)Schar*用于printf时,为宽字节字符串;用于wprintf时,为单字节字符串. 长度为字符串长度或者精度.(VS)Pvoid*指针值转换为因编译器而异的可打印字符.(VS:十六进制数字)nint*无输出,返回到目前为止函数所产生的的输出字符数保存在该指针所指向的内存中(VS中默认禁用,使用_set_printf_count_output(1)开启)% 输出一个%字符
hs 在用于wprintf时,存在字符集问题。

VS需要设置 setlocale(LC_ALL, “”);

GCC需要设置编译参数 gcc Test.cpp -finput-charset=gbk

setlocale(LC_ALL, "");wprintf(L"%hs \n", "abc");wprintf(L"%hs \n", "陈");

格式标志[flags]

FlagMeaningDefault-值在字段中左对齐右对齐+对于格式化有符号的值时,对于非负值,强制加正号非负值,不显示正号0当数值为右对齐时,用0填充未使用的列用空格填充空格当有符号的数值为非负时,在其开始位置添加一个空格不添加空格#选择某些类型的另一种转换形式(见下表) 
0标志
可用于 d,iu,o,x,X,E,f,g,G 。使用 d,i,u,o,x,X 类型(type)时,如果给出了精度(如:%04.d),0标志就被忽略。 如果代码中出现了负号标志,0标志也没有效果
空格标志
和正号标志是互斥的,如果两个同时给出,空格标志将被忽略。
用于..#标志o保证产生的值以一个零开头x,X在非零值前面加0x前缀(%X则为0X)e,E,f,(a,A)确保结果始终包含一个小数点,即使他后面没有数字g,G和上面的e,E,f相同。另外,缀尾的0并不从小数中去除c,d,i,u,s忽略该标志

字段宽度[width]

字符宽度是一个十进制数,用于指定将出现在结果中的 最小 字符数.如果值得字符数少于字段宽度,就会对它填充以增加长度. flags决定填充是用空白字符还是零以及它出现在值的左边还是右边.

字段精度[.precision]

精度已一个 句点(.) 开头,后面是一个可选的十进制整数. 如果未给出整数,精度缺省值为零

对于d,u,u,o,x,X类型的转换,精度字段制定将出现在结果中的 最小数字的个数 并覆盖零标志. 如果转换后的值的位数小于宽度,就在它的前面插入零. 如果值为零且精度也为零,则转换结果就不会产生数字.

对于e,E,f类型的转换,精度决定将出现在 小数点之后的数字位数 .

对于g,G类型的转换,它制定将出现在结果中的 最大 有效位数.

当使用s类型转换时,精度指定将被转换的 最多 的字符数.

Tip

如果用于表示字段宽度 和/或 精度的十进制整数由一个 星号(*) 代替, 那么printf的下一个参数(必须是个整数)就提供宽度 和/或 精度. 因此这些值可以通过计算活动而不必预先设定.

printf("%.*s \n", 3, "abcdef"); // abc  (相当于%.3s)printf("%0*s \n", 5, "abc"); // 00abc  (相当于%05s)printf("%0*.*s \n", 5, 3, "abcdef"); // 00abc  (相当于%05.3s)

修改符[{hh | h | l | ll | j | z | t | L}]

C99

To specifyUse prefixWith type specifierCompilersigned charhhd, i, o, x, or XGCCunsigned charhho, u, x, or XGCCshort inthd, i, o, x, or XGCC VSunsigned short intho, u, x, or XGCC VSlong intl (lowercase L)d, i, o, x, or X unsigned long intlo, u, x, or X wint_tlc wchar_tls long long intlld, i, o, x, or X unsigned long long intlo, u, x, or X long doublel or La, A, e, E, f, f, g, or G 
l
修改符 对 a, A, e, E, f, F, g,or G 类型 无效。
j (GCC)
Specifies that a following d, i, o, u, x,or X conversion specifier applies to an intmax_t or uintmax_t argument; or that a following n conversion specifier applies to a pointer to an intmax_t argument. 表明接下来的d,i,o,u,x或X转换说明符会应用于intmax_t或uintmax_t参数, 或者表明接下来的转换说明符n会应用于一个只想intmax_t参数的指针
z (GCC)
Specifies that a following d, i, o, u, x,or X conversion specifier applies to a size_t or the corresponding signed integer type argument; or that a following n conversion specifier applies to a pointer to a signed integer type corresponding to size_t argument. 表明接下来的转换说明符d,i,o,,u,x或X会应用于一个size_t或对应的有符号整数类型的参数; 或者表明接下来的转换说明符n会应用于一个指针,该指针指向一个对应于size_t类型的参数的有符号整数类型
t (GCC)
Specifies that a following d, i, o, u, x,or X conversion specifier applies to a ptrdiff_t or the corresponding unsigned integer type argument; or that a following n conversion specifier applies to a pointer to a ptrdiff_t argument. 表明接下来的转换说明符d,i,o,,u,x或X会应用于ptrdiff_t或相应的无符号整数类型的参数; 或者表明接下来的n会应用于一个指向ptrdiff_t类型的参数的指针

MS VC++

数值类型
To specifyUse prefixWith type specifierlong intl (lowercase L)d, i, o, x, or Xlong unsigned intlo, u, x, or Xlong longlld, i, o, x, or Xshort inthd, i, o, x, or Xshort unsigned intho, u, x, or X__int32I32d, i, o, x, or Xunsigned __int32I32o, u, x, or X__int64I64d, i, o, x, or Xunsigned __int64I64o, u, x, or Xptrdiff_tId, i, o, x, or Xsize_tIo, u, x, or Xlong doublel or Lf
ptrdiff_t
(that is, __int32 on 32-bit platforms, __int64 on 64-bit platforms)
size_t
(that is, unsigned __int32 on 32-bit platforms, unsigned __int64 on 64-bit platforms)
字符类型
To specifyUse prefixWith type specifierSingle-byte character with printf functionshc or CSingle-byte character with wprintf functionshc or CWide character with printf functionslc or CWide character with wprintf functionslc or CSingle-byte – character string with printf functionshs or SSingle-byte – character string with wprintf functionshs or SWide-character string with printf functionsls or SWide-character string with wprintf functionsls or SWide characterwcWide-character stringws
字符类型等同于
To print character asUse functionWith format specifiersingle byteprintfc, hc, or hCsingle bytewprintfC, hc, or hCwidewprintfc, lc, lC, or wcwideprintfC, lc, lC, or wc

参考资料

[1]http://www.open-std.org/jtc1/sc22/wg14/www/docs/C99RationaleV5.10.pdf[2]http://msdn.microsoft.com/en-us/library/56e442dc(v=vs.80).aspx[3]http://msdn.microsoft.com/en-us/library/8aky45ct(v=vs.80).aspx[4]http://msdn.microsoft.com/en-us/library/0ecbz014(v=vs.80).aspx[5]http://msdn.microsoft.com/en-us/library/25366k66(v=vs.80).aspx[6]http://msdn.microsoft.com/en-us/library/tcxf1dw6(v=vs.80).aspx[7]http://msdn.microsoft.com/en-us/library/hf4y5e3w(v=vs.80).aspx