view docs/ccguide/printf.refentry @ 2342:82c9672489a0

Made change to workings of SHARE. bit to what I believe is the proper way.
author boisy
date Fri, 15 Jan 2010 15:54:53 +0000
parents 8a16d38f3d94
children
line wrap: on
line source

<refentry id="printf">

<refnamediv>
<refname>Printf</refname>
<refname>Fprintf</refname>
<refname>Sprintf</refname>
<refpurpose>formatted output</refpurpose>
</refnamediv>

<refsynopsisdiv>

<funcsynopsis>
<funcsynopsisinfo>
#include &lt;stdio.h&gt;
</funcsynopsisinfo>
<funcprototype>
  <funcdef><function>printf</function></funcdef>
  <paramdef>char *<parameter>control</parameter></paramdef>
  <paramdef>...</paramdef>
</funcprototype>

<funcprototype>
  <funcdef><function>fprintf</function></funcdef>
  <paramdef>FILE *<parameter>fp</parameter></paramdef>
  <paramdef>char *<parameter>control</parameter></paramdef>
  <paramdef>...</paramdef>
</funcprototype>

<funcprototype>
  <funcdef><function>sprintf</function></funcdef>
  <paramdef>char *<parameter>string</parameter></paramdef>
  <paramdef>char *<parameter>control</parameter></paramdef>
  <paramdef>...</paramdef>
</funcprototype>
</funcsynopsis>

</refsynopsisdiv>

<refsect1><title>Description</title>
<para>
Thse three functions are used to place numbers and strings on
the output in formatted, human readable form.
</para>
<para>
Fprintf places its output on the file "fp", printf on the
standard output, and sprintf in the buffer pointed to by
"string". NOTE that it is the user's responsibility to ensure
that this buffer is large enough.
</para>
<para>
The "control" string determines the format, type, and number
of the following arguments expected by the function. If the
control does not match the arguments correctly, the results
are unpredictable.
</para>
<para>
The control may contain characters to be copied directly to
the output and/or format specifications. Each format
specification causes the function to take the next successive
argument for output.
</para>
<para>
A format specification consists of a "%" character followed by
(in this order):
</para>

<itemizedlist spacing="compact">
<listitem>
<para>
An optional minus sign ("-") that means left justification
in the field.
</para>
</listitem>
<listitem>
<para>
An optional string of digits indication the field width
required. The field will be at least this wide and may be
wider if the conversion requires it. The field will be
padded on the left unless the above minus sign is present,
in which case it will be padded on the right. The padding
character is, by default, a space, but if the digit string
starts with a zero ("0"), it will be "0".
</para>
</listitem>
<listitem>
<para>
An optional dot (".") and a digit string, the precision,
which for floating point arguments indicates the number
of digits to follow the decimal point on conversion, and
for strings, the maximum number of characters from the
string argument are to be printed.
</para>
</listitem>
<listitem>
<para>
An optional character "l" indicates that the following
"d","x", or "o" is the specification of a long integer
argument. NOTE that in order for the printing of long
integers to take place, the source code must have in it
somewhere the statement pflinit(), which causes routines
to be linked from the library.
</para>
</listitem>
<listitem>
<para>
A conversion character which shows the type of the
argument and the desired conversion. The recognized
conversion characters are:
</para>

<informaltable frame="none">
<tgroup cols="2">
<colspec colwidth="0.5in"/>
<colspec colwidth="3in"/>
<tbody>
<row>
<entry>d,o,x,X</entry>
<entry>The argument is an integer and the
conversion is to decimal, octal, or
hexadecimal, respectively. "X" prints hex
and alpha in upper case.</entry>
</row>
<row>
<entry>u</entry>
<entry>The argument is an integer and the
conversion is to an unsigned decimal in
the range 0 to 65535.</entry>
</row>
<row>
<entry>f</entry>
<entry>The argument is a double, and the form of
the conversion is "[-]nnn.nnn". Where the
digits after the decimal point are
specified as above. If not specified, the
precision defaults to six digits. If the
precision is 0, no decimal point or
following digits are printed.</entry>
</row>
<row>
<entry>e,E</entry>
<entry>The argument is a double and the form of
the conversion is "[-]n.nnne(+or-)nn"; one
digit before the decimal point, and the
precision controls the number following.
"E" prints the "e" in upper case.</entry>
</row>
<row>
<entry>g,G</entry>
<entry>The argument is a double, and either the
"f" format or the "e" format is chosen,
whichever is the shortest. If the "G"
format is used, the "e" is printed in
upper case.</entry>
</row>
</tbody>
</tgroup>
</informaltable>

<para>
NOTE in each of the above double conversions, the last digit is
rounded.
</para>
<para>
ALSO NOTE that in order for the printing of floats or doubles
to take place, the source program <emphasis>must</emphasis> have the statement
pffinit() somewhere.
</para>

<informaltable frame="none">
<tgroup cols="2">
<colspec colwidth="0.5in"/>
<colspec colwidth="3in"/>
<tbody>
<row>
<entry>c</entry>
<entry>The argument as a character.</entry>
</row>
<row>
<entry>s</entry>
<entry>The argument is a pointer to a string.
Characters from the string are printed up
to a null character, or untill the number of
characters indicated by the precision have
been printed. If the precision is 0 or
missing, the characters are not counted.</entry>
</row>
<row>
<entry>%</entry>
<entry>No argument corresponding; "%" is printed.</entry>
</row>
</tbody>
</tgroup>
</informaltable>

</listitem>
</itemizedlist>

</refsect1>

<refsect1><title>See Also</title>
<para>
Kernighan &amp; Ritchie pages 145-147.
<link linkend="putc">putc()</link>,
<link linkend="scanf">scanf()</link>
</para>
</refsect1>

</refentry>