summaryrefslogtreecommitdiff
path: root/usr/src/man/man3ucb/printf.3ucb
blob: cf104cdaccf5a65e636fba90dd13aa269822cd78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
'\" te
.\" Copyright (c) 2007, Sun Microsystems, Inc. All Rights Reserved.
.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License. You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.
.\"  See the License for the specific language governing permissions and limitations under the License. When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with
.\" the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
.TH printf 3UCB "30 Oct 2007" "SunOS 5.11" "SunOS/BSD Compatibility Library Functions"
.SH NAME
printf, fprintf, sprintf, vprintf, vfprintf, vsprintf \- formatted output
conversion
.SH SYNOPSIS
.LP
.nf
\fB/usr/ucb/cc\fR [\fIflag\fR ...] \fIfile\fR ...
#include <stdio.h>

\fBint\fR \fBprintf\fR(\fIformat\fR, \fI\&...\fR)
\fBconst char *\fR\fIformat\fR;
.fi

.LP
.nf
\fBint\fR \fBfprintf\fR(\fIstream\fR, \fIformat\fR, \fIva_list\fR)
\fBFILE *\fR\fIstream\fR;
\fBchar *\fR\fIformat\fR;
\fIva_dcl\fR;
.fi

.LP
.nf
\fBchar *\fR\fBsprintf\fR(\fIs\fR, \fIformat\fR, \fIva_list\fR)
\fBchar *\fR\fIs\fR, \fB*\fR\fIformat\fR;
\fIva_dcl\fR;
.fi

.LP
.nf
\fBint\fR \fBvprintf\fR(\fIformat\fR, \fIap\fR)
\fBchar *\fR\fIformat\fR;
\fBva_list\fR \fIap\fR;
.fi

.LP
.nf
\fBint\fR \fBvfprintf\fR(\fIstream\fR, \fIformat\fR, \fIap\fR)
\fBFILE *\fR\fIstream\fR;
\fBchar *\fR\fIformat\fR;
\fBva_list\fR \fIap\fR;
.fi

.LP
.nf
\fBchar *\fR\fBvsprintf\fR(\fIs\fR, \fIformat\fR, \fIap\fR)
\fBchar *\fR\fIs\fR, \fB*\fR\fIformat\fR;
\fBva_list\fR \fIap\fR;
.fi

