Skip to content

Instantly share code, notes, and snippets.

@netskink
Created February 6, 2016 12:23
Show Gist options
  • Save netskink/3c8c4d41334858936e0c to your computer and use it in GitHub Desktop.
Save netskink/3c8c4d41334858936e0c to your computer and use it in GitHub Desktop.
printf format specifiers
Escape sequences are used to display non-printing and hard-to-print characters. In general, these characters control how text is positioned on the screen, for example, newlines and tabs:
Character
Value
Escape Sequence
alert (beep)
0x07
\a
backslash
0x5C
\\
backspace
0x08
\b
carriage return
0x0D
\r
double quote
0x22
\"
form feed
0x0C
\f
horizontal tab
0x08
\t
newline
0x0A
\n
null character
0x00
\0
single quote
0x27
\'
STX
0x02
\x02
vertical tab
0x0B
\v
question mark
0x3F
\?
The \x02 syntax works for any 2-digit hex value.
When the program is executed, the control string will be displayed exactly as it appears in the program with two exceptions. First, the computer will replace each conversion specification with a value given in the other arguments part of the printf statement. Second, escape sequences will be replaced with special non-printing and hard-to-print characters. To display the contents of a variable we add a % tag into the format string the specifier defines the type as listed in Table 4.12. The floating-point specifiers have been omitted.
%[flags][width][.precision]specifier
Specifier
Output
Example
c
Character
a
d or i
Signed decimal integer
392
ld
Signed 32-bit long decimal integer
1234567890
e
Scientific notation
6.022141e23
E
Scientific notation, capital letter
6.022141E23
f
Floating point
3.14159
o
Unsigned octal
610
s
String of characters
sample
u
Unsigned decimal integer
7235
x
Unsigned hexadecimal integer
7fa
X
Unsigned hexadecimal integer (capital letters)
7FA
%
%% will write % to stdout
%
The tag can also contain flags, width, .precision, and length sub-specifiers. The flags are listed in Table 4.13. If the width is present, it specifies the 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 .precision sub-specifier specifies the minimum number of digits to be written (d, i, o, u, x, X). If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated if the result requires more digits. A precision of 0 means that no character is written for the value 0. For s the .precision is the maximum number of characters to be printed. For c type is .precision has no effect. For floating point .precision is the number of digits after the decimal.
Flags
Description
-
Left-justify within the given field width
+
Forces the result to have a plus or minus sign
(space)
If no sign is going to be written, a blank space is inserted before the value.
#
Used with o, x or X specifiers the value is preceded with 0, 0x or 0X respectively for values different than zero.
0
Left-pads the number with zeroes (0) instead of spaces, where padding is specified (see width sub-specifier).
If successful, printf will return the total number of characters written. On failure, a negative number is returned. The start of a format specifier is signified by a percent sign and the end is signified by one of the letter codes in the previous table. Each format specifier will be replaced by a value from the argument list converted to the specified format. These optional fields typically occur in this order
The pound sign ('#') specifies that the value should be converted to an alternate form. The alternate form for hexadecimal adds the 0x or 0X. The alternate form for octal is a leading zero.
printf("%x", 13); // prints just 'd'
printf("%#x", 13); // prints '0xd'
printf("%X", 13); // prints just 'D'
printf("%#X", 13); // prints '0XD'
printf("%o", 13); // prints just '15'
printf("%#o", 13); // prints '015'
The zero ('0') specifies zero-padding. The converted value is padded on the left with the specified number of zeros minus the number of digits to be printed. This is described in more detail below.
printf("%d", 15); // prints '15'
printf("%4d", 15); // prints ' 15'
printf("%04d", 15); // prints '0015'
printf("%06d", 1234); // prints '001234'
A minus sign ('-') specifies left justification. Without the minus, the format is right justified.
printf("%5d", 123); // prints ' 123' (right justified)
printf("%-5d", 123); // prints '123 ' (left justified)
A space (' ') specifies that a blank should be left before a positive number.
printf("% d", 17); // prints ' 17'
printf("% d", -17); // prints '-17'
The plus sign ('+') specifies that a sign always be placed before the value. The plus sign overrides a space if both are used.
printf("%+d", 12); // prints '+12'
printf("%+d", -2); // prints '-2'
A decimal digit specifies the minimum field width. Using the minus sign makes the format is left justified, otherwise it is right justified. Used with the zero-modifier for numeric conversions, the value is right-padded with zeros to fill the field width.
printf("%4d", 25); // prints ' 25' (right justified)
printf("%-4d", 25); // prints '25 ' (left justified)
printf("%4d", 1234); // prints '1234' (filled up)
printf("%4d", 12345); // prints '12345' (bigger than 3 width)
A precision value in the form of a period ('.'), followed by an optional digit string. If the digit string is omitted, a precision of zero is used. When used with decimal, hexadecimal or octal integers, it specifies the minimum number of digits to print. For floating point output, it specifies the number of digits after the decimal place. For the 's' (string) conversion, it specifies the maximum number of characters of the string to print, which is quite useful to make sure long strings don’t exceed their field width.
printf("%.4d", 7); // prints '0007'
printf("%.4d", 123456); // prints '123456'
printf("%3s", "Jonathan"); // prints 'Jonathan'
printf("%.3s", "Jonathan"); // prints 'Jon'
printf("%3s", "JV"); // prints 'JV '
printf("%.3s", "JV"); // prints 'JV'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment