格式规范语法:printf 和 wprintf 函数Format specification syntax: printf and wprintf functions

各种 printfwprintf 函数采用格式字符串和可选参数,并生成用于输出的格式化字符序列。The various printf and wprintf functions take a format string and optional arguments and produce a formatted sequence of characters for output. 格式字符串包含零个或多个指令,这些指令是用于输出的文本字符或描述如何在输出中设置参数格式的已编码的转换规范。The format string contains zero or more directives, which are either literal characters for output or encoded conversion specifications that describe how to format an argument in the output. 本主题介绍用于对格式字符串中的转换规范进行编码的语法。This topic describes the syntax used to encode conversion specifications in the format string. 有关这些函数的列表,请参阅流 I/OFor a listing of these functions, see Stream I/O.

一个转换规范由以下形式的可选和必需字段组成:A conversion specification consists of optional and required fields in this form:

%[标志][宽度][.精度][大小]类型%[flags][width][.precision][size]type

转换规范的每个字段都是一个用于指示特定的格式选项或转换说明符的字符或数字。Each field of the conversion specification is a character or a number that signifies a particular format option or conversion specifier. 必填的类型字段指定要应用于参数的转换类型。The required type field specifies the kind of conversion to be applied to an argument. 可选的标志、宽度和精度字段控制格式的其他方面(如前导空格或前导零、对齐方式和显示的精度)。The optional flags, width, and precision fields control additional format aspects such as leading spaces or zeroes, justification, and displayed precision. 大小字段指定使用和转换的参数的大小。The size field specifies the size of the argument consumed and converted.

一个基本的转换规范仅包含百分号和一个类型字符。A basic conversion specification contains only the percent sign and a type character. 例如,%s 指定一个字符串转换。For example, %s specifies a string conversion. 若要打印百分号字符,请使用 %%To print a percent-sign character, use %%. 如果百分号后跟一个没有任何意义的字符作为格式字段,则将调用无效的参数处理程序。If a percent sign is followed by a character that has no meaning as a format field, the invalid parameter handler is invoked. 有关详细信息,请参阅参数验证For more information, see Parameter Validation.

重要

为了实现安全性和稳定性,请确保转换规范字符串不是用户定义的。For security and stability, ensure that conversion specification strings are not user-defined. 例如,考虑这样一个程序,它提示用户输入名称并将输入存储在一个名为 user_name 的字符串变量中。For example, consider a program that prompts the user to enter a name and stores the input in a string variable that's named user_name. 若要打印 user_name,请勿执行下列操作:To print user_name, do not do this:

printf( user_name ); /* Danger! If user_name contains "%s", program will crash */

而应执行以下操作:Instead, do this:

printf( "%s", user_name );

类型转换说明符Type conversion specifier

类型转换说明符字符指定是否要将相应的参数解释为字符、字符串、指针、整数或浮点数。The type conversion specifier character specifies whether to interpret the corresponding argument as a character, a string, a pointer, an integer, or a floating-point number. 类型字符是唯一必填的转换规范字段,它出现在任何可选字段之后。The type character is the only required conversion specification field, and it appears after any optional fields.

将根据相应的类型字符和可选的大小前缀对紧跟格式字符串的参数进行解释。The arguments that follow the format string are interpreted according to the corresponding type character and the optional size prefix. 将通过使用 cC指定字符类型 charwchar_t 的转换,将通过使用 sS 指定单字节和多字节或宽字符字符串,具体取决于正在使用的格式设置函数。Conversions for character types char and wchar_t are specified by using c or C, and single-byte and multi-byte or wide character strings are specified by using s or S, depending on which formatting function is being used. 通过使用 cs 指定的字符和字符串参数将被 printf 系列函数解释为 charchar*,或被 wprintf 系列函数解释为 wchar_twchar_t*Character and string arguments that are specified by using c and s are interpreted as char and char* by printf family functions, or as wchar_t and wchar_t* by wprintf family functions. 通过使用 CS 指定的字符和字符串参数将被 printf 系列函数解释为 wchar_twchar_t*,或被 wprintf 系列函数解释为 charchar*Character and string arguments that are specified by using C and S are interpreted as wchar_t and wchar_t* by printf family functions, or as char and char* by wprintf family functions. 此行为是 Microsoft 专用的。This behavior is Microsoft specific.

将通过使用 diouxX 指定整数类型(如 shortintlonglong long)及其 unsigned 变体。将通过使用 aAeEfFgG 指定浮点类型(如 floatdoublelong double)。默认情况下,除非由大小前缀进行修改,否则整数参数将被强制为 int 类型,浮点参数将被强制为 doubleInteger types such as short, int, long, long long, and their unsigned variants, are specified by using d, i, o, u, x, and X. Floating-point types such as float, double, and long double, are specified by using a, A, e, E, f, F, g, and G. By default, unless they are modified by a size prefix, integer arguments are coerced to int type, and floating-point arguments are coerced to double. 在 64 位系统上,int 是 32 位的值;因此,确定 64 位整数的输出格式时,将把它截断,除非使用 llI64 的大小前缀。On 64-bit systems, an int is a 32-bit value; therefore, 64-bit integers will be truncated when they are formatted for output unless a size prefix of ll or I64 is used. p 指定的指针类型使用平台的默认指针大小。Pointer types that are specified by p use the default pointer size for the platform.

备注

Microsoft 专用Microsoft Specific
printfwprintf 函数一起使用时,Z 类型字符以及 cCsS 类型字符的行为是 Microsoft 扩展。The Z type character, and the behavior of the c, C, s, and S type characters when they are used with the printf and wprintf functions, are Microsoft extensions. 在所有的格式设置函数中,ISO C 标准始终对窄字符和字符串使用 cs,而对宽字符和字符串使用 CSThe ISO C standard uses c and s consistently for narrow characters and strings, and C and S for wide characters and strings, in all formatting functions.

类型字段字符Type field characters

