170 lines
9.5 KiB
HTML
170 lines
9.5 KiB
HTML
<HTML>
|
|
<HEAD>
|
|
<TITLE>printf(3)</TITLE>
|
|
</HEAD>
|
|
<BODY>
|
|
<H1>printf(3)</H1>
|
|
<HR>
|
|
<PRE>
|
|
|
|
</PRE>
|
|
<H2>NAME</H2><PRE>
|
|
printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf,
|
|
vsnprintf - formatted output conversion
|
|
|
|
|
|
</PRE>
|
|
<H2>SYNOPSIS</H2><PRE>
|
|
<STRONG>#include</STRONG> <STRONG><sys/types.h></STRONG>
|
|
<STRONG>#include</STRONG> <STRONG><stdio.h></STRONG>
|
|
<STRONG>#include</STRONG> <STRONG><stdarg.h></STRONG>
|
|
|
|
<STRONG>int</STRONG> <STRONG>printf(const</STRONG> <STRONG>char</STRONG> <STRONG>*</STRONG><EM>format</EM> [<STRONG>,</STRONG> <EM>arg</EM>] ...<STRONG>);</STRONG>
|
|
<STRONG>int</STRONG> <STRONG>fprintf(FILE</STRONG> <STRONG>*</STRONG><EM>stream</EM><STRONG>,</STRONG> <STRONG>const</STRONG> <STRONG>char</STRONG> <STRONG>*</STRONG><EM>format</EM> [<STRONG>,</STRONG> <EM>arg</EM>] ...<STRONG>);</STRONG>
|
|
<STRONG>int</STRONG> <STRONG>sprintf(char</STRONG> <STRONG>*</STRONG><EM>s</EM><STRONG>,</STRONG> <STRONG>const</STRONG> <STRONG>char</STRONG> <STRONG>*</STRONG><EM>format</EM> [<STRONG>,</STRONG> <EM>arg</EM>] ...<STRONG>);</STRONG>
|
|
<STRONG>int</STRONG> <STRONG>snprintf(char</STRONG> <STRONG>*</STRONG><EM>s</EM><STRONG>,</STRONG> <STRONG>size_t</STRONG> <EM>n</EM><STRONG>,</STRONG> <STRONG>const</STRONG> <STRONG>char</STRONG> <STRONG>*</STRONG><EM>format</EM> [<STRONG>,</STRONG> <EM>arg</EM>] ...<STRONG>);</STRONG>
|
|
<STRONG>int</STRONG> <STRONG>vprintf(const</STRONG> <STRONG>char</STRONG> <STRONG>*</STRONG><EM>format</EM><STRONG>,</STRONG> <STRONG>va_list</STRONG> <EM>args</EM><STRONG>);</STRONG>
|
|
<STRONG>int</STRONG> <STRONG>vfprintf(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>args</EM><STRONG>);</STRONG>
|
|
<STRONG>int</STRONG> <STRONG>vsprintf(char</STRONG> <STRONG>*</STRONG><EM>s</EM><STRONG>,</STRONG> <STRONG>const</STRONG> <STRONG>char</STRONG> <STRONG>*</STRONG><EM>format</EM><STRONG>,</STRONG> <STRONG>va_list</STRONG> <EM>args</EM><STRONG>);</STRONG>
|
|
<STRONG>int</STRONG> <STRONG>vsnprintf(char</STRONG> <STRONG>*</STRONG><EM>s</EM><STRONG>,</STRONG> <STRONG>size_t</STRONG> <EM>n</EM><STRONG>,</STRONG> <STRONG>const</STRONG> <STRONG>char</STRONG> <STRONG>*</STRONG><EM>format</EM><STRONG>,</STRONG> <STRONG>va_list</STRONG> <EM>args</EM><STRONG>);</STRONG>
|
|
|
|
|
|
</PRE>
|
|
<H2>DESCRIPTION</H2><PRE>
|
|
<STRONG>Printf</STRONG> places output on the standard output stream <STRONG>stdout</STRONG>. <STRONG>Fprintf</STRONG>
|
|
places output on the named output <EM>stream</EM>. <STRONG>Sprintf</STRONG> places `output' in the
|
|
string <EM>s</EM>, followed by the character `\0'. <STRONG>Snprintf</STRONG> (Minix-vmd only) is
|
|
like <STRONG>sprintf</STRONG> except that no more than <EM>n</EM>-1 characters are written to <EM>s</EM>
|
|
followed by a `\0'.
|
|
|
|
The <STRONG>v*printf</STRONG> functions can be used to make functions like the first four
|
|
by using the <STRONG><A HREF="../man3/stdarg.3.html">stdarg(3)</A></STRONG> method to process the argument.
|
|
|
|
Each of these functions converts, formats, and prints its arguments after
|
|
the first under control of the first argument. The first argument is a
|
|
character string which contains two types of objects: plain characters,
|
|
which are simply copied to the output stream, and conversion
|
|
specifications, each of which causes conversion and printing of the next
|
|
successive <EM>arg</EM>.
|
|
|
|
Each conversion specification is introduced by the character <STRONG>%</STRONG>. The
|
|
remainder of the conversion specification includes in the following order
|
|
|
|
<STRONG>o</STRONG> Zero or more of following flags:
|
|
|
|
<STRONG>o</STRONG> a `#' character specifying that the value should be converted to an
|
|
``alternate form''. For <STRONG>c</STRONG>, <STRONG>d</STRONG>, <STRONG>s</STRONG>, and <STRONG>u</STRONG> conversions, this option has
|
|
no effect. For <STRONG>o</STRONG> conversions, the precision of the number is
|
|
increased to force the first character of the output string to a
|
|
zero. For <STRONG>x</STRONG>(<STRONG>X</STRONG>) conversion, a non-zero result has the string <STRONG><A HREF="../man0/0x.0X.html">0x(0X)</A></STRONG>
|
|
prepended to it. For <STRONG>e</STRONG>, <STRONG>E</STRONG>, <STRONG>f</STRONG>, <STRONG>g</STRONG>, and <STRONG>G</STRONG> conversions, the result will
|
|
always contain a decimal point, even if no digits follow the point
|
|
(normally, a decimal point only appears in the results of those
|
|
conversions if a digit follows the decimal point). For <STRONG>g</STRONG> and <STRONG>G</STRONG>
|
|
conversions, trailing zeros are not removed from the result as they
|
|
would otherwise be.
|
|
|
|
<STRONG>o</STRONG> a minus sign `-' which specifies <EM>left</EM> <EM>adjustment</EM> of the converted
|
|
value in the indicated field;
|
|
|
|
<STRONG>o</STRONG> a `+' character specifying that there should always be a sign placed
|
|
before the number when using signed conversions.
|
|
|
|
<STRONG>o</STRONG> a space specifying that a blank should be left before a positive
|
|
number during a signed conversion. A `+' overrides a space if both
|
|
are used.
|
|
|
|
<STRONG>o</STRONG> an optional digit string specifying a <EM>field</EM> <EM>width</EM>; if the converted
|
|
value has fewer characters than the field width it will be blank-
|
|
padded on the left (or right, if the left-adjustment indicator has
|
|
been given) to make up the field width; if the field width begins
|
|
with a zero, zero-padding will be done instead of blank-padding;
|
|
|
|
<STRONG>o</STRONG> an optional period `<STRONG>.</STRONG>' which serves to separate the field width
|
|
from the next digit string;
|
|
|
|
<STRONG>o</STRONG> an optional digit string specifying a <EM>precision</EM> which specifies the
|
|
number of digits to appear after the decimal point, for e- and f-
|
|
conversion, or the maximum number of characters to be printed from a
|
|
string;
|
|
|
|
<STRONG>o</STRONG> the character <STRONG>l</STRONG> specifying that a following <STRONG>d</STRONG>, <STRONG>o</STRONG>, <STRONG>x</STRONG>, or <STRONG>u</STRONG>
|
|
corresponds to a long integer <EM>arg</EM>.
|
|
|
|
<STRONG>o</STRONG> a character which indicates the type of conversion to be applied.
|
|
|
|
A field width or precision may be `*' instead of a digit string. In this
|
|
case an integer <EM>arg</EM> supplies the field width or precision.
|
|
|
|
The conversion characters and their meanings are
|
|
|
|
<STRONG>dox</STRONG> The integer <EM>arg</EM> is converted to decimal, octal, or hexadecimal
|
|
notation respectively.
|
|
|
|
<STRONG>X</STRONG> Like <STRONG>x</STRONG>, but use upper case instead of lower case.
|
|
|
|
<STRONG>f</STRONG> The float or double <EM>arg</EM> is converted to decimal notation in the
|
|
style `[<STRONG>-</STRONG>]ddd.ddd' where the number of d's after the decimal point
|
|
is equal to the precision specification for the argument. If the
|
|
precision is missing, 6 digits are given; if the precision is
|
|
explicitly 0, no digits and no decimal point are printed.
|
|
|
|
<STRONG>e</STRONG> The float or double <EM>arg</EM> is converted in the style `[<STRONG>-</STRONG>]d<STRONG>.</STRONG>ddd<STRONG>e</STRONG><EM>+</EM>dd'
|
|
where there is one digit before the decimal point and the number
|
|
after is equal to the precision specification for the argument; when
|
|
the precision is missing, 6 digits are produced.
|
|
|
|
<STRONG>g</STRONG> The float or double <EM>arg</EM> is printed in style <STRONG>d</STRONG>, in style <STRONG>f</STRONG>, or in
|
|
style <STRONG>e</STRONG>, whichever gives full precision in minimum space.
|
|
|
|
<STRONG>c</STRONG> The character <EM>arg</EM> is printed.
|
|
|
|
<STRONG>s</STRONG> <EM>Arg</EM> is taken to be a string (character pointer) and characters from
|
|
the string are printed until a null character or until the number of
|
|
characters indicated by the precision specification is reached;
|
|
however if the precision is 0 or missing all characters up to a null
|
|
are printed.
|
|
|
|
<STRONG>u</STRONG> The unsigned integer <EM>arg</EM> is converted to decimal and printed.
|
|
|
|
<STRONG>%</STRONG> Print a `%'; no argument is converted.
|
|
|
|
In no case does a non-existent or small field width cause truncation of a
|
|
field; padding takes place only if the specified field width exceeds the
|
|
actual width. Characters generated by <STRONG>printf</STRONG> are printed by <STRONG><A HREF="../man3/putc.3.html">putc(3)</A></STRONG>.
|
|
|
|
<STRONG>Examples</STRONG>
|
|
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 null-terminated strings:
|
|
|
|
printf("%s, %s %d, %02d:%02d", weekday, month, day, hour, min);
|
|
|
|
To print pi to 5 decimals:
|
|
|
|
printf("pi = %.5f", 4*atan(1.0));
|
|
|
|
|
|
</PRE>
|
|
<H2>SEE ALSO</H2><PRE>
|
|
<STRONG><A HREF="../man3/putc.3.html">putc(3)</A></STRONG>, <STRONG><A HREF="../man3/scanf.3.html">scanf(3)</A></STRONG>, <STRONG><A HREF="../man3/ecvt.3.html">ecvt(3)</A></STRONG>, <STRONG><A HREF="../man3/stdarg.3.html">stdarg(3)</A></STRONG>.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</PRE>
|
|
</BODY>
|
|
</HTML>
|