Previous: 8.2 Formatted Input/Output
Up: 8.2 Formatted Input/Output
Next: 8.2.2 Formatted Input: scanf()
Previous Page: 8.2 Formatted Input/Output
Next Page: 8.2.2 Formatted Input: scanf()
As we have seen, printf() expects arguments giving a format string and values to be printed. The printf() prototype, in stdio.h, is:
int printf(char *, ...);The first argument of printf() is the format string (we will see what the above type declaration means in the next chapter). The number of remaining arguments depends on the number of conversion specifiers in the format string. In C, an ellipsis, i.e. ``...'', is used to indicate an arbitrary number of arguments. The return value of printf() is an int giving the number of bytes output, if successful; otherwise it returns EOF. This information from printf() is not generally very useful, and we often simply ignore the return value.
The function, printf(), converts, formats, and prints its
arguments on the standard output
using the conversion specifications given in the format string. The
format string is made up of two kinds of characters: regular characters,
which are simply copied to the output,
and conversion specification characters.
A conversion specification indicates how the corresponding argument
value is to be converted and formatted before being printed.
The number of conversion specifications in the format string must match exactly
the number of arguments that follow; otherwise, the results are undefined. The
data type of the argument should also match the data type it will
be converted to;
for example, integral types for decimal integer formats,
float or double types
for floating point or exponential formats, and so on.
If the proper type is not used, the conversion is
performed anyway assuming correct data types
and the results can be very strange and unexpected.
Of course, character values are integral types; so characters can
be converted to ASCII integer values for printing, or printed as characters.
We have already seen most of the conversion characters.
Table 8.1 gives a complete list with their meanings.
We will discuss some examples, given the following declarations and initializations:
int i;
char c;
float f1;
double d1;
char *s;
long x;
i = 33;
c = 'e';
f1 = 12345.00
d1 = 12345.00
s = "This is a test";
x = 123456789;
Different
conversion characters may be used to print the values of these variables.
The space used to print a value is called the field, and
by default,
is exactly the space needed to print the value. We show
examples of conversion characters and default output below:

So far, we have used very simple conversion specifiers, such as %d, %f, and %c. A complete conversion specification starts with the character % and ends with a conversion character. Between these two characters, special format characters may be used which can specify justification, field width, field separation, precision, and length modification. The characters that follow the % character and precede the conversion characters are called format characters. All format characters are optional, and if they are absent their default values are assumed. (We will indicate the default value in each case below). The syntax of a complete conversion specifier is:
The first format character is the minus sign. If present, it specifies left justification of the converted argument in its field. The default is right justification, i.e. padding on the left with blanks if the field specified is wider than the converted argument.
The field width is the amount of space, in character positions, used to print the data item. The digits, DD, specify the minimum field width. A converted argument will be printed in a field of at least this size, if it fits into it; otherwise, the field width is made large enough to fit the value. If a converted argument has fewer characters than the field width, by default it will be padded with blanks to the left, unless left justification is specified, in which case, padding is to the right.
A period is used as a separator between the field width and the precision specification.
The digits, dd, specify the precision of the argument. If the argument is a float or double, this specifies the number of digits to the right of the decimal point. If an argument is a string, it specifies the maximum number of characters to be printed from the string.
The length modifier, l, (ell) indicates that an integer type argument is a long rather than an int type.
Conversion Variable OutputField Pos. 01234567890123456789 ___________________________________________________________ %10d i | 33| %-10d i |33 | | | %10f f1 |12345.000000 %-10f f1 |12345.000000 %20.2f f1 | 12345.00| %-20.2f f1 |12345.00 | %5c c | E| %-5c c |E |
%10s s |This is a test %20s s | This is a test| %-20s s |This is a test | %20.10s s | This is a |
%ld x |123456789 %-12ld x |123456789 |