类型字符Type character 参数Argument 输出格式Output format
cc 字符Character printf 函数一起使用时,指定单字节字符;与 wprintf 函数一起使用时,指定宽字符。When used with printf functions, specifies a single-byte character; when used with wprintf functions, specifies a wide character.
CC 字符Character printf 函数一起使用时,指定宽字符;与 wprintf 函数一起使用时,指定单字节字符。When used with printf functions, specifies a wide character; when used with wprintf functions, specifies a single-byte character.
dd 整数Integer 带符号十进制整数。Signed decimal integer.
ii 整数Integer 带符号十进制整数。Signed decimal integer.
oo 整数Integer 无符号八进制整数。Unsigned octal integer.
uu 整数Integer 无符号十进制整数。Unsigned decimal integer.
xx 整数Integer 无符号十六进制整数;使用“abcdef.”Unsigned hexadecimal integer; uses "abcdef."
XX 整数Integer 无符号十六进制整数;使用“ABCDEF.”Unsigned hexadecimal integer; uses "ABCDEF."
ee 浮点Floating-point 有符号的值,形式为 [-]d.dddddd[d],其中 d 是一个十进制数,dddd 是一个或多个十进制数(具体取决于指定的精度),或为默认的六个数,dd[d] 是两个或三个十进制数(具体取决于输出格式和指数大小)。Signed value that has the form [-]d.dddddd[d] where d is one decimal digit, dddd is one or more decimal digits depending on the specified precision, or six by default, and dd[d] is two or three decimal digits depending on the output format and size of the exponent.
EE 浮点Floating-point e 格式相同,只不过指数由 E 引入,而不是由 e 引入。Identical to the e format except that E rather than e introduces the exponent.
ff 浮点Floating-point 有符号的值,形式为 [-]dddd.dddd,其中 dddd 是一个或多个十进制数。Signed value that has the form [-]dddd.dddd, where dddd is one or more decimal digits. 小数点前的数字位数取决于数字的度量值,小数点后的数字位数取决于请求的精度,或为默认的六位数。The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on the requested precision, or six by default.
FF 浮点Floating-point f 格式相同,只不过 infinity 和 nan 输出为大写形式。Identical to the f format except that infinity and nan output is capitalized.
gg 浮点Floating-point 有符号的值将显示为 fe 格式,取其中对于给定的值和精度更为精简一个。Signed values are displayed in f or e format, whichever is more compact for the given value and precision. 仅当值的指数小于 -4 或大于等于 precision 参数时,才使用 e 格式。The e format is used only when the exponent of the value is less than -4 or greater than or equal to the precision argument. 截去尾随零,仅当后跟一个或多个数字时,才会显示小数点。Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it.
GG 浮点Floating-point g 格式相同,只不过指数由 E 引入,而不是由 e 引入(如果适用)。Identical to the g format, except that E, rather than e, introduces the exponent (where appropriate).
aa 浮点Floating-point 有符号的十六进制双精度浮点值,形式为 [-]0xh.hhhhdd,其中 h.hhhh 是尾数的十六进制数(使用小写字母),dd 是一位指数或多位指数。Signed hexadecimal double-precision floating-point value that has the form [-]0xh.hhhhdd, where h.hhhh are the hex digits (using lower case letters) of the mantissa, and dd are one or more digits for the exponent. 精度指定此点后的数字位数。The precision specifies the number of digits after the point.
AA 浮点Floating-point 有符号的十六进制双精度浮点值,形式为 [-]0Xh.hhhhdd,其中 h.hhhh 是尾数的十六进制数(使用大写字母),dd 是一位指数或多位指数。Signed hexadecimal double-precision floating-point value that has the form [-]0Xh.hhhhdd, where h.hhhh are the hex digits (using capital letters) of the mantissa, and dd are one or more digits for the exponent. 精度指定此点后的数字位数。The precision specifies the number of digits after the point.
n 指向整数的指针Pointer to integer 目前成功写入流或缓冲区的字符数。Number of characters that are successfully written so far to the stream or buffer. 此值存储在地址作为自变量的整数中。This value is stored in the integer whose address is given as the argument. 可通过参数大小规范前缀控制指向的整数的大小。The size of the integer pointed to can be controlled by an argument size specification prefix. n 说明符默认为禁用;请参阅重要的安全说明了解相关信息。The n specifier is disabled by default; for information see the important security note.
pp 指针类型Pointer type 将自变量显示为十六进制数中的地址。Displays the argument as an address in hexadecimal digits.
ss 字符串String printf 函数一起使用时,指定单字节或多字节字符串;与 wprintf 函数一起使用时,指定宽字符字符串。When used with printf functions, specifies a single-byte or multi-byte character string; when used with wprintf functions, specifies a wide-character string. 将于第一个空字符之前或达到精度值时显示字符。Characters are displayed up to the first null character or until the precision value is reached.
SS 字符串String printf 函数一起使用时,指定宽字符字符串;与 wprintf 函数一起使用时,指定单字节或多字节字符串。When used with printf functions, specifies a wide-character string; when used with wprintf functions, specifies a single-byte or multi-byte character string. 将于第一个空字符之前或达到精度值时显示字符。Characters are displayed up to the first null character or until the precision value is reached.
ZZ ANSI_STRINGUNICODE_STRING 结构ANSI_STRING or UNICODE_STRING structure ANSI_STRINGUNICODE_STRING 结构的地址作为参数传递时,会显示包含在由结构的 Buffer 字段指向的缓冲区中的字符串。When the address of an ANSI_STRING or UNICODE_STRING structure is passed as the argument, displays the string contained in the buffer pointed to by the Buffer field of the structure. 使用 w 的大小修饰符前缀指定 UNICODE_STRING 参数,例如 %wZUse a size modifier prefix of w to specify a UNICODE_STRING argument—for example, %wZ. 结构的 Length 字段必须设置为字符串的长度(以字节为单位)。The Length field of the structure must be set to the length, in bytes, of the string. 结构的 MaximumLength 字段必须设置为缓冲区的长度(以字节为单位)。The MaximumLength field of the structure must be set to the length, in bytes, of the buffer.

