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