Fandom

Programmer's Wiki

printf

406pages on
this wiki
Add New Page
Talk0 Share

C's printf function provides formatted output to the console.

This function was also ported to other languages, such as Perl.

printf format placeholdersEdit

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 "%[parameter][flags][width][.precision][length]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 %1$#x %1$d",16,17) produces "17 0x10 16"
  • Flags can be zero or more (in any order) of:
Character Description
a number Causes printf to left-pad the output with spaces until the required length of output is attained. If combined with '0' (see below), it will cause the sign to become a space when positive, but the remaining characters will be zero-padded
+ Causes printf to always denote the sign '+' or '-' of a number (the default is to omit the sign for positive numbers). Only applicable to numeric types.
- Causes printf to 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', a 0, 0x, and 0X, respectively, is prepended to non-zero numbers.
0 Causes printf to use 0 instead of spaces to left-fill a fixed-length field. For example, printf("%2d", 3) results in " 3", while printf("%02d", 3) results in "03".
  • Width can be omitted or be any of:
Character Description
a number Causes printf to pad the output of this placeholder with spaces until it is at least number characters wide. As mentioned above, if number has a leading '0', that is interpreted as a flag, and the padding is done with '0' characters instead of spaces.
* Causes printf to pad the output until it is n characters wide, where n is an integer value stored in the a function argument just preceding that represented by the modified type. For example printf("%*d", 5, 10) will result in "10" being printed with a width of 5.
  • Precision can be omitted or be any of:
Character Description
a number For non-integral numeric types, causes the decimal portion of the output to be expressed in at least number digits. For the string type, causes the output to be truncated at number characters. If the precision is zero, nothing is printed for the corresponding argument.
* Same as the above, but uses an integer value in the intaken argument to determine the number of decimal places or maximum string length. For example, printf("%.*s", 3, "abcdef") will result in "abc" being printed.
  • Length can be omitted or be any of:
Character Description
hh For integer types, causes printf to expect an int sized integer argument which was promoted from a char.
h For integer types, causes printf to expect a int sized integer argument which was promoted from a short.
l For integer types, causes printf to expect a long sized integer argument.
ll For integer types, causes printf to expect a long long sized integer argument.
L For floating point types, causes printf to expect a long double argument.
z For integer types, causes printf to expect a size_t sized integer argument.
j For integer types, causes printf to expect a intmax_t sized integer argument.
t For integer types, causes printf to expect a ptrdiff_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 expect ptrdiff_t sized integer argument; for unsigned integer types, causes printf to expect size_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:

Characters Description
"PRId32" Typically equivalent to I32d (Win32/Win64) or d
"PRId64" Typically equivalent to I64d (Win32/Win64), lld (32-bit platforms) or ld (64-bit platforms)
"PRIi32" Typically equivalent to I32i (Win32/Win64) or i
"PRIi64" Typically equivalent to I64i (Win32/Win64), lli (32-bit platforms) or li (64-bit platforms)
"PRIu32" Typically equivalent to I32u (Win32/Win64) or u
"PRIu64" Typically equivalent to I64u (Win32/Win64), llu (32-bit platforms) or lu (64-bit platforms)
  • Type can be any of:
Character Description
d, i Print an int as a signed decimal number. '%d' and '%i' are synonymous for output, but are different when used with scanf() for input.
u Print decimal unsigned int.
f, F Print a 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 Print a double value in standard form ([-]d.ddd e[+/-]ddd).An E conversion uses the letter E (rather than e) to introduce the exponent. The exponent always contains at least two digits; if the value is zero, the exponent is 00.
g, G Print a 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 Print an unsigned int as a hexadecimal number. 'x' uses lower-case letters and 'X' uses upper-case.
o Print an unsigned int in octal.
s Print a character string.
c Print a char (character).
p Print a 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.
% Print a literal '%' character (this type doesn't accept any flags, width, precision or length).

If the syntax of a conversion specification is invalid, behavior remains undefined, and in fact 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.

Note that some compilers, like the GNU Compiler Collection, will statically check the format strings of printf-like functions and warn about problems (specially by using the flags -Wall or -Wformat). The GNU Compiler Collection will also warn about user-defined printf-style functions if the non-standard "format" __attribute__ is applied to the function.

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.