Template:cpp/io/c/printf format

The format string consists of ordinary characters (except ), which are copied unchanged into the output stream, and conversion specifications. Each conversion specification has the following format:


 * introductory character


 * one or more flags that modify the behavior of the conversion:
 * : the result of the conversion is left-justified within the field (by default it is right-justified)
 * : the sign of signed conversions is always prepended to the result of the conversion (by default the result is preceded by minus only when it is negative)
 * space: if the result of a signed conversion does not start with a sign character, or is empty, space is prepended to the result. It is ignored if flag is present.
 * : alternative form of the conversion is performed. See the table below for exact effects otherwise the behavior is undefined.
 * : for integer and floating point number conversions, leading zeros are used to pad the field instead of space characters. For integer numbers it is ignored if the precision is explicitly specified. For other conversions using this flag results in undefined behavior. It is ignored if flag is present.


 * integer value or that specifies minimum field width. The result is padded with space characters (by default), if required, on the left when right-justified, or on the right if left-justified. In the case when  is used, the width is specified by an additional argument of type, which appears before the argument to be converted and the argument supplying precision if one is supplied. If the value of the argument is negative, it results with the  flag specified and positive field width. (Note: This is the minimum width: The value is never truncated.)


 * followed by integer number or, or neither that specifies precision of the conversion. In the case when is used, the precision is specified by an additional argument of type , which appears before the argument to be converted, but after the argument supplying minimum field width if one is supplied. If the value of this argument is negative, it is ignored. If neither a number nor  is used, the precision is taken as zero. See the table below for exact effects of precision.


 * length modifier that specifies the size of the argument (in combination with the conversion format specifier, it specifies the type of the corresponding argument)


 * conversion format specifier

The following format specifiers are available:

The floating point conversion functions convert infinity to or. Which one is used is implementation defined.

Not-a-number is converted to or. Which one is used is implementation defined.

The conversions, , , output , ,  instead.

Even though expects  argument, it is safe to pass a  because of the integer promotion that takes place when a variadic function is called.

The correct conversion specifications for the fixed-width character types (int8_t, etc) are defined in the header (although pridmax, priumax, etc is synonymous with, , etc).

The memory-writing conversion specifier is a common target of security exploits where format strings depend on user input and is not supported by the bounds-checked  family of functions.

There is a sequence point after the action of each conversion specifier; this permits storing multiple results in the same variable or, as an edge case, printing a string modified by an earlier  within the same call.

If a conversion specification is invalid, the behavior is undefined.