printf, fprintf, sprintf - output formatters
int printf (format [, arg ] ... )
int fprintf (stream, format [ , arg ] ... )
int sprintf (s, format [ , arg ] ... )
char *s, format;
places output on the standard output stream
places output on the named output
places "output", followed by the null character (\0) in consecutive
bytes starting at *
it is the user's responsibility to ensure that enough storage is
available. Each function returns the number of characters transmitted
(not including the \0 for
), or a negative value if an output error was encountered.
Each of these functins converts, formats, and prints its
s under control of the
is a character string that contains two types of objects: plain
characters, which are simply copied to the output stream, and
conversion specifications, each of which results in fetching zero or
s. The results are undefined if there are insufficient
s for the format. If the format is exhausted while
s remain, the excess
s are simply ignored.
Each conversion specification is introduced by the character %. After
the %, the following appear in sequence:
Zero or more
, which modify the meaning of the conversion
An optional decimal digit string specifying a
minimum field width.
If the converted value has fewer characters than the field width, it
will be padded on the left (or right, if the left-adjustment flag (see
below) has been given) to the field witdh;
that gives the minimum number of digits to appear for the
d, o, u, x,
conversions, the number of digits to appear after the decimal point
conversions, the maximum number of significant digits for the
conversion, or the maximum number of character to be
printed from a string in
conversion. The precision takes the form of a period (.) followed by
a decimal digit string: a null digit string is treated as zero.
specifying that a following
d, o, u, x,
conversion character applies to a long integer
A character that indicates the type of conversion to be applied.
A field width or precision may be indicated by an asterisk (*) instead
of a digit string. Here an integer
supplies the field width or precision. The
that is actually converted is not fetched until the conversion letter
is seen, so the
specifying field width or precision must appear before the
(if any) to be converted.
The flag characters and their meanings are:
The result of the conversion will be left-justified within the field.
The result of a signed conversion will always begin with a sign (+ or
If the first character of a signed conversion is not a sign, a
blank will be prepended to the result. This implies that if the blank
and + flags both appear, the blank flag will be ignored.
This flag specifies that the value is to be converted to an "alternate
c, d, s,
conversions, the flag has no effect. For
conversion, it increases the precision to force the first digit of the
result to be zero. For
conversion, a non-zero result will have
0x (0X) prepended to it. For
e, E, f, g,
conversions, the result will always contain a decimal point, even if
no digits follow the point (normally, a decimal point appears in the
result of these conversions only if a digit follows it). For
conversinos, trailing zeroes will not be removed from the result
(which they normally are).
The conversion characters and their meanings are:
is converted to signed decimal, unsigned octal, decimal, or
hexadecimal notation (
), respectively; the letters
conversion and the letters
conversion. The precision specifies the minimum number of digits to
appear; if the value being converted can be represented in fewer
digits, it will be expanded with leading zeroes. The default
precision is 1. The result of converting a zero value with a
precision of zero is a null string (unless the conversion is
flag is present).
The float or double
is converted to decimal notation in the style "[-]ddd.ddd", where the
number of digits after the decimal point is equal to the precision
specification. If the precision is missing, 6 digits are output; if
the precision is explicitly 0, no decimal point appears.
The float or double
is converted in the style "[-]d.ddde+-dd", where there is one digit
before the decimal point and the number of digits after it is equal to
the precision; when the precision is missing, 6 digits are produced;
if the precision is zero, no decimal point appears. The
format code will produce a number with E instead of
introducing the exponent. The exponent always contains exactly two
The float or double
is printed in style
(or in style
format code), with the precision specifying the number of significant
digits. The style used depends on the value converted: style
will be used only if the exponent resulting from the conversion is
less than -4 or greater than the precision. Trailing zeroes are
removed from the result; a decimal point appears only if it is
followed by a digit.
is taken to be a string (character pointer) and characters from the
string are printed until a null character (\0) is encountered or the
number of characters indicated by the precision specification is
reached. If the precision is missing, it is taken to be infinite, so
all characters up to the first null character are printed.
; no argument is converted.
Never does a non-existent or small field width cause truncation of a
field; if the result of the conversion is wider than the field width,
the field is simply expanded to contain the conversion result.
Characters generated by
are printed as if
had been called (see
To print a date and time in the form "Sunday, July 3, 10:02", where
weekday and month are pointers to null-terminated strings:
printf("%s, %s, %d, %.2d:%.2d", weekday, month, day, hour, min);
To print Pi to 5 decimal places:
printf("pi = %.5f", 4*atan(1.0));