通常情况下,Z 类型字符仅在使用转换规范的驱动程序调试函数(如 dbgPrintkdPrint)中使用。Typically, the Z type character is used only in driver debugging functions that use a conversion specification, such as dbgPrint and kdPrint.

从 Visual Studio 2015 开始,如果对应浮点转换说明符(aAeEfFgG)的参数为无穷大、不定或 NaN,格式化的输出则符合 C99 标准。Starting in Visual Studio 2015, if the argument that corresponds to a floating-point conversion specifier (a, A, e, E, f, F, g, G) is infinite, indefinite, or NaN, the formatted output conforms to the C99 standard. 下表列出了格式化的输出:This table lists the formatted output:

Value 输出Output
infinityinfinity inf
静默 NaNQuiet NaN nan
信号 NaNSignalling NaN nan(snan)
不定 NaNIndefinite NaN nan(ind)

可能以符号作为其中任何一个值的前缀。Any of these values may be prefixed by a sign. 如果浮点类型转换说明符字符是一个大写字母,则输出也将使用大写字母格式。If a floating-point type conversion specifier character is a capital letter, then the output is also formatted in capital letters. 例如,如果格式说明符是 %F而不是 %f,则 infinity 的格式将被设置为 INF,而不是 infFor example, if the format specifier is %F instead of %f, an infinity is formatted as INF instead of inf. scanf 函数也可以分析这些字符串,使这些值可以通过 printfscanf 函数进行往返。The scanf functions can also parse these strings, so these values can make a round-trip through printf and scanf functions.

在 Visual Studio 2015 之前,CRT 使用一种不同的非标准格式作为无穷大、不定和 NaN 值的输出:Before Visual Studio 2015, the CRT used a different, non-standard format for output of infinite, indefinite, and NaN values:

Value 输出Output
+ 无穷+ infinity 1.#INF 随机数字1.#INF random-digits
- 无穷- infinity -1.#INF 随机数字-1.#INF random-digits
不定(与静默 NaN 相同)Indefinite (same as quiet NaN) 数字 .#IND 随机数字digit .#IND random-digits
NaNNaN 数字 .#NAN 随机数字digit .#NAN random-digits

其中任何一种都可能已采用符号作为前缀并且格式设置也可能略有不同,具体取决于字段宽度和精度,有时会起到不寻常的作用。Any of these may have been prefixed by a sign, and may have been formatted slightly differently depending on field width and precision, sometimes with unusual effects. 例如,printf("%.2f\n", INFINITY) 可以打印 1.#J,因为 #INF 会“四舍五入”到 2 位数的精度。For example, printf("%.2f\n", INFINITY) would print 1.#J because the #INF would be "rounded" to a precision of 2 digits.

备注

如果与 %s%S 对应的参数,或与 %Z 对应的参数的 Buffer 字段为空指针,则将显示“(null)”。If the argument that corresponds to %s or %S, or the Buffer field of the argument that corresponds to %Z, is a null pointer, "(null)" is displayed.

备注

在所有的指数格式中,要显示的指数的位数最少为两位,仅在必要时使用三位。In all exponential formats, the minimum number of digits of exponent to display is two, using three only if necessary. 通过使用 _set_output_format 函数,可以将显示的数字位数设置为三位,以确保与为 Visual Studio 2013 及更早版本编写的代码的后向兼容性。By using the _set_output_format function, you can set the number of digits displayed to three for backward compatibility with code written for Visual Studio 2013 and before.

重要

%n 格式在本质上是不安全的,因此它默认处于禁用状态。Because the %n format is inherently insecure, it is disabled by default. 如果在格式字符串中遇到 %n,则将调用无效的参数处理程序,如参数验证中所述。If %n is encountered in a format string, the invalid parameter handler is invoked, as described in Parameter Validation. 若要启用 %n 支持,请参阅 _set_printf_count_outputTo enable %n support, see _set_printf_count_output.

