printf Type Field Characters

 

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

In a format specification, the type character is a conversion specifier that specifies whether the corresponding argument is to be interpreted as a character, a string, a pointer, an integer, or a floating-point number. The type character is the only required format 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. 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. 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. 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.

Integer 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, g, and G. By default, unless they are modified by a size field length prefix, integer arguments are coerced to int type, and floating-point arguments are coerced to double. 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. Pointer types that are specified by p use the default length for the platform.

System_CAPS_ICON_note.jpg Note

The C, S, and Z type characters, and the behavior of the c and s type characters when they are used with the printf and wprintf functions, are Microsoft extensions and are not ANSI compatible. Visual C++ does not support the F type character.

printf Type Field Characters

Type CharacterArgumentOutput format
cCharacterWhen used with printf functions, specifies a single-byte character; when used with wprintf functions, specifies a wide character.
CCharacterWhen used with printf functions, specifies a wide character; when used with wprintf functions, specifies a single-byte character.
dIntegerSigned decimal integer.
iIntegerSigned decimal integer.
oIntegerUnsigned octal integer.
uIntegerUnsigned decimal integer.
xIntegerUnsigned hexadecimal integer; uses "abcdef."
XIntegerUnsigned hexadecimal integer; uses "ABCDEF."
eFloating-pointSigned value that has the form [ – ]d.dddd e [sign]dd[d] where d is one decimal digit, dddd is one or more decimal digits, dd[d] is two or three decimal digits depending on the output format and size of the exponent, and sign is + or –.
EFloating-pointIdentical to the e format except that E rather than e introduces the exponent.
fFloating-pointSigned 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.
gFloating-pointSigned values are displayed in f or e format, whichever is more compact for the given value and precision. 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.
GFloating-pointIdentical to the g format, except that E, rather than e, introduces the exponent (where appropriate).
aFloating-pointSigned hexadecimal double-precision floating-point value that has the form [−]0xh.hhhh dd, 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.
AFloating-pointSigned hexadecimal double-precision floating-point value that has the form [−]0Xh.hhhh dd, 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.
nPointer to integerNumber 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. See the Security Note later in this article.
pPointer typeDisplays the argument as an address in hexadecimal digits.
sStringWhen 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.
SStringWhen 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.
ZANSI_STRING or UNICODE_STRING structureWhen the address of an ANSI_STRING or UNICODE_STRING structure is passed as the argument, displays the string that's contained in the buffer that's pointed to by the Buffer field of the structure. Use a length modifier prefix of w to specify a UNICODE_STRING argument—for example, %wZ. The Length field of the structure must be set to the length, in bytes, of the string. The MaximumLength field of the structure must be set to the length, in bytes, of the buffer.

Typically, the Z type character is used only in driver debugging functions that use a format specification, such as dbgPrint and kdPrint.

If the argument that corresponds to a floating-point conversion specifier is infinite, indefinite, or NAN, the following table lists the formatted output.

ValueOutput
+ infinity1.#INF random-digits
– infinity–1.#INF random-digits
Indefinite (same as quiet NaN)digit .#IND random-digits
NANdigit .#NAN random-digits
System_CAPS_ICON_note.jpg Note

If the the Buffer field of the argument that corresponds to %Z, or of the argument that corresponds to %s or %S, is a null pointer, "(null)" will be displayed.

System_CAPS_ICON_note.jpg Note

In all exponential formats, the default number of digits of exponent to display is three. By using the _set_output_format function, you can set the number of digits displayed to two but expanding to three if demanded by the size of exponent.

System_CAPS_ICON_important.jpg Important

Because the %n format is inherently insecure, it is disabled by default. If %n is encountered in a format string, the invalid parameter handler is invoked, as described in Parameter Validation. To enable %n support, see _set_printf_count_output.

printf, _printf_l, wprintf, _wprintf_l
Format Specification Syntax: printf and wprintf Functions
Flag Directives
printf Width Specification
Precision Specification
Size Specification
_set_output_format

Show: