466
|
1 <refentry id="printf">
|
|
2
|
|
3 <refnamediv>
|
603
|
4 <refname>Printf</refname>
|
|
5 <refname>Fprintf</refname>
|
|
6 <refname>Sprintf</refname>
|
466
|
7 <refpurpose>formatted output</refpurpose>
|
|
8 </refnamediv>
|
|
9
|
|
10 <refsynopsisdiv>
|
|
11
|
|
12 <funcsynopsis>
|
|
13 <funcsynopsisinfo>
|
|
14 #include <stdio.h>
|
|
15 </funcsynopsisinfo>
|
|
16 <funcprototype>
|
|
17 <funcdef><function>printf</function></funcdef>
|
|
18 <paramdef>char *<parameter>control</parameter></paramdef>
|
472
|
19 <paramdef>...</paramdef>
|
466
|
20 </funcprototype>
|
|
21
|
|
22 <funcprototype>
|
|
23 <funcdef><function>fprintf</function></funcdef>
|
|
24 <paramdef>FILE *<parameter>fp</parameter></paramdef>
|
|
25 <paramdef>char *<parameter>control</parameter></paramdef>
|
|
26 <paramdef>...</paramdef>
|
|
27 </funcprototype>
|
|
28
|
|
29 <funcprototype>
|
|
30 <funcdef><function>sprintf</function></funcdef>
|
|
31 <paramdef>char *<parameter>string</parameter></paramdef>
|
|
32 <paramdef>char *<parameter>control</parameter></paramdef>
|
|
33 <paramdef>...</paramdef>
|
|
34 </funcprototype>
|
|
35 </funcsynopsis>
|
|
36
|
|
37 </refsynopsisdiv>
|
|
38
|
|
39 <refsect1><title>Description</title>
|
|
40 <para>
|
|
41 Thse three functions are used to place numbers and strings on
|
|
42 the output in formatted, human readable form.
|
|
43 </para>
|
|
44 <para>
|
|
45 Fprintf places its output on the file "fp", printf on the
|
|
46 standard output, and sprintf in the buffer pointed to by
|
|
47 "string". NOTE that it is the user's responsibility to ensure
|
|
48 that this buffer is large enough.
|
|
49 </para>
|
603
|
50 <para>
|
|
51 The "control" string determines the format, type, and number
|
|
52 of the following arguments expected by the function. If the
|
|
53 control does not match the arguments correctly, the results
|
|
54 are unpredictable.
|
|
55 </para>
|
|
56 <para>
|
|
57 The control may contain characters to be copied directly to
|
|
58 the output and/or format specifications. Each format
|
|
59 specification causes the function to take the next successive
|
|
60 argument for output.
|
|
61 </para>
|
|
62 <para>
|
|
63 A format specification consists of a "%" character followed by
|
|
64 (in this order):
|
|
65 </para>
|
|
66
|
|
67 <itemizedlist spacing="compact">
|
|
68 <listitem>
|
|
69 <para>
|
|
70 An optional minus sign ("-") that means left justification
|
|
71 in the field.
|
|
72 </para>
|
|
73 </listitem>
|
|
74 <listitem>
|
|
75 <para>
|
|
76 An optional string of digits indication the field width
|
|
77 required. The field will be at least this wide and may be
|
|
78 wider if the conversion requires it. The field will be
|
|
79 padded on the left unless the above minus sign is present,
|
|
80 in which case it will be padded on the right. The padding
|
|
81 character is, by default, a space, but if the digit string
|
|
82 starts with a zero ("0"), it will be "0".
|
|
83 </para>
|
|
84 </listitem>
|
|
85 <listitem>
|
|
86 <para>
|
|
87 An optional dot (".") and a digit string, the precision,
|
|
88 which for floating point arguments indicates the number
|
|
89 of digits to follow the decimal point on conversion, and
|
|
90 for strings, the maximum number of characters from the
|
|
91 string argument are to be printed.
|
|
92 </para>
|
|
93 </listitem>
|
|
94 <listitem>
|
|
95 <para>
|
|
96 An optional character "l" indicates that the following
|
|
97 "d","x", or "o" is the specification of a long integer
|
|
98 argument. NOTE that in order for the printing of long
|
|
99 integers to take place, the source code must have in it
|
|
100 somewhere the statement pflinit(), which causes routines
|
|
101 to be linked from the library.
|
|
102 </para>
|
|
103 </listitem>
|
|
104 <listitem>
|
|
105 <para>
|
|
106 A conversion character which shows the type of the
|
|
107 argument and the desired conversion. The recognized
|
|
108 conversion characters are:
|
|
109 </para>
|
|
110
|
|
111 <informaltable frame="none">
|
|
112 <tgroup cols="2">
|
640
|
113 <colspec colwidth="0.5in"/>
|
|
114 <colspec colwidth="3in"/>
|
603
|
115 <tbody>
|
|
116 <row>
|
|
117 <entry>d,o,x,X</entry>
|
|
118 <entry>The argument is an integer and the
|
|
119 conversion is to decimal, octal, or
|
|
120 hexadecimal, respectively. "X" prints hex
|
|
121 and alpha in upper case.</entry>
|
|
122 </row>
|
|
123 <row>
|
|
124 <entry>u</entry>
|
|
125 <entry>The argument is an integer and the
|
|
126 conversion is to an unsigned decimal in
|
|
127 the range 0 to 65535.</entry>
|
|
128 </row>
|
|
129 <row>
|
|
130 <entry>f</entry>
|
|
131 <entry>The argument is a double, and the form of
|
|
132 the conversion is "[-]nnn.nnn". Where the
|
|
133 digits after the decimal point are
|
|
134 specified as above. If not specified, the
|
|
135 precision defaults to six digits. If the
|
|
136 precision is 0, no decimal point or
|
|
137 following digits are printed.</entry>
|
|
138 </row>
|
|
139 <row>
|
|
140 <entry>e,E</entry>
|
|
141 <entry>The argument is a double and the form of
|
|
142 the conversion is "[-]n.nnne(+or-)nn"; one
|
|
143 digit before the decimal point, and the
|
|
144 precision controls the number following.
|
|
145 "E" prints the "e" in upper case.</entry>
|
|
146 </row>
|
|
147 <row>
|
|
148 <entry>g,G</entry>
|
|
149 <entry>The argument is a double, and either the
|
|
150 "f" format or the "e" format is chosen,
|
|
151 whichever is the shortest. If the "G"
|
|
152 format is used, the "e" is printed in
|
|
153 upper case.</entry>
|
|
154 </row>
|
|
155 </tbody>
|
|
156 </tgroup>
|
|
157 </informaltable>
|
|
158
|
|
159 <para>
|
|
160 NOTE in each of the above double conversions, the last digit is
|
|
161 rounded.
|
|
162 </para>
|
|
163 <para>
|
|
164 ALSO NOTE that in order for the printing of floats or doubles
|
|
165 to take place, the source program <emphasis>must</emphasis> have the statement
|
|
166 pffinit() somewhere.
|
|
167 </para>
|
|
168
|
|
169 <informaltable frame="none">
|
|
170 <tgroup cols="2">
|
640
|
171 <colspec colwidth="0.5in"/>
|
|
172 <colspec colwidth="3in"/>
|
603
|
173 <tbody>
|
|
174 <row>
|
|
175 <entry>c</entry>
|
|
176 <entry>The argument as a character.</entry>
|
|
177 </row>
|
|
178 <row>
|
|
179 <entry>s</entry>
|
|
180 <entry>The argument is a pointer to a string.
|
|
181 Characters from the string are printed up
|
|
182 to a null character, or untill the number of
|
|
183 characters indicated by the precision have
|
|
184 been printed. If the precision is 0 or
|
|
185 missing, the characters are not counted.</entry>
|
|
186 </row>
|
|
187 <row>
|
|
188 <entry>%</entry>
|
|
189 <entry>No argument corresponding; "%" is printed.</entry>
|
|
190 </row>
|
|
191 </tbody>
|
|
192 </tgroup>
|
|
193 </informaltable>
|
|
194
|
|
195 </listitem>
|
|
196 </itemizedlist>
|
|
197
|
466
|
198 </refsect1>
|
542
|
199
|
|
200 <refsect1><title>See Also</title>
|
|
201 <para>
|
|
202 Kernighan & Ritchie pages 145-147.
|
640
|
203 <link linkend="putc">putc()</link>,
|
542
|
204 <link linkend="scanf">scanf()</link>
|
|
205 </para>
|
|
206 </refsect1>
|
|
207
|
466
|
208 </refentry>
|