标志指令Flag directives

转换规范中的第一个可选字段包含标志指令、零个或多个标志字符,用于指定输出对齐方式以及控制符号、空白、前导零、小数点以及八进制和十六进制前缀的输出。The first optional field in a conversion specification contains flag directives, zero or more flag characters that specify output justification and control output of signs, blanks, leading zeros, decimal points, and octal and hexadecimal prefixes. 转换规范中可能会出现多个标志指令,并且标志字符可能会按任意顺序出现。More than one flag directive may appear in a conversion specification, and the flag characters can appear in any order.

标志字符Flag characters

FlagFlag 含义Meaning 默认Default
- 在给定的字段宽度内左对齐结果。Left align the result within the given field width. 右对齐。Right align.
+ 如果输出值为有符号的类型,则在该值前使用符号(+ 或 -)作为前缀。Use a sign (+ or -) to prefix the output value if it is of a signed type. 只对有符号的负值 (-) 显示符号。Sign appears only for negative signed values (-).
00 如果将 0 作为宽度的前缀,则会在达到最小宽度前添加前导零。If width is prefixed by 0, leading zeros are added until the minimum width is reached. 如果 0- 同时出现,0 则将被忽略。If both 0 and - appear, the 0 is ignored. 如果为整数格式(iuxXod)指定了 0,并且还存在精度规范(例如 %04.d),0 则将被忽略。If 0 is specified for an integer format (i, u, x, X, o, d) and a precision specification is also present—for example, %04.d—the 0 is ignored. 如果为 aA 浮点格式指定了 0,则会在 0x0X 前缀后,在尾数前追加前导零。If 0 is specified for the a or A floating-point format, leading zeros are prepended to the mantissa, after the 0x or 0X prefix. 不填充。No padding.
空白 (' ')blank (' ') 如果输出值为有符号的正值,则使用空白作为其前缀。Use a blank to prefix the output value if it is signed and positive. 如果空白和 + 标志同时出现,空白则将被忽略。The blank is ignored if both the blank and + flags appear. 没有显示空白。No blank appears.
# oxX 格式一起使用时,# 标志将分别使用 0、0x 或 0X 作为任何非零输出值的前缀。When it's used with the o, x, or X format, the # flag uses 0, 0x, or 0X, respectively, to prefix any nonzero output value. 没有显示空白。No blank appears.
eEfFaA 格式一起使用时,# 标志将强制输出值包含小数点。When it's used with the e, E, f, F, a or A format, the # flag forces the output value to contain a decimal point. 仅当小数点后紧跟数字时,才会显示小数点。Decimal point appears only if digits follow it.
gG 格式一起使用时,# 标志将强制输出值包含小数点,并阻止截断尾随零。When it's used with the g or G format, the # flag forces the output value to contain a decimal point and prevents the truncation of trailing zeros.

cdius 一起使用时,则将被忽略。Ignored when used with c, d, i, u, or s.
仅当小数点后紧跟数字时,才会显示小数点。Decimal point appears only if digits follow it. 尾随零将被截断。Trailing zeros are truncated.

宽度规范Width specification

在转换规范中,可选宽度规范字段出现在任何标志字符之后。In a conversion specification, the optional width specification field appears after any flags characters. 宽度参数是控制输出的最小字符数量的非负十进制整数。The width argument is a non-negative decimal integer that controls the minimum number of characters that are output. 如果输出值中的字符数小于指定宽度,则将在值的左侧或右侧添加空白(具体取决于是否指定了左对齐标志 (-)),直到达到最小宽度为止。If the number of characters in the output value is less than the specified width, blanks are added to the left or the right of the values—depending on whether the left-alignment flag (-) is specified—until the minimum width is reached. 如果 0 作为宽度的前缀,则将向整数或浮点转换添加前导零,直到达到最小宽度为止,但转换到 infinity 或 NaN 时除外。If width is prefixed by 0, leading zeros are added to integer or floating-point conversions until the minimum width is reached, except when conversion is to an infinity or NaN.

