Message Formatting

The first character of the format parameter is always a percent sign (%). The last character of format is a letter code that determines the type of formatting. One or more format modifiers can appear between the first and last character of the format argument (see below). The possible letter codes are as follows:

Character
Argument Type
Printed As
d,i
int
decimal number
o
int
unsigned octal number without a leading zero
x,X
int
unsigned hexadecimal number without a leading 0x
u
int
unsigned decimal number
c
int
single character
s
char *
print characters until either `\0' or the number of characters given in the precision is reached
e,E
double
engineering notation (m.dddddd e+exponent). If precision is greater than fractional part, prints trailing zeros
g,G
double
engineering notation without trailing zeros or trailing decimal point
%
none
print the character `%'

Note that a separate formatting specification must be provided for each expression to be printed. For example, the statement:

     printf("My name is %s%s\n", "Arthur", "Dent");

prints the statement "My name is Arthur Dent" to standard output. Without the second "%s," the second expression (Dent) would have been ignored.

The output generated by a particular formatting code can be modified. Three types of modifiers can appear between the percent sign (%) and the format code character:

(justification)
A hyphen (-) indicates that the printed output is to be left justified in its field.
field width
A number, by itself or to the left of a decimal point, indicates how many characters the field should be padded. When this number is preceded by a 0, the padding occurs with zeros to the left of the printed value.
precision
A number to the right of a decimal point indicates the maximum width of the printed string or how many digits are printed to the right of the output decimal point.