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

Specifier Output Example
d / i Signed decimal integer 392
u Unsigned decimal integer 7235
o Unsigned octal 610
x Unsigned hexadecimal integer 7fa
X Unsigned hexadecimal integer (uppercase) 7FA
f Decimal floating point, lowercase 392.65
F Decimal floating point, uppercase 392.65
e Scientific notation (mantissa/exponent), lowercase 3.9265e+2
E Scientific notation (mantissa/exponent), uppercase 3.9265E+2
g Use the shortest representation: %e or %f 392.65
G Use the shortest representation: %E or %F 392.65
a Hexadecimal floating point, lowercase -0xc.90fep-2
A Hexadecimal floating point, uppercase -0XC.90FEP-2
c Character a
s String of characters sample
p Pointer address b8000000
% 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
Flag Description
- 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
0 Left-pads the number with zeroes (0) instead of spaces when padding is specified (see width sub-specifier)

Width

Width Description
(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

Flag Details
d, i, o, u, x, X Specifies 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, F Specifies the number of digits to be printed after the decimal point (default: 6)
g and G Specifies the maximum number of significant digits to be printed
s Specifies 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

length d i u o x X f F e E g G a A c s p n
(none) int unsigned int double int
hh signed char unsigned char
h short int unsigned short int
l long int unsigned long int wint_t
ll long long int unsigned long long int
j intmax_t uintmax_t
z size_t size_t
t ptrdiff_t ptrdiff_t
L long 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 Sequence Hex Sequence Character Represented
\a \x07 Alert (Bell)
\b \x08 Backspace
\e \x1b Escape Character
\f \x0c Formfeed (Page Break)
\n \x0a Newline
\r \x0d Carriage Return
\t \x09 Horizontal Tab
\v \x0b Vertical Tab
\\ \x5c Backslash
\' \x27 Single Quote
\" \x22 Double Quote
\? \x3f Question 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 Specifier Description
%s String value
%b String value, processes escape sequences in the argument string
%c ASCII character
%d Integer
%f Floating point number
%g Floating point number w/out trailing zeroes
%e Floating point number in scientific notation
%x Hex number
%% Literal %

Examples

C / C++

zsh