]> git.saurik.com Git - apple/libc.git/blob - stdio/printf.3
dacf1d4458df490f236fcf636886cb948cda6f4a
[apple/libc.git] / stdio / printf.3
1 .\" Copyright (c) 1990, 1991, 1993
2 .\" The Regents of the University of California. All rights reserved.
3 .\"
4 .\" This code is derived from software contributed to Berkeley by
5 .\" Chris Torek and the American National Standards Committee X3,
6 .\" on Information Processing Systems.
7 .\"
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
10 .\" are met:
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\" notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\" notice, this list of conditions and the following disclaimer in the
15 .\" documentation and/or other materials provided with the distribution.
16 .\" 3. All advertising materials mentioning features or use of this software
17 .\" must display the following acknowledgement:
18 .\" This product includes software developed by the University of
19 .\" California, Berkeley and its contributors.
20 .\" 4. Neither the name of the University nor the names of its contributors
21 .\" may be used to endorse or promote products derived from this software
22 .\" without specific prior written permission.
23 .\"
24 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 .\" SUCH DAMAGE.
35 .\"
36 .\" @(#)printf.3 8.1 (Berkeley) 6/4/93
37 .\" $FreeBSD: src/lib/libc/stdio/printf.3,v 1.17.2.10 2001/12/14 18:33:57 ru Exp $
38 .\"
39 .Dd June 4, 1993
40 .Dt PRINTF 3
41 .Os
42 .Sh NAME
43 .Nm printf , fprintf , sprintf , snprintf , asprintf ,
44 .Nm vprintf , vfprintf, vsprintf , vsnprintf , vasprintf
45 .Nd formatted output conversion
46 .Sh LIBRARY
47 .Lb libc
48 .Sh SYNOPSIS
49 .In stdio.h
50 .Ft int
51 .Fn printf "const char *format" ...
52 .Ft int
53 .Fn fprintf "FILE *stream" "const char *format" ...
54 .Ft int
55 .Fn sprintf "char *str" "const char *format" ...
56 .Ft int
57 .Fn snprintf "char *str" "size_t size" "const char *format" ...
58 .Ft int
59 .Fn asprintf "char **ret" "const char *format" ...
60 .In stdarg.h
61 .Ft int
62 .Fn vprintf "const char *format" "va_list ap"
63 .Ft int
64 .Fn vfprintf "FILE *stream" "const char *format" "va_list ap"
65 .Ft int
66 .Fn vsprintf "char *str" "const char *format" "va_list ap"
67 .Ft int
68 .Fn vsnprintf "char *str" "size_t size" "const char *format" "va_list ap"
69 .Ft int
70 .Fn vasprintf "char **ret" "const char *format" "va_list ap"
71 .Sh DESCRIPTION
72 The
73 .Fn printf
74 family of functions produces output according to a
75 .Fa format
76 as described below.
77 .Fn Printf
78 and
79 .Fn vprintf
80 write output to
81 .Pa stdout ,
82 the standard output stream;
83 .Fn fprintf
84 and
85 .Fn vfprintf
86 write output to the given output
87 .Fa stream ;
88 .Fn sprintf ,
89 .Fn snprintf ,
90 .Fn vsprintf ,
91 and
92 .Fn vsnprintf
93 write to the character string
94 .Fa str ;
95 and
96 .Fn asprintf
97 and
98 .Fn vasprintf
99 dynamically allocate a new string with
100 .Xr malloc 3 .
101 .Pp
102 These functions write the output under the control of a
103 .Fa format
104 string that specifies how subsequent arguments
105 (or arguments accessed via the variable-length argument facilities of
106 .Xr stdarg 3 )
107 are converted for output.
108 .Pp
109 These functions return the number of characters printed
110 (not including the trailing
111 .Ql \e0
112 used to end output to strings),
113 except for
114 .Fn snprintf
115 and
116 .Fn vsnprintf ,
117 which return the number of characters that would have been printed if the
118 .Fa size
119 were unlimited
120 (again, not including the final
121 .Ql \e0 ) .
122 .Pp
123 .Fn Asprintf
124 and
125 .Fn vasprintf
126 set
127 .Fa *ret
128 to be a pointer to a buffer sufficiently large to hold the formatted string.
129 This pointer should be passed to
130 .Xr free 3
131 to release the allocated storage when it is no longer needed.
132 If sufficient space cannot be allocated,
133 .Fn asprintf
134 and
135 .Fn vasprintf
136 will return -1 and set
137 .Fa ret
138 to be a
139 .Dv NULL
140 pointer.
141 .Pp
142 .Fn Snprintf
143 and
144 .Fn vsnprintf
145 will write at most
146 .Fa size Ns \-1
147 of the characters printed into the output string
148 (the
149 .Fa size Ns 'th
150 character then gets the terminating
151 .Ql \e0 ) ;
152 if the return value is greater than or equal to the
153 .Fa size
154 argument, the string was too short
155 and some of the printed characters were discarded.
156 .Pp
157 .Fn Sprintf
158 and
159 .Fn vsprintf
160 effectively assume an infinite
161 .Fa size .
162 .Pp
163 The format string is composed of zero or more directives:
164 ordinary
165 .\" multibyte
166 characters (not
167 .Cm % ) ,
168 which are copied unchanged to the output stream;
169 and conversion specifications, each of which results
170 in fetching zero or more subsequent arguments.
171 Each conversion specification is introduced by
172 the
173 .Cm %
174 character.
175 The arguments must correspond properly (after type promotion)
176 with the conversion specifier.
177 After the
178 .Cm % ,
179 the following appear in sequence:
180 .Bl -bullet
181 .It
182 An optional field, consisting of a decimal digit string followed by a
183 .Cm $ ,
184 specifying the next argument to access.
185 If this field is not provided, the argument following the last
186 argument accessed will be used.
187 Arguments are numbered starting at
188 .Cm 1 .
189 If unaccessed arguments in the format string are interspersed with ones that
190 are accessed the results will be indeterminate.
191 .It
192 Zero or more of the following flags:
193 .Bl -hyphen
194 .It
195 A
196 .Cm #
197 character
198 specifying that the value should be converted to an
199 .Dq alternate form .
200 For
201 .Cm c , d , i , n , p , s ,
202 and
203 .Cm u
204 conversions, this option has no effect.
205 For
206 .Cm o
207 conversions, the precision of the number is increased to force the first
208 character of the output string to a zero (except if a zero value is printed
209 with an explicit precision of zero).
210 For
211 .Cm x
212 and
213 .Cm X
214 conversions, a non-zero result has the string
215 .Ql 0x
216 (or
217 .Ql 0X
218 for
219 .Cm X
220 conversions) prepended to it.
221 For
222 .Cm e , E , f , g ,
223 and
224 .Cm G
225 conversions, the result will always contain a decimal point, even if no
226 digits follow it (normally, a decimal point appears in the results of
227 those conversions only if a digit follows).
228 For
229 .Cm g
230 and
231 .Cm G
232 conversions, trailing zeros are not removed from the result as they
233 would otherwise be.
234 .It
235 A
236 .Cm 0
237 (zero)
238 character specifying zero padding.
239 For all conversions except
240 .Cm n ,
241 the converted value is padded on the left with zeros rather than blanks.
242 If a precision is given with a numeric conversion
243 .Cm ( d , i , o , u , i , x ,
244 and
245 .Cm X ) ,
246 the
247 .Cm 0
248 flag is ignored.
249 .It
250 A negative field width flag
251 .Cm \-
252 indicates the converted value is to be left adjusted on the field boundary.
253 Except for
254 .Cm n
255 conversions, the converted value is padded on the right with blanks,
256 rather than on the left with blanks or zeros.
257 A
258 .Cm \-
259 overrides a
260 .Cm 0
261 if both are given.
262 .It
263 A space, specifying that a blank should be left before a positive number
264 produced by a signed conversion
265 .Cm ( d , e , E , f , g , G ,
266 or
267 .Cm i ) .
268 .It
269 A
270 .Cm +
271 character specifying that a sign always be placed before a
272 number produced by a signed conversion.
273 A
274 .Cm +
275 overrides a space if both are used.
276 .El
277 .It
278 An optional decimal digit string specifying a minimum field width.
279 If the converted value has fewer characters than the field width, it will
280 be padded with spaces on the left (or right, if the left-adjustment
281 flag has been given) to fill out
282 the field width.
283 .It
284 An optional precision, in the form of a period
285 .Cm \&.
286 followed by an
287 optional digit string.
288 If the digit string is omitted, the precision is taken as zero.
289 This gives the minimum number of digits to appear for
290 .Cm d , i , o , u , x ,
291 and
292 .Cm X
293 conversions, the number of digits to appear after the decimal-point for
294 .Cm e , E ,
295 and
296 .Cm f
297 conversions, the maximum number of significant digits for
298 .Cm g
299 and
300 .Cm G
301 conversions, or the maximum number of characters to be printed from a
302 string for
303 .Cm s
304 conversions.
305 .It
306 The optional character
307 .Cm h ,
308 specifying that a following
309 .Cm d , i , o , u , x ,
310 or
311 .Cm X
312 conversion corresponds to a
313 .Vt short int
314 or
315 .Vt unsigned short int
316 argument, or that a following
317 .Cm n
318 conversion corresponds to a pointer to a
319 .Vt short int
320 argument.
321 .It
322 The optional character
323 .Cm l
324 (ell) specifying that a following
325 .Cm d , i , o , u , x ,
326 or
327 .Cm X
328 conversion applies to a pointer to a
329 .Vt long int
330 or
331 .Vt unsigned long int
332 argument, or that a following
333 .Cm n
334 conversion corresponds to a pointer to a
335 .Vt long int
336 argument.
337 .It
338 The optional characters
339 .Cm ll
340 (ell ell) specifying that a following
341 .Cm d , i , o , u , x ,
342 or
343 .Cm X
344 conversion applies to a pointer to a
345 .Vt long long int
346 or
347 .Vt unsigned long long int
348 argument, or that a following
349 .Cm n
350 conversion corresponds to a pointer to a
351 .Vt long long int
352 argument.
353 .It
354 The optional character
355 .Cm q ,
356 specifying that a following
357 .Cm d , i , o , u , x ,
358 or
359 .Cm X
360 conversion corresponds to a
361 .Vt quad int
362 or
363 .Vt unsigned quad int
364 argument, or that a following
365 .Cm n
366 conversion corresponds to a pointer to a
367 .Vt quad int
368 argument.
369 .It
370 The character
371 .Cm L
372 specifying that a following
373 .Cm e , E , f , g ,
374 or
375 .Cm G
376 conversion corresponds to a
377 .Vt long double
378 argument.
379 .It
380 A character that specifies the type of conversion to be applied.
381 .El
382 .Pp
383 A field width or precision, or both, may be indicated by
384 an asterisk
385 .Ql *
386 or an asterisk followed by one or more decimal digits and a
387 .Ql $
388 instead of a
389 digit string.
390 In this case, an
391 .Vt int
392 argument supplies the field width or precision.
393 A negative field width is treated as a left adjustment flag followed by a
394 positive field width; a negative precision is treated as though it were
395 missing.
396 If a single format directive mixes positional (nn$)
397 and non-positional arguments, the results are undefined.
398 .Pp
399 The conversion specifiers and their meanings are:
400 .Bl -tag -width "diouxX"
401 .It Cm diouxX
402 The
403 .Vt int
404 (or appropriate variant) argument is converted to signed decimal
405 .Cm ( d
406 and
407 .Cm i ) ,
408 unsigned octal
409 .Pq Cm o ,
410 unsigned decimal
411 .Pq Cm u ,
412 or unsigned hexadecimal
413 .Cm ( x
414 and
415 .Cm X )
416 notation.
417 The letters
418 .Cm abcdef
419 are used for
420 .Cm x
421 conversions; the letters
422 .Cm ABCDEF
423 are used for
424 .Cm X
425 conversions.
426 The precision, if any, gives the minimum number of digits that must
427 appear; if the converted value requires fewer digits, it is padded on
428 the left with zeros.
429 .It Cm DOU
430 The
431 .Vt long int
432 argument is converted to signed decimal, unsigned octal, or unsigned
433 decimal, as if the format had been
434 .Cm ld , lo ,
435 or
436 .Cm lu
437 respectively.
438 These conversion characters are deprecated, and will eventually disappear.
439 .It Cm eE
440 The
441 .Vt double
442 argument is rounded and converted in the style
443 .Oo \- Oc Ns d Ns Cm \&. Ns ddd Ns Cm e Ns \\*[Pm]dd
444 where there is one digit before the
445 decimal-point character
446 and the number of digits after it is equal to the precision;
447 if the precision is missing,
448 it is taken as 6; if the precision is
449 zero, no decimal-point character appears.
450 An
451 .Cm E
452 conversion uses the letter
453 .Cm E
454 (rather than
455 .Cm e )
456 to introduce the exponent.
457 The exponent always contains at least two digits; if the value is zero,
458 the exponent is 00.
459 .It Cm f
460 The
461 .Vt double
462 argument is rounded and converted to decimal notation in the style
463 .Oo \- Oc Ns ddd Ns Cm \&. Ns ddd ,
464 where the number of digits after the decimal-point character
465 is equal to the precision specification.
466 If the precision is missing, it is taken as 6; if the precision is
467 explicitly zero, no decimal-point character appears.
468 If a decimal point appears, at least one digit appears before it.
469 .It Cm gG
470 The
471 .Vt double
472 argument is converted in style
473 .Cm f
474 or
475 .Cm e
476 (or
477 .Cm E
478 for
479 .Cm G
480 conversions).
481 The precision specifies the number of significant digits.
482 If the precision is missing, 6 digits are given; if the precision is zero,
483 it is treated as 1.
484 Style
485 .Cm e
486 is used if the exponent from its conversion is less than -4 or greater than
487 or equal to the precision.
488 Trailing zeros are removed from the fractional part of the result; a
489 decimal point appears only if it is followed by at least one digit.
490 .It Cm c
491 The
492 .Vt int
493 argument is converted to an
494 .Vt unsigned char ,
495 and the resulting character is written.
496 .It Cm s
497 The
498 .Vt char *
499 argument is expected to be a pointer to an array of character type (pointer
500 to a string).
501 Characters from the array are written up to (but not including)
502 a terminating
503 .Dv NUL
504 character;
505 if a precision is specified, no more than the number specified are
506 written.
507 If a precision is given, no null character
508 need be present; if the precision is not specified, or is greater than
509 the size of the array, the array must contain a terminating
510 .Dv NUL
511 character.
512 .It Cm p
513 The
514 .Vt void *
515 pointer argument is printed in hexadecimal (as if by
516 .Ql %#x
517 or
518 .Ql %#lx ) .
519 .It Cm n
520 The number of characters written so far is stored into the
521 integer indicated by the
522 .Vt int *
523 (or variant) pointer argument.
524 No argument is converted.
525 .It Cm %
526 A
527 .Ql %
528 is written.
529 No argument is converted.
530 The complete conversion specification
531 is
532 .Ql %% .
533 .El
534 .Pp
535 In no case does a non-existent or small field width cause truncation of
536 a field; if the result of a conversion is wider than the field width, the
537 field is expanded to contain the conversion result.
538 .Sh EXAMPLES
539 To print a date and time in the form
540 .Dq Li "Sunday, July 3, 10:02" ,
541 where
542 .Fa weekday
543 and
544 .Fa month
545 are pointers to strings:
546 .Bd -literal -offset indent
547 #include <stdio.h>
548 fprintf(stdout, "%s, %s %d, %.2d:%.2d\en",
549 weekday, month, day, hour, min);
550 .Ed
551 .Pp
552 To print \*(Pi
553 to five decimal places:
554 .Bd -literal -offset indent
555 #include <math.h>
556 #include <stdio.h>
557 fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0));
558 .Ed
559 .Pp
560 To allocate a 128 byte string and print into it:
561 .Bd -literal -offset indent
562 #include <stdio.h>
563 #include <stdlib.h>
564 #include <stdarg.h>
565 char *newfmt(const char *fmt, ...)
566 {
567 char *p;
568 va_list ap;
569 if ((p = malloc(128)) == NULL)
570 return (NULL);
571 va_start(ap, fmt);
572 (void) vsnprintf(p, 128, fmt, ap);
573 va_end(ap);
574 return (p);
575 }
576 .Ed
577 .Sh SEE ALSO
578 .Xr printf 1 ,
579 .Xr scanf 3
580 .Sh STANDARDS
581 The
582 .Fn fprintf ,
583 .Fn printf ,
584 .Fn sprintf ,
585 .Fn vprintf ,
586 .Fn vfprintf ,
587 and
588 .Fn vsprintf
589 functions
590 conform to
591 .St -isoC .
592 .Sh HISTORY
593 The functions
594 .Fn asprintf
595 and
596 .Fn vasprintf
597 first appeared in the
598 .Tn GNU C
599 library.
600 These were implemented by
601 .An Peter Wemm Aq peter@FreeBSD.org
602 in
603 .Fx 2.2 ,
604 but were later replaced with a different implementation
605 from
606 .An Todd C. Miller Aq Todd.Miller@courtesan.com
607 for
608 .Ox 2.3 .
609 .Sh BUGS
610 The conversion formats
611 .Cm \&%D , \&%O ,
612 and
613 .Cm %U
614 are not standard and
615 are provided only for backward compatibility.
616 The effect of padding the
617 .Cm %p
618 format with zeros (either by the
619 .Cm 0
620 flag or by specifying a precision), and the benign effect (i.e., none)
621 of the
622 .Cm #
623 flag on
624 .Cm %n
625 and
626 .Cm %p
627 conversions, as well as other
628 nonsensical combinations such as
629 .Cm %Ld ,
630 are not standard; such combinations
631 should be avoided.
632 .Pp
633 Because
634 .Fn sprintf
635 and
636 .Fn vsprintf
637 assume an infinitely long string,
638 callers must be careful not to overflow the actual space;
639 this is often hard to assure.
640 For safety, programmers should use the
641 .Fn snprintf
642 interface instead.
643 Unfortunately, this interface is not portable.