The printf function formats and prints a series of characters and values to the standard output stream. If arguments follow the format string, the format string must contain specifications that determine the output format for the arguments.

The format argument consists of ordinary characters, and (if arguments follow format) format specifications. The ordinary characters are copied to output in order of their appearance.

Format specifications always begin with a percent sign (%) and are read left to right. When printf encounters the first format specification (if any), it converts the value of the first argument after format and outputs it accordingly. The second format specification causes the second argument to be converted and output, and so on. If there are more arguments than there are format specifications, the extra arguments are ignored. The results are undefined if there are not enough arguments for all the format specifications.

Format specification syntax

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.

A conversion specification consists of optional and required fields in this form:

%[flags][width][.precision]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. For example, %s specifies a string conversion. To print a percent-sign character, use %%.

Type conversion specifier

The type conversion specifier character specifies whether to interpret the corresponding argument as a character, a string, 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.

Type field characters

Type character Argument Output format
c Integer Character
C Integer Character
d Integer Signed decimal integer.
i Integer Signed decimal integer.
o Integer Unsigned octal integer.
u Integer Unsigned decimal integer.
x Integer Unsigned hexadecimal integer; uses "abcdef."
X Integer Unsigned hexadecimal integer; uses "ABCDEF."
e Floating-point Signed value that has the form [-]d.dddde±dd[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.
E Floating-point Identical to the e format except that E rather than e introduces the exponent.
f Floating-point 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.
F Floating-point Identical to the f format except that infinity and nan output is capitalized.
g Floating-point Signed 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.
g Floating-point Identical to the g format, except that E, rather than e, introduces the exponent (where appropriate).
a Floating-point Signed hexadecimal double-precision floating-point value that has the form [-]0xh.hhhhp±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.
A Floating-point Signed hexadecimal double-precision floating-point value that has the form [-]0Xh.hhhhP±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.
s String Characters are displayed up to the end character or until the precision value is reached.
S String Characters are displayed up to the end character or until the precision value is reached.

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

Flag 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 (-).
0 If width is prefixed by 0, leading zeros are added until the minimum width is reached. If both 0 and - appear, the 0 is ignored. 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. 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.
# 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.
# 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.
# 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. 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. 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 isn't given, all characters of the value are output, subject to the precision specification.

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*d", 5, 3); /* 00003 is output */

A missing or small width value in a conversion specification doesn't 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. 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 */

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
a, A The precision specifies the number of digits after the point. Default precision is 13. If precision is 0, no decimal point is printed unless the # flag is used.
c, C The precision has no effect. Character is printed.
d, 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. Default precision is 1.
e, E The precision specifies the number of digits to be printed after the decimal point. The last printed digit is rounded. Default precision is 6. If precision is 0 or the period (.) appears without a number following it, no decimal point is printed.
f, 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. Default precision is 6. If precision is 0, or if the period (.) appears without a number following it, no decimal point is printed.
g, G The precision specifies the maximum number of significant digits printed. Six significant digits are printed, and any trailing zeros are truncated.
s, S The precision specifies the maximum number of characters to be printed. Characters in excess of precision aren't printed. Characters are printed until a null character is encountered.