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