宽度规范永远不会导致值被截断。The width specification never causes a value to be truncated. 如果输出值中的字符数大于指定宽度,或如果未提供宽度,则将根据精度规范输出值中的所有字符。If the number of characters in the output value is greater than the specified width, or if width is not given, all characters of the value are output, subject to the precision specification.

如果宽度规范是一个星号 (*),则参数列表中的 int 参数将提供此值。If the width specification is an asterisk (*), an int argument from the argument list supplies the value. 宽度参数必须先于在参数列表中要设置其格式的值,如以下示例中所示:The width argument must precede the value that's being formatted in the argument list, as shown in this example:

printf("%0*f", 5, 3); /* 00003 is output */

转换规范中缺少宽度值或此值较小将不会导致截断输出值。A missing or small width value in a conversion specification does not cause the truncation of an output value. 如果转换结果的宽度大于宽度值,则字段将扩展以包含转换结果。If the result of a conversion is wider than the width value, the field expands to contain the conversion result.

精度规范Precision specification

在转换规范中,第三个可选字段是精度规范。In a conversion specification, the third optional field is the precision specification. 它包含一个句点 (.),后跟一个非负十进制整数,指定字符串字符数、小数位数或要输出的有效数字位数,具体取决于转换类型.It consists of a period (.) followed by a non-negative decimal integer that, depending on the conversion type, specifies the number of string characters, the number of decimal places, or the number of significant digits to be output.

与宽度规范不同的是,精度规范可能导致输出值截断或浮点值舍入。Unlike the width specification, the precision specification can cause either truncation of the output value or rounding of a floating-point value. 如果将精度指定为 0 并且要转换的值为 0,则结果为无字符输出,如以下示例中所示:If precision is specified as 0 and the value to be converted is 0, the result is no characters output, as shown in this example:

printf( "%.0d", 0 ); /* No characters output */

如果精度规范是一个星号 (*),则参数列表中的某个 int 参数将提供此值。If the precision specification is an asterisk (*), an int argument from the argument list supplies the value. 在参数列表中,精度参数前必须先于要设置其格式的值,如以下示例中所示:In the argument list, the precision argument must precede the value that's being formatted, as shown in this example:

printf( "%.*f", 3, 3.14159265 ); /* 3.142 output */

如果省略精度,则类型字符将决定精度的解释或默认精度,如下表中所示。The type character determines either the interpretation of precision or the default precision when precision is omitted, as shown in the following table.

精度值如何影响类型How Precision Values Affect Type

类型Type 含义Meaning 默认Default
aAa, A 精度指定此点后的数字位数。The precision specifies the number of digits after the point. 默认精度为 13。Default precision is 13. 如果精度为 0,除非使用了 # 标志,否则不会打印小数点。If precision is 0, no decimal point is printed unless the # flag is used.
cCc, C 精度不产生任何影响。The precision has no effect. 打印字符。Character is printed.
diouxXd, i, o, u, x, X 精度指定要打印的最小数字位数。The precision specifies the minimum number of digits to be printed. 如果参数中的数字位数小于精度,则将在输出值的左侧使用零进行填充。If the number of digits in the argument is less than precision, the output value is padded on the left with zeros. 数字位数超过精度时,值将不会被截断。The value is not truncated when the number of digits exceeds precision. 默认精度为 1。Default precision is 1.
eEe, E 精度指定此小数点后要打印的数字位数。The precision specifies the number of digits to be printed after the decimal point. 打印的最后一位数舍入。The last printed digit is rounded. 默认精度为 6。Default precision is 6. 如果精度为 0,或者如果句点 (.) 后面不跟数字,则不会打印小数点。If precision is 0 or the period (.) appears without a number following it, no decimal point is printed.
fFf, F 精度值指定此小数点后的数字位数。The precision value specifies the number of digits after the decimal point. 如果出现小数点,则在它之前至少会显示一个数字。If a decimal point appears, at least one digit appears before it. 该值舍入为适当数量的数字。The value is rounded to the appropriate number of digits. 默认精度为 6。Default precision is 6. 如果精度为 0,或者如果句点 (.) 后面不跟数字,则不会打印小数点。If precision is 0, or if the period (.) appears without a number following it, no decimal point is printed.
gGg, G 精度指定打印的最大有效位数。The precision specifies the maximum number of significant digits printed. 打印六个有效位数,并且任何尾随零都会被截断。Six significant digits are printed, and any trailing zeros are truncated.
sSs, S 精度指定要打印的最大字符数。The precision specifies the maximum number of characters to be printed. 不会打印超过精度的字符。Characters in excess of precision are not printed. 在遇到 null 字符之前不会打印字符。Characters are printed until a null character is encountered.

