Previous Table of Contents Next


Finally, the overall formatting character determines the type of conversion performed and (subject to the type size modifiers) the expected type of the argument to be printed:

c An int argument is printed as a character. (Under NA1, the size modifier l indicates a wchar_t argument.)
d, i An int (or, with the l modifier, long int) argument is printed as a decimal number.
e, E A float or double (or, with the L modifier, long double) argument is printed in scientific notation. %E indicates that a capital E should be used. If the precision is 0 and the # flag is present, no decimal point is printed. Due to the default argument promotions (see section 3.5.3), no size modifier is needed to distinguish float from double arguments. (Type float has been promoted to double by the time printf sees it.)
f A float or double (or, with the L modifier, long double) argument is printed as a decimal fraction. If the precision is 0 and the # flag is present, no decimal point is printed.
g, G A float or double (or, with the L modifier, long double) argument is printed in the style of %e or %f, whichever gives better precision in less space. %G indicates that a capital E should be used with scientific notation (i.e., %G implies %E or %f). Trailing zeros are suppressed unless the # flag is present.
n Nothing is printed. The corresponding argument is a pointer-to-int; the number of characters written so far is stored in that int.
o An unsigned int (or unsigned long int) argument is printed as an octal number. If the # flag is present and the number is nonzero, a leading 0 is prepended.
p A pointer argument of type void * is printed in an implementation-defined way.
s A string (char *) argument is printed. If a precision is specified, no more than that number of characters from the string is used. (Under NA1, the size modifier l indicates a wchar_t * argument.)
u An unsigned int (or unsigned long int) argument is printed as a decimal number.
x, X An unsigned int (or unsigned long int) argument is printed as a hexadecimal number, using capital ABCDEF for %X. If the # flag is present and the number is nonzero, a leading 0x or 0X is prepended.
% No corresponding argument is expected. A single % is printed.

To demonstrate a few of the formatting possibilities offered by printf, here are some examples:

printf(“%d, %5d, %-5d, %05d, %5.5d\n”, 1, 2, 3, 4, 5);
printf(“%o %x %X %#o %#x\n”, 171, 171, 171, 171, 171);
printf(“%f %e %g\n”, 3.14, 3.14, 3.14);
printf(“%s, %.5s!\n”, “Hello”, “worldly”);
printf(“%0*d, %.*f, %*.*s\n”, 2, 3, 4, 5.6, 7, 3, “abcdef”);

Those calls print these five lines:

1,     2, 3    , 00004, 00005
253 ab AB 0253 0xab
3.140000 3.140000e+00 3.14
Hello, world!
03, 5.6000,     abc

printf and fprintf return the number of characters printed, or a negative number on error.

scanf, fscanf

int scanf(const char *fmt, …)
int fscanf(FILE *fp, const char *fmt, …)

fscanf is roughly the inverse of fprintf; it scans text from the stream fp and attempts to extract values from it under control of the fmt string. The fmt string contains either literal characters that are expected in the input; whitespace characters (space, tab, or newline), any of which indicate that any whitespace should be consumed from the input; or format specifiers that indicate that a value is to be converted and stored in the location pointed to by one of the variable arguments. With the exception of the %c, %n, %[, and %% conversions, all conversions also imply the consumption of leading whitespace.

scanf(fmt, …) is equivalent to fscanf(stdin, fmt, …).

A format specifier consists of the percent sign character (%), followed by optional flags, width, and size modifiers, ending in a character representing the overall conversion to be performed. The complete specification of all allowable scanf format specifiers is extensive; the presentation here is somewhat simplified.

The only flag character is the asterisk (*), which indicates that the converted argument should not be assigned to a corresponding pointed-to value. No corresponding argument is expected.

The field width is a string of digits indicating the maximum number of characters that will be consumed from the input for a field.

The type size modifier is an additional character optionally appearing just before the overall formatting character, as follows:

h The corresponding argument is a pointer to short int or unsigned short int.
l The corresponding argument is a pointer to long int or unsigned long int, or a pointer to double as opposed to float. (See also NA1 note under %c, %s, and %[.)
L The corresponding argument is a pointer to long double.


Previous Table of Contents Next