NAME
printf, fprintf, sprintf, vprintf, vfprintf, vsprintf -
formatted output conversion
SYNOPSIS
<strong>#include</strong> <strong><stdio.h></strong>
<strong>int</strong> <strong>printf(</strong> <strong>const</strong> <strong>char</strong> <strong>*</strong><em>format</em><strong>,</strong> <strong>...);</strong>
<strong>int</strong> <strong>fprintf(</strong> <strong>FILE</strong> <strong>*</strong><em>stream</em><strong>,</strong> <strong>const</strong> <strong>char</strong> <strong>*</strong><em>format</em><strong>,</strong> <strong>...);</strong>
<strong>int</strong> <strong>sprintf(</strong> <strong>char</strong> <strong>*</strong><em>str</em><strong>,</strong> <strong>const</strong> <strong>char</strong> <strong>*</strong><em>format</em><strong>,</strong> <strong>...);</strong>
<strong>#include</strong> <strong><stdarg.h></strong>
<strong>int</strong> <strong>vprintf(</strong> <strong>const</strong> <strong>char</strong> <strong>*</strong><em>format</em><strong>,</strong> <strong>va_list</strong> <em>ap</em><strong>);</strong>
<strong>int</strong> <strong>vfprintf(</strong> <strong>FILE</strong> <strong>*</strong><em>stream</em><strong>,</strong> <strong>const</strong> <strong>char</strong> <strong>*</strong><em>format</em><strong>,</strong> <strong>va_list</strong>
<em>ap</em><strong>);</strong>
<strong>int</strong> <strong>vsprintf(</strong> <strong>char</strong> <strong>*</strong><em>str</em><strong>,</strong> <strong>char</strong> <strong>*</strong><em>format</em><strong>,</strong> <strong>va_list</strong> <em>ap</em><strong>);</strong>
DESCRIPTION
The <strong>printf</strong> family of functions produces output according
to a <em>format</em> as described below. <strong>Printf</strong> and <strong>vprintf</strong> write
output to <em>stdout</em>, the standard output stream; <strong>fprintf</strong> and
<strong>vfprintf</strong> write output to the given output <em>stream</em>; <strong>sprintf</strong>,
and <strong>vsprintf</strong> write to the character string <em>str</em>. These
functions write the output under the control of a <em>format</em>
string that specifies how subsequent arguments (or argu-
ments accessed via the variable-length argument facilities
of <strong><a href="http://man.he.net/man3/stdarg">stdarg(3)</a></strong> are converted for output. These functions
return the number of characters printed (not including the
trailing ` ' used to end output to strings).
The format string is composed of zero or more directives:
ordinary characters (not <strong>%</strong>), which are copied unchanged to
the output stream; and conversion specifications, each of
which results in fetching zero or more subsequent argu-
ments. Each conversion specification is introduced by the
character <strong>%</strong>. The arguments must correspond properly
(after type promotion) with the conversion specifier.
After the <strong>%</strong>, the following appear in sequence:
<strong>o</strong> Zero or more of the following flags:
<strong>#</strong> specifying that the value should be con-
verted to an ``alternate form''. For <strong>c</strong>, <strong>d</strong>,
<strong>i</strong>, <strong>n</strong>, <strong>p</strong>, <strong>s</strong>, and <strong>u</strong> conversions, this option
has no effect. For <strong>o</strong> conversions, the pre-
cision of the number is increased to force
the first character of the output string to
a zero (except if a zero value is printed
with an explicit precision of zero). For <strong>x</strong>
and <strong>X</strong> conversions, a non-zero result has the
string `0x' (or `0X' for <strong>X</strong> conversions)
prepended to it. For <strong>e</strong>, <strong>E</strong>, <strong>f</strong>, <strong>g</strong>, and <strong>G</strong> con-
versions, the result will always contain a
decimal point, even if no digits follow it
(normally, a decimal point appears in the
results of those conversions only if a digit
follows). For <strong>g</strong> and <strong>G</strong> conversions, trailing
zeros are not removed from the result as
they would otherwise be.
<strong>0</strong> specifying zero padding. For all conver-
sions except <strong>n</strong>, the converted value is
padded on the left with zeros rather than
blanks. If a precision is given with a
numeric conversion (<strong>d</strong>, <strong>i</strong>, <strong>o</strong>, <strong>u</strong>, <strong>i</strong>, <strong>x</strong>, and
<strong>X</strong>), the <strong>0</strong> flag is ignored.
<strong>-</strong> (a negative field width flag) indicates the
converted value is to be left adjusted on
the field boundary. Except for <strong>n</strong> conver-
sions, the converted value is padded on the
right with blanks, rather than on the left
with blanks or zeros. A <strong>-</strong> overrides a <strong>0</strong> if
both are given.
(a space) specifying that a blank should be
left before a positive number produced by a
signed conversion (<strong>d</strong>, <strong>e</strong>, <strong>E</strong>, <strong>f</strong>, <strong>g</strong>, <strong>G</strong>, or <strong>i</strong>).
<strong>+</strong> specifying that a sign always be placed
before a number produced by a signed conver-
sion. A <strong>+</strong> overrides a space if both are
used.
<strong>o</strong> An optional decimal digit string specifying a mini-
mum field width. If the converted value has fewer
characters than the field width, it will be padded
with spaces on the left (or right, if the left-
adjustment flag has been given) to fill out the
field width.
<strong>o</strong> An optional precision, in the form of a period
(`.') followed by an optional digit string. If
the digit string is omitted, the precision is taken
as zero. This gives the minimum number of digits
to appear for <strong>d</strong>, <strong>i</strong>, <strong>o</strong>, <strong>u</strong>, <strong>x</strong>, and <strong>X</strong> conversions, the
number of digits to appear after the decimal-point
for <strong>e</strong>, <strong>E</strong>, and <strong>f</strong> conversions, the maximum number of
significant digits for <strong>g</strong> and <strong>G</strong> conversions, or the
maximum number of characters to be printed from a
string for <strong>s</strong> conversions.
<strong>o</strong> The optional character <strong>h</strong>, specifying that a follow-
ing <strong>d</strong>, <strong>i</strong>, <strong>o</strong>, <strong>u</strong>, <strong>x</strong>, or <strong>X</strong> conversion corresponds to a
<em>short</em> <em>int</em> or <em>unsigned</em> <em>short</em> <em>int</em> argument, or that a
following <strong>n</strong> conversion corresponds to a pointer to
a <em>short</em> <em>int</em> argument.
<strong>o</strong> The optional character <strong>l</strong> (ell) specifying that a
following <strong>d</strong>, <strong>i</strong>, <strong>o</strong>, <strong>u</strong>, <strong>x</strong>, or <strong>X</strong> conversion applies to
a pointer to a <em>long</em> <em>int</em> or <em>unsigned</em> <em>long</em> <em>int</em> argu-
ment, or that a following <strong>n</strong> conversion corresponds
to a pointer to a <em>long</em> <em>int</em> argument.
<strong>o</strong> The character <strong>L</strong> specifying that a following <strong>e</strong>, <strong>E</strong>,
<strong>f</strong>, <strong>g</strong>, or <strong>G</strong> conversion corresponds to a <em>long</em> <em>double</em>
argument.
<strong>o</strong> A character that specifies the type of conversion
to be applied.
A field width or precision, or both, may be indicated by
an asterisk `*' instead of a digit string. In this case,
an <em>int</em> argument supplies the field width or precision. A
negative field width is treated as a left adjustment flag
followed by a positive field width; a negative precision
is treated as though it were missing.
The conversion specifiers and their meanings are:
<strong>diouxX</strong> The <em>int</em> (or appropriate variant) argument is con-
verted to signed decimal (<strong>d</strong> and <strong>i</strong>), unsigned octal
(<strong>o</strong>, unsigned decimal (<strong>u</strong>, or unsigned hexadecimal (<strong>x</strong>
and <strong>X</strong>) notation. The letters <strong>abcdef</strong> are used for <strong>x</strong>
conversions; the letters <strong>ABCDEF</strong> are used for <strong>X</strong> con-
versions. The precision, if any, gives the minimum
number of digits that must appear; if the converted
value requires fewer digits, it is padded on the
left with zeros.
<strong>DOU</strong> The <em>long</em> <em>int</em> argument is converted to signed deci-
mal, unsigned octal, or unsigned decimal, as if the
format had been <strong>ld</strong>, <strong>lo</strong>, or <strong>lu</strong> respectively. These
conversion characters are deprecated, and will
eventually disappear.
<strong>eE</strong> The <em>double</em> argument is rounded and converted in the
style [-]d<strong>.</strong>ddd<strong>e</strong>dd where there is one digit before
the decimal-point character and the number of dig-
its after it is equal to the precision; if the pre-
cision is missing, it is taken as 6; if the preci-
sion is zero, no decimal-point character appears.
An <strong>E</strong> conversion uses the letter <strong>E</strong> (rather than <strong>e</strong>)
to introduce the exponent. The exponent always
contains at least two digits; if the value is zero,
the exponent is 00.
<strong>f</strong> The <em>double</em> argument is rounded and converted to
decimal notation in the style [-]ddd<strong>.</strong>ddd, where the
number of digits after the decimal-point character
is equal to the precision specification. If the
precision is missing, it is taken as 6; if the
precision is explicitly zero, no decimal-point
character appears. If a decimal point appears, at
least one digit appears before it.
<strong>g</strong> The <em>double</em> argument is converted in style <strong>f</strong> or <strong>e</strong>
(or <strong>E</strong> for <strong>G</strong> conversions). The precision specifies
the number of significant digits. If the precision
is missing, 6 digits are given; if the precision is
zero, it is treated as 1. Style <strong>e</strong> is used if the
exponent from its conversion is less than -4 or
greater than or equal to the precision. Trailing
zeros are removed from the fractional part of the
result; a decimal point appears only if it is fol-
lowed by at least one digit.
<strong>c</strong> The <em>int</em> argument is converted to an <em>unsigned</em> <em>char</em>,
and the resulting character is written.
<strong>s</strong> The ``<em>char</em> <em>*</em>'' argument is expected to be a pointer
to an array of character type (pointer to a
string). Characters from the array are written up
to (but not including) a terminating <strong>NUL</strong> character;
if a precision is specified, no more than the num-
ber specified are written. If a precision is
given, no null character need be present; if the
precision is not specified, or is greater than the
size of the array, the array must contain a termi-
nating <strong>NUL</strong> character.
<strong>p</strong> The ``<em>void</em> <em>*</em>'' pointer argument is printed in hex-
adecimal (as if by <strong>%#x</strong> or <strong>%#lx</strong>).
<strong>n</strong> The number of characters written so far is stored
into the integer indicated by the ``<em>int</em> <em>*</em>'' (or
variant) pointer argument. No argument is con-
verted.
<strong>%</strong> A `%' is written. No argument is converted. The
complete conversion specification is `%%'.
In no case does a non-existent or small field width cause
truncation of a field; if the result of a conversion is
wider than the field width, the field is expanded to con-
tain the conversion result.
EXAMPLES
To print a date and time in the form `Sunday, July 3,
10:02', where <em>weekday</em> and <em>month</em> are pointers to strings:
#include <stdio.h>
fprintf(stdout, "%s, %s %d, %.2d:%.2dn",
weekday, month, day, hour, min);
To print to five decimal places:
#include <math.h>
#include <stdio.h>
fprintf(stdout, "pi = %.5fn", 4 * atan(1.0));
To allocate a 128 byte string and print into it:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
char *newfmt(const char *fmt, ...)
{
char *p;
va_list ap;
if ((p = malloc(128)) == NULL)
return (NULL);
va_start(ap, fmt);
(void) vsnprintf(p, 128, fmt, ap);
va_end(ap);
return (p);
}
SEE ALSO
<strong><a href="http://man.he.net/man1/printf">printf(1)</a></strong>, <strong><a href="http://man.he.net/man3/scanf">scanf(3)</a></strong>
STANDARDS
The <strong>fprintf</strong>, <strong>printf</strong>, <strong>sprintf</strong>, <strong>vprintf</strong>, <strong>vfprintf</strong>, and
<strong>vsprintf</strong> functions conform to ANSI C3.159-1989 (``ANSI
C'').
BUGS
Some floating point conversions under Linux cause memory
leaks.
The conversion formats <strong>%D</strong>, <strong>%O</strong>, and <strong>%U</strong> are not standard and
are provided only for backward compatibility. These may
not be provided under Linux.
The effect of padding the <strong>%p</strong> format with zeros (either by
the <strong>0</strong> flag or by specifying a precision), and the benign
effect (i.e., none) of the <strong>#</strong> flag on <strong>%n</strong> and <strong>%p</strong> conver-
sions, as well as other nonsensical combinations such as
<strong>%Ld</strong>, are not standard; such combinations should be
avoided.
Because <strong>sprintf</strong> and <strong>vsprintf</strong> assume an infinitely long
string, callers must be careful not to overflow the actual
space; this is often impossible to assure.
转载请注明:在路上 » printf
Post Views: 6,246