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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
|
.TH FPRINTF 2
.SH NAME
fprintf, printf, sprintf, snprintf, vfprintf, vprintf, vsprintf, vsnprintf \- print formatted output
.SH SYNOPSIS
.B #include <u.h>
.br
.B #include <stdio.h>
.PP
.B
int fprintf(FILE *f, char *format, ...)
.PP
.B
int printf(char *format, ...)
.PP
.B
int sprintf(char *s, char *format, ...)
.PP
.B
int snprintf(char *s, int n, char *format, ...)
.PP
.B
int vfprintf(FILE *f, char *format, va_list args)
.PP
.B
int vprintf(char *format, va_list args)
.PP
.B
int vsprintf(char *s, char *format, va_list args)
.PP
.B
int vsnprintf(char *s, int n, char *format, va_list args)
.SH DESCRIPTION
.I Fprintf
places output on the named output stream
.I f
(see
.IR fopen (2)).
.I Printf
places output on the standard output stream
.IR stdout .
.I Sprintf
places output
followed by the null character
.RB ( \e0 )
in consecutive bytes starting at
.IR s ;
it is the user's responsibility to ensure that
enough storage is available.
.I Snprintf
is like
.I sprintf
but writes at most
.I n
bytes (including the null character)
into
.IR s .
.IR Vfprintf ,
.IR vprintf ,
.IR vsnprintf ,
and
.I vsprintf
are the same, except the
.I args
argument is the argument list of the
calling function, and the effect is as if the calling function's
argument list from that point on is passed to the
.I printf
routines.
.PP
Each function returns the number of characters
transmitted (not including the
.B \e0
in the case of
.IR sprintf
and friends),
or
a negative value if an output error was encountered.
.PP
These functions
convert, format, and print their
trailing arguments
under control of a
.IR format
string.
The
.I format
contains two types of objects:
plain characters, which are simply copied to the
output stream,
and conversion specifications,
each of which results in fetching of
zero or more
arguments.
The results are undefined if there are arguments of the
wrong type or too few
arguments for the format.
If the format is exhausted while
arguments remain, the excess
are ignored.
.PP
Each conversion specification is introduced by
the character
.BR % .
After the
.BR % ,
the following
appear in sequence:
.PP
.RS
Zero or more
.IR flags ,
which modify the meaning of
the conversion specification.
.PP
An optional decimal digit string specifying a minimum
.IR "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, described later, has been given)
to the field width.
.PP
An optional
.I precision\^
that gives
the minimum number of digits to appear for the
.BR d ,
.BR i ,
.BR o ,
.BR u ,
.BR x ,
and
.B X
conversions,
the number of digits to appear after the
decimal point for the
.BR e ,
.BR E ,
and
.B f
conversions,
the maximum number of significant digits
for the
.B g
and
.B G
conversions,
or the maximum number of characters
to be written from a string in
.B s
conversion.
The precision takes the form of a period
.RB ( \&. )
followed by an optional decimal integer;
if the integer is omitted, it is treated as zero.
.PP
An optional
.B h
specifying that a following
.BR d ,
.BR i ,
.BR o ,
.BR u ,
.BR x
or
.BR X
conversion specifier applies to a
.B short
.B int
or
.B unsigned
.B short
argument (the argument will have been promoted according to the integral
promotions, and its value shall be converted to
.B short
or
.B unsigned
.B short
before printing);
an optional
.B h
specifying that a following
.B n
conversion specifier applies to a pointer to a
.B short
argument;
an optional
.B l
(ell) specifying that a following
.BR d ,
.BR i ,
.BR o ,
.BR u ,
.BR x ,
or
.B X
conversion character applies to a
.B long
or
.B unsigned
.B long
argument;
an optional
.B l
specifying that a following
.B n
conversion specifier applies to a pointer to a
.B long
.B int
argument;
or an optional
.B L
specifying that a following
.BR e ,
.BR E ,
.BR f ,
.BR g ,
or
.B G
conversion specifier applies to a
.B long double
argument.
If an
.BR h ,
.BR l ,
or
.B L
appears with any other conversion specifier, the behavior is undefined.
.PP
A character that indicates the type of
conversion to be applied.
.RE
.PP
A field width or precision, or both, may be
indicated by an asterisk
.RB ( * )
instead of a digit string.
In this case, an
.B int
.I arg\^
supplies
the field width or precision.
The arguments specifying field width or precision, or both,
shall appear (in that order) before the argument (if any) to be converted.
A negative field width argument is taken as a
.B -
flag followed by a positive field width.
A negative precision is taken as if it were missing.
.PP
The flag characters and their meanings are:
.PD 0
.TP 10
.B -
The result of the conversion is left-justified within the field.
.TP
.B +
The result of a signed
conversion always begins with a sign
.RB ( +
or
.BR - ).
.TP
blank
If the first character of a signed conversion is not a sign,
or a signed conversion results in no characters,
a blank
is prefixed to the result.
This implies that if the blank and
.B +
flags both appear, the blank flag is ignored.
.TP
.B #
The result is to be converted
to an ``alternate form.''
For
.B o
conversion, it increases the precision to force
the first digit of the result to be a zero.
For
.B x
or
.B X
conversion, a non-zero result has
.B 0x
or
.B 0X
prefixed to it.
For
.BR e ,
.BR E ,
.BR f ,
.BR g ,
and
.B G
conversions, the result always contains 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
.B g
and
.B G
conversions, trailing zeros are
.I not\^
be removed from the result
as they normally are.
For other conversions, the behavior is undefined.
.TP
.B 0
For
.BR d ,
.BR i ,
.BR o ,
.BR u ,
.BR x ,
.BR X ,
.BR e ,
.BR E ,
.BR f ,
.BR g ,
and
.B G
conversions, leading zeros (following any indication of sign or base)
are used to pad the field width; no space padding is performed.
If the
.B 0
and
.B -
flags both appear, the
.B 0
flag will be ignored.
For
.BR d ,
.BR i ,
.BR o ,
.BR u ,
.BR x ,
and
.B X
conversions, if a precision is specified, the
.B 0
flag will be ignored.
For other conversions, the behavior is undefined.
.PD
.PP
The conversion characters
and their meanings are:
.PP
.PD 0
.TP 10
\fLd\fP,\fLo\fP,\fLu\fP,\fLx\fP,\fLX\fP
The integer
.I arg\^
is converted to signed decimal
.RB ( d
or
.BR i ),
unsigned octal
.RB ( o ),
unsigned decimal
.RB ( u ),
or unsigned hexadecimal notation
.RB ( x
or
.BR X );
the letters
.B abcdef
are used for
.B x
conversion and the letters
.B ABCDEF
for
.B X
conversion.
The precision specifies the minimum number of digits
to appear; if the value being converted can be represented
in fewer digits, it is expanded with leading zeros.
The default precision is 1.
The result of converting a zero value with a precision
of zero is no characters.
.TP
.BR f
The
.B double
argument is converted to decimal notation
in the style
[\-]\fIddd\fL.\fIddd\fR,
where the number of digits after the decimal point
is equal to the precision specification.
If the precision
is missing,
it is taken as 6;
if the precision is explicitly
.LR 0 ,
no decimal point appears.
.TP
.BR e , E
The
.B double
argument is converted in the style
[\-]\fId\fL.\fIddd\fLe\fR±\fIdd\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, it is taken as 6;
if the precision is zero, no decimal point appears.
The
.B E
format code produces a number with
.B E
instead of
.B e
introducing the exponent.
The exponent always contains at least two digits.
.TP
.BR g , G
The
.B double
argument is printed in style
.BR f
or
.BR e
(or in style
.B E
in the case of a
.B G
conversion specifier),
with the precision specifying the number of significant digits.
If an explicit precision is zero, it is taken as 1.
The style used depends on the value converted:
style
.B e
is used only if the exponent resulting from
the conversion is less than \-4
or greater than or equal to the precision.
Trailing zeros are removed from the fractional portion of the result;
a decimal point appears only if it is followed by a digit.
.TP
.B c
The
.B int
argument is converted to an
.B unsigned
.BR char ,
and the resulting character is written.
.TP
.B s
The
argument is taken to be a string (character pointer)
and characters from the string are printed until
a null character
.RB ( \e0 )
is encountered or
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 null character are printed.
A
zero
value for
the argument yields undefined results.
.TP
.B P
The
.B void*
argument is printed in an implementation-defined way (for Plan 9:
the address as hexadecimal number).
.TP
.B n
The argument shall be a pointer to an integer into which is
.I written
the number of characters written to the output stream so far by
this call to
.IR fprintf .
No argument is converted.
.TP
.B %
Print a
.BR % ;
no argument is converted.
.PD
.PP
If a conversion specification is invalid, the behavior is undefined.
.PP
If any argument is, or points to, a union or an aggregate
(except for an array of character type using
.B %s
conversion, or a pointer cast to be a pointer to
.B void
using
.B %P
conversion), the behavior is undefined.
.PP
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 expanded to contain the conversion result.
.SH SOURCE
.B /sys/src/libstdio
.SH SEE ALSO
.IR fopen (2),
.IR fscanf (2),
.IR print (2)
.SH BUGS
There is no way to print a wide character (rune); use
.IR print (2)
or
.IR bio (2).
|