Format Placeholders
Formatting takes place via placeholders within the format string. For example, if a program wanted to print out a person's age, it could present the output by prefixing it with "Your age is ". To denote that we want the integer for the age to be shown immediately after that message, we may use the format string:
"Your age is %d."
The syntax for a format placeholder is
- %type
Parameter can be omitted or can be:
-
Character Description n$
n is the number of the parameter to display using this format specifier, allowing the parameters provided to be output multiple times, using varying format specifiers or in different orders. This is a POSIX extension and not in C99. Example: printf("%2$d %2$#x; %1$d %1$#x",16,17)
produces"
17 0x11; 16 0x10
"
Flags can be zero or more (in any order) of:
-
Character Description +
always denote the sign ' +
' or '-
' of a number (the default is to omit the sign for positive numbers). Only applicable to numeric types.space prefixes non-negative signed numbers with a space -
left-align the output of this placeholder (the default is to right-align the output). #
Alternate form. For ' g
' and 'G
', trailing zeros are not removed. For 'f
', 'F
', 'e
', 'E
', 'g
', 'G
', the output always contains a decimal point. For 'o
', 'x
', and 'X
', a0
,0x
, and0X
, respectively, is prepended to non-zero numbers.0
use 0
instead of spaces to pad a field when the width option is specified. For example,printf("%2d", 3)
results in "3
", whileprintf("%02d", 3)
results in "03
".
Width specifies a minimum number of characters to output, and is typically used to pad fixed-width fields in tabulated output, where the fields would otherwise be smaller, although it does not cause truncation of oversized fields. A leading zero in the width value is interpreted as the zero-padding flag mentioned above, and a negative value is treated as the positive value in conjunction with the left-alignment "-
" flag also mentioned above.
Precision usually specifies a maximum limit on the output, depending on the particular formatting type. For floating point numeric types, it specifies the number of digits to the right of the decimal point that the output should be rounded. For the string type, it limits the number of characters that should be output, after which the string is truncated.
Length can be omitted or be any of:
-
Character Description hh
For integer types, causes printf
to expect anint
-sized integer argument which was promoted from achar
.h
For integer types, causes printf
to expect anint
-sized integer argument which was promoted from ashort
.l
For integer types, causes printf
to expect along
-sized integer argument.ll
For integer types, causes printf
to expect along long
-sized integer argument.L
For floating point types, causes printf
to expect along double
argument.z
For integer types, causes printf
to expect asize_t
-sized integer argument.j
For integer types, causes printf
to expect aintmax_t
-sized integer argument.t
For integer types, causes printf
to expect aptrdiff_t
-sized integer argument.
Additionally, several platform-specific length options came to exist prior to widespread use of the ISO C99 extensions:
-
Characters Description I
For signed integer types, causes printf
to expectptrdiff_t
-sized integer argument; for unsigned integer types, causesprintf
to expectsize_t
-sized integer argument. Commonly found in Win32/Win64 platforms.I32
For integer types, causes printf
to expect a 32-bit (double word) integer argument. Commonly found in Win32/Win64 platforms.I64
For integer types, causes printf
to expect a 64-bit (quad word) integer argument. Commonly found in Win32/Win64 platforms.q
For integer types, causes printf
to expect a 64-bit (quad word) integer argument. Commonly found in BSD platforms.
ISO C99 includes the inttypes.h
header file that includes a number of macros for use in platform-independent printf
coding. Example macros include:
-
Macro Description PRId32
Typically equivalent to I32d
(Win32/Win64) ord
PRId64
Typically equivalent to I64d
(Win32/Win64),lld
(32-bit platforms) orld
(64-bit platforms)PRIi32
Typically equivalent to I32i
(Win32/Win64) ori
PRIi64
Typically equivalent to I64i
(Win32/Win64),lli
(32-bit platforms) orli
(64-bit platforms)PRIu32
Typically equivalent to I32u
(Win32/Win64) oru
PRIu64
Typically equivalent to I64u
(Win32/Win64),llu
(32-bit platforms) orlu
(64-bit platforms)PRIx64
Typically equivalent to I64x
(Win32/Win64),llx
(32-bit platforms) orlx
(64-bit platforms)
Type can be any of:
-
Character Description d
,i
int
as a signed decimal number. '%d
' and '%i
' are synonymous for output, but are different when used withscanf
for input (using%i
will interpret a number as hexadecimal if it's preceded by0x
, and octal if it's preceded by 0.)u
Print decimal unsigned int
.f
,F
double
in normal (fixed-point) notation. 'f
' and 'F
' only differs in how the strings for an infinite number or NaN are printed ('inf
', 'infinity
' and 'nan
' for 'f
', 'INF
', 'INFINITY
' and 'NAN
' for 'F
').e
,E
double
value in standard form (d.ddde
ddd). AnE
conversion uses the letterE
(rather thane
) to introduce the exponent. The exponent always contains at least two digits; if the value is zero, the exponent is00
. In Windows, the exponent contains three digits by default, e.g.1.5e002
, but this can be altered by Microsoft-specific_set_output_format
function.g
,G
double
in either normal or exponential notation, whichever is more appropriate for its magnitude. 'g
' uses lower-case letters, 'G
' uses upper-case letters. This type differs slightly from fixed-point notation in that insignificant zeroes to the right of the decimal point are not included. Also, the decimal point is not included on whole numbers.x
,X
unsigned int
as a hexadecimal number. 'x
' uses lower-case letters and 'X
' uses upper-case.o
unsigned int
in octal.s
null-terminated string. c
char
(character).p
void *
(pointer to void) in an implementation-defined format.n
Print nothing, but write number of characters successfully written so far into an integer pointer parameter. %
a literal ' %
' character (this type doesn't accept any flags, width, precision or length).
The width and precision formatting parameters may be omitted, or they can be a fixed number embedded in the format string, or passed as another function argument when indicated by an asterisk "*
" in the format string. For example printf("%*d", 5, 10)
will result in " 10
" being printed, with a total width of 5 characters, and printf("%.*s", 3, "abcdef")
will result in "abc
" being printed.
If the syntax of a conversion specification is invalid, behavior is undefined, and can cause program termination. If there are too few function arguments provided to supply values for all the conversion specifications in the template string, or if the arguments are not of the correct types, the results are also undefined. Excess arguments are ignored. In a number of cases, the undefined behavior has led to "Format string attack" security vulnerabilities.
Some compilers, like the GNU Compiler Collection, will statically check the format strings of printf-like functions and warn about problems (when using the flags -Wall
or -Wformat
). GCC will also warn about user-defined printf-style functions if the non-standard "format
" __attribute__
is applied to the function.
Read more about this topic: Printf Format String