printf format specifier

general

%[flags][width][.precision][length]specifier

flag

FlagDescription
-Left-justify: The result is left-aligned within the specified field width. By default, it is right-aligned.
+Sign: The result of a signed conversion will always begin with a sign (+ or -). By default, only negative values have a sign.
(space)Space: If the first character of a signed conversion is not a sign, a space is prepended. This is ignored if the + flag is present.
#Alternate Form: For o, the output is prefixed with 0. For x and X, it's prefixed with 0x and 0X respectively. For a, A, e, E, f, F, g, and G, the output will always contain a decimal point.
0Zero-padding: For numeric types, the output is padded with leading zeros instead of spaces to fill the field width. This is ignored if the - flag is present.

width

The width sub-specifier sets the minimum number of characters to be printed.

WidthDescription
numberA non-negative integer specifying the minimum number of characters to print. If the printed value is shorter, it is padded with spaces (or zeros if the 0 flag is used).
*The width is not specified in the format string itself, but as an additional integer argument preceding the argument to be formatted.

precision

PrecisionDescription
.numberFor integer specifiers (d, i, u, o, x, X), it specifies the minimum number of digits to be written. For e, E, and f, it is the number of digits to appear after the decimal point. For g and G, it's the maximum number of significant digits. For s, it's the maximum number of characters to be printed.
.*The precision is not specified in the format string itself, but as an additional integer argument preceding the argument to be formatted.

length

ModifierFor Integer Specifiers (d, i, u, o, x, X)For Floating-Point Specifiers (f, e, g, a)
hshort int or unsigned short int-
hhsigned char or unsigned char-
llong int or unsigned long int-
lllong long int or unsigned long long int-
L-long double
jintmax_t or uintmax_t-
zsize_t or the corresponding signed type-
tptrdiff_t or the corresponding unsigned type-

specifier

SpecifierData TypeOutput Format
%ccharA single character.
%schar*A string of characters.
%d or %iintSigned decimal integer.
%uunsigned intUnsigned decimal integer.
%ounsigned intUnsigned octal integer.
%xunsigned intUnsigned hexadecimal integer (lowercase letters).
%Xunsigned intUnsigned hexadecimal integer (uppercase letters).
%fdoubleDecimal floating-point number.
%FdoubleDecimal floating-point number (uppercase 'INF' and 'NAN').
%edoubleScientific notation (lowercase 'e').
%EdoubleScientific notation (uppercase 'E').
%gdoubleUses the shorter of %f or %e.
%GdoubleUses the shorter of %F or %E.
%adoubleHexadecimal floating-point number (lowercase 'p').
%AdoubleHexadecimal floating-point number (uppercase 'P').
%pvoid*Pointer address.
%nint*The number of characters written so far is stored in the integer pointed to by the argument. Nothing is printed.
%%NoneA literal '%' character is printed.