.SH DESCRIPTION
.sp
.LP
\fBprintf()\fR places output on the standard output stream \fBstdout\fR.
\fBfprintf()\fR places output on the named output \fIstream\fR. \fBsprintf()\fR
places "output," followed by the \fINULL\fR character (\fB\e0\fR), in
consecutive bytes starting at *\fIs\fR; it is the user's responsibility to
ensure that enough storage is available.
.sp
.LP
\fBvprintf()\fR, \fBvfprintf()\fR, and \fBvsprintf()\fR are the same as
\fBprintf()\fR, \fBfprintf()\fR, and \fBsprintf()\fR respectively, except that
instead of being called with a variable number of arguments, they are called
with an argument list as defined by \fB<varargs.h>\fR.
.sp
.LP
Each of these functions converts, formats, and prints its \fIarg\fRs under
control of the \fIformat\fR. The \fIformat\fR 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 zero or more \fIarg\fRs. The results are undefined if there are
insufficient \fIarg\fRs for the format. If the format is exhausted while
\fIarg\fRs remain, the excess \fIarg\fRs are simply ignored.
.sp
.LP
Each conversion specification is introduced by the character \fB%\fR. After the
\fB%\fR, the following appear in sequence:
.RS +4
.TP
.ie t \(bu
.el o
Zero or more \fIflags\fR, which modify the meaning of the conversion
specification.
.RE
.RS +4
.TP
.ie t \(bu
.el o
An optional decimal digit string specifying a minimum \fIfield width\fR. 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 `\fB\(mi\fR\&', described
below, has been given) to the field width. The padding is with blanks unless
the field width digit string starts with a zero, in which case the padding is
with zeros.
.RE
.RS +4
.TP
.ie t \(bu
.el o
A \fIprecision\fR that gives the minimum number of digits to appear for the
\fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or \fBX\fR conversions, the number
of digits to appear after the decimal point for the \fBe\fR, \fBE\fR, and
\fBf\fR conversions, the maximum number of significant digits for the \fBg\fR
and \fBG\fR conversion, or the maximum number of characters to be printed from
a string in \fBs\fR conversion. The precision takes the form of a period
(\fB\&.\fR) followed by a decimal digit string; a \fINULL\fR digit string is
treated as zero. Padding specified by the precision overrides the padding
specified by the field width.
.RE
.RS +4
.TP
.ie t \(bu
.el o
An optional \fBl\fR (ell) specifying that a following \fBd\fR, \fBi\fR,
\fBo\fR, \fBu\fR, \fBx\fR, or \fBX\fR conversion character applies to a long
integer \fIarg\fR. An \fBl\fR before any other conversion character is ignored.
.RE
.RS +4
.TP
.ie t \(bu
.el o
A character that indicates the type of conversion to be applied.
.RE
.sp
.LP
A field width or precision or both may be indicated by an asterisk (\fB*\fR)
instead of a digit string. In this case, an integer \fIarg\fR supplies the
field width or precision. The \fIarg\fR that is actually converted is not
fetched until the conversion letter is seen, so the \fIarg\fRs specifying field
width or precision must appear \fIbefore\fR the \fIarg\fR (if any) to be
converted. A negative field width argument is taken as a `\fB\(mi\fR\&' flag
followed by a positive field width. If the precision argument is negative, it
will be changed to zero.
.sp
.LP
The flag characters and their meanings are:
.sp
.ne 2
.mk
.na
\fB\fB\(mi\fR\fR
.ad
.RS 9n
.rt  
The result of the conversion will be left-justified within the field.
.RE

.sp
.ne 2
.mk
.na
\fB\fB+\fR\fR
.ad
.RS 9n
.rt  
The result of a signed conversion will always begin with a sign (\fB+\fR or
\fB\(mi\fR).
.RE

.sp
.ne 2
.mk
.na
\fBblank\fR
.ad
.RS 9n
.rt  
If the first character of a signed conversion is not a sign, a blank will be
prefixed to the result. This implies that if the blank and \fB+\fR flags both
appear, the blank flag will be ignored.
.RE

.sp
.ne 2
.mk
.na
\fB\fB#\fR\fR
.ad
.RS 9n
.rt  
This flag specifies that the value is to be converted to an "alternate form."
For \fBc\fR, \fBd\fR, \fBi\fR, \fBs\fR, and \fBu\fR conversions, the flag has
no effect. For \fBo\fR conversion, it increases the precision to force the
first digit of the result to be a zero. For \fBx\fR or \fBX\fR conversion, a
non-zero result will have \fB0x\fR or \fB0X\fR prefixed to it. For \fBe\fR,
\fBE\fR, \fBf\fR, \fBg\fR, and \fBG\fR 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 \fBg\fR and \fBG\fR conversions, trailing zeroes will
\fInot\fR be removed from the result (which they normally are).
.RE

.sp
.LP
The conversion characters and their meanings are:
.sp
.ne 2
.mk
.na
\fB\fBd\fR,\fBi\fR,\fBo\fR,\fBu\fR,\fBx\fR,\fBX\fR\fR
.ad
.RS 15n
.rt  
The integer \fIarg\fR is converted to signed decimal (\fBd\fR or \fBi\fR),
unsigned octal (\fBo\fR), unsigned decimal (\fBu\fR), or unsigned hexadecimal
notation (\fBx\fR and \fBX\fR), respectively; the letters \fBabcdef\fR are used
for \fBx\fR conversion and the letters \fBABCDEF\fR for \fBX\fR 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. (For compatibility with older versions, padding with leading zeroes may
alternatively be specified by prepending a zero to the field width. This does
not imply an octal value for the field width.) The default precision is 1. The
result of converting a zero value with a precision of zero is a \fINULL\fR
string.
.RE

.sp
.ne 2
.mk
.na
\fB\fBf\fR\fR
.ad
.RS 15n
.rt  
The float or double \fIarg\fR is converted to decimal notation in the style
[\fB\(mi\fR]\fIddd\fR\fB\&.\fR\fIddd\fR where the number of digits after the
decimal point is equal to the precision specification. If the precision is
missing, 6 digits are given; if the precision is explicitly 0, no digits and no
decimal point are printed.
.RE

.sp
.ne 2
.mk
.na
\fB\fBe\fR,\fBE\fR\fR
.ad
.RS 15n
.rt  
The float or double \fIarg\fR is converted in the style
[\fB\(mi\fR]\fId\fR\fB\&.\fR\fIddd\fR\fBe\(+-\fR\fIddd\fR, 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 \fBE\fR format code will
produce a number with \fBE\fR instead of \fBe\fR introducing the exponent. The
exponent always contains at least two digits.
.RE

.sp
.ne 2
.mk
.na
\fB\fBg\fR,\fBG\fR\fR
.ad
.RS 15n
.rt  
The float or double \fIarg\fR is printed in style \fBf\fR or \fBe\fR (or in
style \fBE\fR in the case of a \fBG\fR format code), with the precision
specifying the number of significant digits. The style used depends on the
value converted: style \fBe\fR or \fBE\fR will be used only if the exponent
resulting from the conversion is less than \(mi4 or greater than the precision.
Trailing zeroes are removed from the result; a decimal point appears only if it
is followed by a digit.
.RE

.sp
.LP
The \fBe\fR, \fBE f\fR, \fBg,\fR and \fBG\fR formats print \fBIEEE\fR
indeterminate values (infinity or not-a-number) as "Infinity" or "NaN"
respectively.
.sp
.ne 2
.mk
.na
\fB\fBc\fR\fR
.ad
.RS 5n
.rt  
The character \fIarg\fR is printed.
.RE

.sp
.ne 2
.mk
.na
\fB\fBs\fR\fR
.ad
.RS 5n
.rt  
The \fIarg\fR is taken to be a string (character pointer) and characters from
the string are printed until a \fINULL\fR character (\fB\e0\fR) is encountered
or until 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 \fINULL\fR character are printed. A \fINULL\fR value
for \fIarg\fR will yield undefined results.
.RE

.sp
.ne 2
.mk
.na
\fB\fB%\fR\fR
.ad
.RS 5n
.rt  
Print a \fB%\fR; no argument is converted.
.RE

.sp
.LP
In no case does a nonexistent or small field width cause truncation of a field;
if the result of a conversion is wider than the field width, the field is
simply expanded to contain the conversion result. Padding takes place only if
the specified field width exceeds the actual width. Characters generated by
\fBprintf()\fR and \fBfprintf()\fR are printed as if \fBputc\fR(3C) had been
called.
.SH RETURN VALUES
.sp
.LP
Upon success, \fBprintf()\fR and \fBfprintf()\fR return the number of
characters transmitted, excluding the null character. \fBvprintf()\fR and
\fBvfprintf()\fR return the number of characters transmitted. \fBsprintf()\fR
and \fBvsprintf()\fR always return \fIs\fR. If an output error is encountered,
\fBprintf()\fR, \fBfprint()\fR, \fBvprintf()\fR, and \fBvfprintf()\fR return
EOF.
.SH EXAMPLES
.LP
\fBExample 1 \fRExamples of the \fBprintf\fR Command To Print a Date and Time
.sp
.LP
To print a date and time in the form "Sunday, July 3, 10:02," where
\fIweekday\fR and \fImonth\fR are pointers to \fINULL\fR-terminated strings:

.sp
.in +2
.nf
printf("%s, %s %i, %d:%.2d", weekday, month, day, hour, min);
.fi
.in -2

.LP
\fBExample 2 \fRExamples of the \fBprintf\fR Command To Print to Five Decimal
Places
.sp
.LP
To print to five decimal places:

.sp
.in +2
.nf
printf("pi \|= \|%.5f", \|4 * atan(1. 0));
.fi
.in -2

.SH SEE ALSO
.sp
.LP
\fBeconvert\fR(3C), \fBputc\fR(3C), \fBscanf\fR(3C), \fBvprintf\fR(3C)
.SH NOTES
.sp
.LP
Use of these interfaces should be restricted to only applications written on
BSD platforms. Use of these interfaces with any of the system libraries or in
multi-thread applications is unsupported.
.sp
.LP
Very wide fields (>128 characters) fail.