printf()

The printf() function was originally part of C but it's included in C++ as well. You don't even have to #include <iostream> to use it, as it's declared in the <stdio.h> header used by C.

Parameters

  1. A C string that contains the text to be written to stdout

  2. (optional) embedded format specifiers, which will be replaced by the values specified in subsequent additional arguments and formatted as requested.

Format Specifiers

A format specifier follows the prototype as follows: %[flags][width][.precision][length]specifier

SpecifierOutputExample
d / iSigned decimal integer392
uUnsigned decimal integer7235
oUnsigned octal610
xUnsigned hexadecimal integer7fa
XUnsigned hexadecimal integer (uppercase)7FA
fDecimal floating point, lowercase392.65
FDecimal floating point, uppercase392.65
eScientific notation (mantissa/exponent), lowercase3.9265e+2
EScientific notation (mantissa/exponent), uppercase3.9265E+2
gUse the shortest representation: %e or %f392.65
GUse the shortest representation: %E or %F392.65
aHexadecimal floating point, lowercase-0xc.90fep-2
AHexadecimal floating point, uppercase-0XC.90FEP-2
cCharactera
sString of characterssample
pPointer addressb8000000
%A % followed by another % character will write a single % to the stream.%

The format specifier can also contain sub-specifiers:

  1. flags
  2. width
  3. .precision
  4. modifiers
FlagDescription
-Left-justify within the given field width; Right justification is the default (see width sub-specifier)
+Forces to preceed the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a - sign
(space)If no sign is going to be written, a blank space is inserted before the value
#With o, x, or X, precede the outputted value with 0, 0x, or 0X respectively for values different than zero
#With a, A, e, E, f, F, g or G, force the output to contain a decimal point even if no more digits follow
0Left-pads the number with zeroes (0) instead of spaces when padding is specified (see width sub-specifier)

Width

WidthDescription
(number)Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger
*The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted

Precision

FlagDetails
d, i, o, u, x, XSpecifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0
a, A, e, E, f, FSpecifies the number of digits to be printed after the decimal point (default: 6)
g and GSpecifies the maximum number of significant digits to be printed
sSpecifies the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered If the period is specified without an explicit value for precision, 0 is assumed
.*The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted
.A lone . with no number following it is equivalent to inputting .0

Modifiers

lengthd iu o x Xf F e E g G a Acspn
(none)intunsigned intdoubleint
hhsigned charunsigned char
hshort intunsigned short int
llong intunsigned long intwint_t
lllong long intunsigned long long int
jintmax_tuintmax_t
zsize_tsize_t
tptrdiff_tptrdiff_t
Llong double

Additional Arguments

Return Value

Escape Sequences

printf() allows you to specify certain characters by escaping them. The most common example is \n which is used to represent a newline. Here is a table of characters, their escape sequence, and their corresponding hex value. That hex value corresponds with the character's location in the ASCII table.

Escape SequenceHex SequenceCharacter Represented
\a\x07Alert (Bell)
\b\x08Backspace
\e\x1bEscape Character
\f\x0cFormfeed (Page Break)
\n\x0aNewline
\r\x0dCarriage Return
\t\x09Horizontal Tab
\v\x0bVertical Tab
\\\x5cBackslash
\'\x27Single Quote
\"\x22Double Quote
\?\x3fQuestion mark

Argument Index Specifiers

The printf command allows for the reuse of arguments, which can come in handy when you're trying to save space in a command. To use an argument specifier, the must be the very next character that comes after the % character, and must have a $ character appended after it. e.g. %1$s would print the first argument.

printf '%1$s has been moved to %1$s.old\n' file.txt

Format Specifiers

The printf command's real utility is in its format specifiers, which allow you to replace sections of the format string with the argument strings that follow it.

Format SpecifierDescription
%sString value
%bString value, processes escape sequences in the argument string
%cASCII character
%dInteger
%fFloating point number
%gFloating point number w/out trailing zeroes
%eFloating point number in scientific notation
%xHex number
%%Literal %

Examples

C / C++

zsh