参数大小规范Argument size specification

在转换规范中,大小字段是类型转换说明符的参数长度修饰符。In a conversion specification, the size field is an argument length modifier for the type conversion specifier. 大小字段作为类型字段(hhhjl(小写的 L)、LlltwzI(大写的 i)、I32I64)的前缀,根据它们修饰的转换说明符,指定对应参数的“大小”(长型或短型、32 位或 64 位、单字节字符或宽字符)。The size field prefixes to the type field—hh, h, j, l (lowercase L), L, ll, t, w, z, I (uppercase i), I32, and I64—specify the "size" of the corresponding argument—long or short, 32-bit or 64-bit, single-byte character or wide character—depending on the conversion specifier that they modify. 这些大小前缀在 printfwprintf 系列函数中与 类型 字符一起使用,以指定参数大小的解释(如下表中所示)。These size prefixes are used with type characters in the printf and wprintf families of functions to specify the interpretation of argument sizes, as shown in the following table. 大小字段对于某些参数类型是可选的。The size field is optional for some argument types. 未指定任何大小前缀时,格式化程序使用整数参数(例如,有符号或无符号的 charshortintlong 和枚举类型)作为 32 位 int 类型,而使用 floatdoublelong double 浮点参数作为 64 位 double 类型。When no size prefix is specified, the formatter consumes integer arguments—for example, signed or unsigned char, short, int, long, and enumeration types—as 32-bit int types, and float, double, and long double floating-point arguments are consumed as 64-bit double types. 这与变量自变量列表的默认自变量提升规则相匹配。This matches the default argument promotion rules for variable argument lists. 有关自变量提升的详细信息,请参阅后缀表达式中的“省略号和默认自变量”。For more information about argument promotion, see Ellipses and Default Arguments in Postfix expressions. 在 32 位和 64 位系统上,64 位整数参数的转换规范必须包含 llI64 大小前缀。On both 32-bit and 64-bit systems, the conversion specification of a 64-bit integer argument must include a size prefix of ll or I64. 否则,格式化程序的行为是不明确的。Otherwise, the behavior of the formatter is undefined.

某些类型在 32 位和 64 位代码中具有不同大小。Some types are different sizes in 32-bit and 64-bit code. 例如,size_t 在针对 x86 编译的代码中是 32 位长,而在针对 x64 编译的代码中是 64 位。For example, size_t is 32 bits long in code compiled for x86, and 64 bits in code compiled for x64. 若要为宽度可变的类型创建与平台无关的格式设置代码,可以使用宽度可变的参数大小修饰符。To create platform-agnostic formatting code for variable-width types, you can use a variable-width argument size modifier. 或者,使用 64 位参数大小修饰符,并将宽度可变的参数类型显式提升为 64 位。Alternatively, use a 64-bit argument size modifier and explicitly promote the variable-width argument type to 64 bits. 特定于 Microsoft 的 I(大写的 i)参数大小修饰符可处理宽度可变的整数参数,但我们建议使用特定于类型的 jtz 修饰符以确保可移植性。The Microsoft-specific I (uppercase i) argument size modifier handles variable-width integer arguments, but we recommend the type-specific j, t, and z modifiers for portability.

printf 和 wprintf 格式类型说明符的大小前缀Size Prefixes for printf and wprintf Format-Type Specifiers

若要指定To specify 使用前缀Use prefix 及类型说明符With type specifier
char
unsigned char
hhhh diouxXd, i, o, u, x, or X
short int
short unsigned int
hh diouxXd, i, o, u, x, or X
__int32
unsigned __int32
I32I32 diouxXd, i, o, u, x, or X
__int64
unsigned __int64
I64I64 diouxXd, i, o, u, x, or X
intmax_t
uintmax_t
j 或 I64j or I64 diouxXd, i, o, u, x, or X
long double l(小写的 L)或 Ll (lowercase L) or L aAeEfFgGa, A, e, E, f, F, g, or G
long int
long unsigned int
l(小写的 L)l (lowercase L) diouxXd, i, o, u, x, or X
long long int
unsigned long long int
ll(小写的 LL)ll (lowercase LL) diouxXd, i, o, u, x, or X
ptrdiff_t tI(大写的 i)t or I (uppercase i) diouxXd, i, o, u, x, or X
size_t zI(大写的 i)z or I (uppercase i) diouxXd, i, o, u, x, or X
单字节字符Single-byte character hh cCc or C
宽字符Wide character l(小写的 L)或 wl (lowercase L) or w cCc or C
单字节字符串Single-byte character string hh sSZs, S, or Z
宽字符字符串Wide-character string l(小写的 L)或 wl (lowercase L) or w sSZs, S, or Z

ptrdiff_tsize_t 类型在 32 位平台上为 __int32unsigned __int32,在 64 位平台上为 __int64unsigned __int64The ptrdiff_t and size_t types are __int32 or unsigned __int32 on 32-bit platforms, and __int64 or unsigned __int64 on 64-bit platforms. I(大写的 i)、jtz 大小前缀采用平台的正确参数宽度。The I (uppercase i), j, t, and z size prefixes take the correct argument width for the platform.

在 Visual C++ 中,虽然 long double 是互异的类型,但是它具有与 double 相同的内部表示形式。In Visual C++, although long double is a distinct type, it has the same internal representation as double.

hchC 类型说明符与 printf 函数中的 c 以及 wprintf 函数中的 C 是同义的。An hc or hC type specifier is synonymous with c in printf functions and with C in wprintf functions. lclCwcwC 类型说明符与 printf 函数中的 C 以及 wprintf 函数中的 c 是同义的。An lc, lC, wc or wC type specifier is synonymous with C in printf functions and with c in wprintf functions. hshS 类型说明符与 printf 函数中的 s 以及 wprintf 函数中的 S 是同义的。An hs or hS type specifier is synonymous with s in printf functions and with S in wprintf functions. lslSwswS 类型说明符与 printf 函数中的 S 以及 wprintf 函数中的 s 是同义的。An ls, lS, ws or wS type specifier is synonymous with S in printf functions and with s in wprintf functions.

备注

Microsoft 专用Microsoft Specific
I(大写的 i)、I32I64w 参数大小修饰符前缀是 Microsoft 扩展,且不符合 ISO C。The I (uppercase i), I32, I64, and w argument size modifier prefixes are Microsoft extensions and are not ISO C-compatible. h 前缀(在与 char 类型的数据一起使用时)和 l(小写 L)前缀(在与 double 类型的数据一起使用时)是 Microsoft 扩展。The h prefix when it's used with data of type char and the l (lowercase L) prefix when it's used with data of type double are Microsoft extensions.

另请参阅See Also

printf、_printf_l、wprintf、_wprintf_lprintf, _printf_l, wprintf, _wprintf_l
printf_s、_printf_s_l、wprintf_s、_wprintf_s_lprintf_s, _printf_s_l, wprintf_s, _wprintf_s_l
printf_p 位置参数printf_p Positional Parameters