]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/printf.3
Many small formatting fixes.
[thirdparty/man-pages.git] / man3 / printf.3
1 .\" Copyright (c) 1999 Andries Brouwer (aeb@cwi.nl)
2 .\"
3 .\" This is free documentation; you can redistribute it and/or
4 .\" modify it under the terms of the GNU General Public License as
5 .\" published by the Free Software Foundation; either version 2 of
6 .\" the License, or (at your option) any later version.
7 .\"
8 .\" The GNU General Public License's references to "object code"
9 .\" and "executables" are to be interpreted as the output of any
10 .\" document formatting or typesetting system, including
11 .\" intermediate and printed output.
12 .\"
13 .\" This manual is distributed in the hope that it will be useful,
14 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
15 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 .\" GNU General Public License for more details.
17 .\"
18 .\" You should have received a copy of the GNU General Public
19 .\" License along with this manual; if not, write to the Free
20 .\" Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111,
21 .\" USA.
22 .\"
23 .\"
24 .\" Earlier versions of this page influenced the present text.
25 .\" It was derived from a Berkeley page with version
26 .\" @(#)printf.3 6.14 (Berkeley) 7/30/91
27 .\" converted for Linux by faith@cs.unc.edu, updated by
28 .\" Helmut.Geyer@iwr.uni-heidelberg.de, agulbra@troll.no and Bruno Haible.
29 .\"
30 .\" 1999-11-25 aeb - Rewritten, using SUSv2 and C99.
31 .\" 2000-07-26 jsm28@hermes.cam.ac.uk - three small fixes
32 .\" 2000-10-16 jsm28@hermes.cam.ac.uk - more fixes
33 .\"
34 .TH PRINTF 3 2008-07-12 "GNU" "Linux Programmer's Manual"
35 .SH NAME
36 printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf,
37 vsnprintf \- formatted output conversion
38 .SH SYNOPSIS
39 .B #include <stdio.h>
40 .sp
41 .BI "int printf(const char *" format ", ...);"
42 .br
43 .BI "int fprintf(FILE *" stream ", const char *" format ", ...);"
44 .br
45 .BI "int sprintf(char *" str ", const char *" format ", ...);"
46 .br
47 .BI "int snprintf(char *" str ", size_t " size ", const char *" format ", ...);"
48 .sp
49 .B #include <stdarg.h>
50 .sp
51 .BI "int vprintf(const char *" format ", va_list " ap );
52 .br
53 .BI "int vfprintf(FILE *" stream ", const char *" format ", va_list " ap );
54 .br
55 .BI "int vsprintf(char *" str ", const char *" format ", va_list " ap );
56 .br
57 .BI "int vsnprintf(char *" str ", size_t " size ", const char *" format \
58 ", va_list " ap );
59 .sp
60 .in -4n
61 Feature Test Macro Requirements for glibc (see
62 .BR feature_test_macros (7)):
63 .in
64 .sp
65 .ad l
66 .BR snprintf (),
67 .BR vsnprintf ():
68 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 || _ISOC99_SOURCE; or
69 .I "cc -std=c99"
70 .ad b
71 .SH DESCRIPTION
72 The functions in the
73 .BR printf ()
74 family produce output according to a
75 .I format
76 as described below.
77 The functions
78 .BR printf ()
79 and
80 .BR vprintf ()
81 write output to
82 .IR stdout ,
83 the standard output stream;
84 .BR fprintf ()
85 and
86 .BR vfprintf ()
87 write output to the given output
88 .IR stream ;
89 .BR sprintf (),
90 .BR snprintf (),
91 .BR vsprintf ()
92 and
93 .BR vsnprintf ()
94 write to the character string
95 .IR str .
96 .PP
97 The functions
98 .BR snprintf ()
99 and
100 .BR vsnprintf ()
101 write at most
102 .I size
103 bytes (including the trailing null byte (\(aq\e0\(aq)) to
104 .IR str .
105 .PP
106 The functions
107 .BR vprintf (),
108 .BR vfprintf (),
109 .BR vsprintf (),
110 .BR vsnprintf ()
111 are equivalent to the functions
112 .BR printf (),
113 .BR fprintf (),
114 .BR sprintf (),
115 .BR snprintf (),
116 respectively, except that they are called with a
117 .I va_list
118 instead of a variable number of arguments.
119 These functions do not call the
120 .I va_end
121 macro.
122 Because they invoke the
123 .I va_arg
124 macro, the value of
125 .I ap
126 is undefined after the call.
127 See
128 .BR stdarg (3).
129 .PP
130 These eight functions write the output under the control of a
131 .I format
132 string that specifies how subsequent arguments (or arguments accessed via
133 the variable-length argument facilities of
134 .BR stdarg (3))
135 are converted for output.
136 .SS "Return value"
137 Upon successful return, these functions return the number of characters
138 printed (not including the
139 trailing \(aq\e0\(aq used to end output to strings).
140
141 The functions
142 .BR snprintf ()
143 and
144 .BR vsnprintf ()
145 do not write more than
146 .I size
147 bytes (including the trailing \(aq\e0\(aq).
148 If the output was truncated due to this limit then the return value
149 is the number of characters (not including the trailing \(aq\e0\(aq)
150 which would have been written to the final string if enough space
151 had been available.
152 Thus, a return value of
153 .I size
154 or more means that the output was truncated.
155 (See also below under NOTES.)
156
157 If an output error is encountered, a negative value is returned.
158 .SS "Format of the format string"
159 The format string is a character string, beginning and ending
160 in its initial shift state, if any.
161 The format string is composed of zero or more directives: ordinary
162 characters (not
163 .BR % ),
164 which are copied unchanged to the output stream;
165 and conversion specifications, each of which results in fetching zero or
166 more subsequent arguments.
167 Each conversion specification is introduced by
168 the character
169 .BR % ,
170 and ends with a
171 .IR "conversion specifier" .
172 In between there may be (in this order) zero or more
173 .IR flags ,
174 an optional minimum
175 .IR "field width" ,
176 an optional
177 .I precision
178 and an optional
179 .IR "length modifier" .
180
181 The arguments must correspond properly (after type promotion) with the
182 conversion specifier.
183 By default, the arguments are used in the order
184 given, where each \(aq*\(aq and each conversion specifier asks for the next
185 argument (and it is an error if insufficiently many arguments are given).
186 One can also specify explicitly which argument is taken,
187 at each place where an argument is required, by writing "%m$" instead
188 of \(aq%\(aq and "*m$" instead of \(aq*\(aq,
189 where the decimal integer m denotes
190 the position in the argument list of the desired argument, indexed starting
191 from 1.
192 Thus,
193 .in +4n
194 .nf
195
196 printf("%*d", width, num);
197
198 .fi
199 .in
200 and
201 .in +4n
202 .nf
203
204 printf("%2$*1$d", width, num);
205
206 .fi
207 .in
208 are equivalent.
209 The second style allows repeated references to the
210 same argument.
211 The C99 standard does not include the style using \(aq$\(aq,
212 which comes from the Single Unix Specification.
213 If the style using
214 \(aq$\(aq is used, it must be used throughout for all conversions taking an
215 argument and all width and precision arguments, but it may be mixed
216 with "%%" formats which do not consume an argument.
217 There may be no
218 gaps in the numbers of arguments specified using \(aq$\(aq; for example, if
219 arguments 1 and 3 are specified, argument 2 must also be specified
220 somewhere in the format string.
221
222 For some numeric conversions a radix character ("decimal point") or
223 thousands' grouping character is used.
224 The actual character used
225 depends on the
226 .B LC_NUMERIC
227 part of the locale.
228 The POSIX locale
229 uses \(aq.\(aq as radix character, and does not have a grouping character.
230 Thus,
231 .in +4n
232 .nf
233
234 printf("%\(aq.2f", 1234567.89);
235
236 .fi
237 .in
238 results in "1234567.89" in the POSIX locale, in "1234567,89" in the
239 nl_NL locale, and in "1.234.567,89" in the da_DK locale.
240 .SS "The flag characters"
241 The character % is followed by zero or more of the following flags:
242 .TP
243 .B #
244 The value should be converted to an "alternate form".
245 For
246 .B o
247 conversions, the first character of the output string is made zero
248 (by prefixing a 0 if it was not zero already).
249 For
250 .B x
251 and
252 .B X
253 conversions, a non-zero result has the string "0x" (or "0X" for
254 .B X
255 conversions) prepended to it.
256 For
257 .BR a ,
258 .BR A ,
259 .BR e ,
260 .BR E ,
261 .BR f ,
262 .BR F ,
263 .BR g ,
264 and
265 .B G
266 conversions, the result will always contain a decimal point, even if no
267 digits follow it (normally, a decimal point appears in the results of those
268 conversions only if a digit follows).
269 For
270 .B g
271 and
272 .B G
273 conversions, trailing zeros are not removed from the result as they would
274 otherwise be.
275 For other conversions, the result is undefined.
276 .TP
277 .B \&0
278 The value should be zero padded.
279 For
280 .BR d ,
281 .BR i ,
282 .BR o ,
283 .BR u ,
284 .BR x ,
285 .BR X ,
286 .BR a ,
287 .BR A ,
288 .BR e ,
289 .BR E ,
290 .BR f ,
291 .BR F ,
292 .BR g ,
293 and
294 .B G
295 conversions, the converted value is padded on the left with zeros rather
296 than blanks.
297 If the
298 .B \&0
299 and
300 .B \-
301 flags both appear, the
302 .B \&0
303 flag is ignored.
304 If a precision is given with a numeric conversion
305 .RB ( d ,
306 .BR i ,
307 .BR o ,
308 .BR u ,
309 .BR x ,
310 and
311 .BR X ),
312 the
313 .B \&0
314 flag is ignored.
315 For other conversions, the behavior is undefined.
316 .TP
317 .B \-
318 The converted value is to be left adjusted on the field boundary.
319 (The default is right justification.)
320 Except for
321 .B n
322 conversions, the converted value is padded on the right with blanks, rather
323 than on the left with blanks or zeros.
324 A
325 .B \-
326 overrides a
327 .B \&0
328 if both are given.
329 .TP
330 .B \(aq \(aq
331 (a space) A blank should be left before a positive number
332 (or empty string) produced by a signed conversion.
333 .TP
334 .B +
335 A sign (+ or \-) should always be placed before a number produced by a signed
336 conversion.
337 By default a sign is used only for negative numbers.
338 A
339 .B +
340 overrides a space if both are used.
341 .PP
342 The five flag characters above are defined in the C standard.
343 The SUSv2 specifies one further flag character.
344 .TP
345 .B \(aq
346 For decimal conversion
347 .RB ( i ,
348 .BR d ,
349 .BR u ,
350 .BR f ,
351 .BR F ,
352 .BR g ,
353 .BR G )
354 the output is to be grouped with thousands' grouping characters
355 if the locale information indicates any.
356 Note that many versions of
357 .BR gcc (1)
358 cannot parse this option and will issue a warning.
359 SUSv2 does not
360 include \fI%\(aqF\fP.
361 .PP
362 glibc 2.2 adds one further flag character.
363 .TP
364 .B I
365 For decimal integer conversion
366 .RB ( i ,
367 .BR d ,
368 .BR u )
369 the output uses the locale's alternative output digits, if any.
370 For example, since glibc 2.2.3 this will give Arabic-Indic digits
371 in the Persian ("fa_IR") locale.
372 .\" outdigits keyword in locale file
373 .SS "The field width"
374 An optional decimal digit string (with non-zero first digit) specifying
375 a minimum field width.
376 If the converted value has fewer characters
377 than the field width, it will be padded with spaces on the left
378 (or right, if the left-adjustment flag has been given).
379 Instead of a decimal digit string one may write "*" or "*m$"
380 (for some decimal integer \fIm\fP) to specify that the field width
381 is given in the next argument, or in the \fIm\fP-th argument, respectively,
382 which must be of type
383 .IR int .
384 A negative field width is taken as a \(aq\-\(aq flag followed by a
385 positive field width.
386 In no case does a nonexistent or small field width cause truncation of a
387 field; if the result of a conversion is wider than the field width, the
388 field is expanded to contain the conversion result.
389 .SS "The precision"
390 An optional precision, in the form of a period (\(aq.\(aq) followed by an
391 optional decimal digit string.
392 Instead of a decimal digit string one may write "*" or "*m$"
393 (for some decimal integer m) to specify that the precision
394 is given in the next argument, or in the m-th argument, respectively,
395 which must be of type
396 .IR int .
397 If the precision is given as just \(aq.\(aq, or the precision is negative,
398 the precision is taken to be zero.
399 This gives the minimum number of digits to appear for
400 .BR d ,
401 .BR i ,
402 .BR o ,
403 .BR u ,
404 .BR x ,
405 and
406 .B X
407 conversions, the number of digits to appear after the radix character for
408 .BR a ,
409 .BR A ,
410 .BR e ,
411 .BR E ,
412 .BR f ,
413 and
414 .B F
415 conversions, the maximum number of significant digits for
416 .B g
417 and
418 .B G
419 conversions, or the maximum number of characters to be printed from a
420 string for
421 .B s
422 and
423 .B S
424 conversions.
425 .SS "The length modifier"
426 Here, "integer conversion" stands for
427 .BR d ,
428 .BR i ,
429 .BR o ,
430 .BR u ,
431 .BR x ,
432 or
433 .B X
434 conversion.
435 .TP
436 .B hh
437 A following integer conversion corresponds to a
438 .I signed char
439 or
440 .I unsigned char
441 argument, or a following
442 .B n
443 conversion corresponds to a pointer to a
444 .I signed char
445 argument.
446 .TP
447 .B h
448 A following integer conversion corresponds to a
449 .I short int
450 or
451 .I unsigned short int
452 argument, or a following
453 .B n
454 conversion corresponds to a pointer to a
455 .I short int
456 argument.
457 .TP
458 .B l
459 (ell) A following integer conversion corresponds to a
460 .I long int
461 or
462 .I unsigned long int
463 argument, or a following
464 .B n
465 conversion corresponds to a pointer to a
466 .I long int
467 argument, or a following
468 .B c
469 conversion corresponds to a
470 .I wint_t
471 argument, or a following
472 .B s
473 conversion corresponds to a pointer to
474 .I wchar_t
475 argument.
476 .TP
477 .B ll
478 (ell-ell).
479 A following integer conversion corresponds to a
480 .I long long int
481 or
482 .I unsigned long long int
483 argument, or a following
484 .B n
485 conversion corresponds to a pointer to a
486 .I long long int
487 argument.
488 .TP
489 .B L
490 A following
491 .BR a ,
492 .BR A ,
493 .BR e ,
494 .BR E ,
495 .BR f ,
496 .BR F ,
497 .BR g ,
498 or
499 .B G
500 conversion corresponds to a
501 .I long double
502 argument.
503 (C99 allows %LF, but SUSv2 does not.)
504 .TP
505 .B q
506 ("quad". 4.4BSD and Linux libc5 only.
507 Don't use.)
508 This is a synonym for
509 .BR ll .
510 .TP
511 .B j
512 A following integer conversion corresponds to an
513 .I intmax_t
514 or
515 .I uintmax_t
516 argument.
517 .TP
518 .B z
519 A following integer conversion corresponds to a
520 .I size_t
521 or
522 .I ssize_t
523 argument.
524 (Linux libc5 has
525 .B Z
526 with this meaning.
527 Don't use it.)
528 .TP
529 .B t
530 A following integer conversion corresponds to a
531 .I ptrdiff_t
532 argument.
533 .PP
534 The SUSv2 only knows about the length modifiers
535 .B h
536 (in
537 .BR hd ,
538 .BR hi ,
539 .BR ho ,
540 .BR hx ,
541 .BR hX ,
542 .BR hn )
543 and
544 .B l
545 (in
546 .BR ld ,
547 .BR li ,
548 .BR lo ,
549 .BR lx ,
550 .BR lX ,
551 .BR ln ,
552 .BR lc ,
553 .BR ls )
554 and
555 .B L
556 (in
557 .BR Le ,
558 .BR LE ,
559 .BR Lf ,
560 .BR Lg ,
561 .BR LG ).
562 .SS "The conversion specifier"
563 A character that specifies the type of conversion to be applied.
564 The conversion specifiers and their meanings are:
565 .TP
566 .BR d ", " i
567 The
568 .I int
569 argument is converted to signed decimal notation.
570 The precision, if any, gives the minimum number of digits
571 that must appear; if the converted value requires fewer digits, it is
572 padded on the left with zeros.
573 The default precision is 1.
574 When 0 is printed with an explicit precision 0, the output is empty.
575 .TP
576 .BR o ", " u ", " x ", " X
577 The
578 .I "unsigned int"
579 argument is converted to unsigned octal
580 .RB ( o ),
581 unsigned decimal
582 .RB ( u ),
583 or unsigned hexadecimal
584 .RB ( x
585 and
586 .BR X )
587 notation.
588 The letters
589 .B abcdef
590 are used for
591 .B x
592 conversions; the letters
593 .B ABCDEF
594 are used for
595 .B X
596 conversions.
597 The precision, if any, gives the minimum number of digits
598 that must appear; if the converted value requires fewer digits, it is
599 padded on the left with zeros.
600 The default precision is 1.
601 When 0 is printed with an explicit precision 0, the output is empty.
602 .TP
603 .BR e ", " E
604 The
605 .I double
606 argument is rounded and converted in the style
607 .if \w'\*(Pm'=0 .ds Pm \(+-
608 .RB [\-]d \&. ddd e \\*(Pmdd
609 where there is one digit before the decimal-point character and the number
610 of digits after it is equal to the precision; if the precision is missing,
611 it is taken as 6; if the precision is zero, no decimal-point character
612 appears.
613 An
614 .B E
615 conversion uses the letter
616 .B E
617 (rather than
618 .BR e )
619 to introduce the exponent.
620 The exponent always contains at least two
621 digits; if the value is zero, the exponent is 00.
622 .TP
623 .BR f ", " F
624 The
625 .I double
626 argument is rounded and converted to decimal notation in the style
627 .RB [\-]ddd \&. ddd,
628 where the number of digits after the decimal-point character is equal to
629 the precision specification.
630 If the precision is missing, it is taken as
631 6; if the precision is explicitly zero, no decimal-point character appears.
632 If a decimal point appears, at least one digit appears before it.
633
634 (The SUSv2 does not know about
635 .B F
636 and says that character string representations for infinity and NaN
637 may be made available.
638 The C99 standard specifies "[\-]inf" or "[\-]infinity"
639 for infinity, and a string starting with "nan" for NaN, in the case of
640 .B f
641 conversion, and "[\-]INF" or "[\-]INFINITY" or "NAN*" in the case of
642 .B F
643 conversion.)
644 .TP
645 .BR g ", " G
646 The
647 .I double
648 argument is converted in style
649 .B f
650 or
651 .B e
652 (or
653 .B F
654 or
655 .B E
656 for
657 .B G
658 conversions).
659 The precision specifies the number of significant digits.
660 If the precision is missing, 6 digits are given; if the precision is zero,
661 it is treated as 1.
662 Style
663 .B e
664 is used if the exponent from its conversion is less than \-4 or greater
665 than or equal to the precision.
666 Trailing zeros are removed from the
667 fractional part of the result; a decimal point appears only if it is
668 followed by at least one digit.
669 .TP
670 .BR a ", " A
671 (C99; not in SUSv2) For
672 .B a
673 conversion, the
674 .I double
675 argument is converted to hexadecimal notation (using the letters abcdef)
676 in the style
677 .RB [\-] 0x h \&. hhhh p \\*(Pmd;
678 for
679 .B A
680 conversion the prefix
681 .BR 0X ,
682 the letters ABCDEF, and the exponent separator
683 .B P
684 is used.
685 There is one hexadecimal digit before the decimal point,
686 and the number of digits after it is equal to the precision.
687 The default precision suffices for an exact representation of the value
688 if an exact representation in base 2 exists
689 and otherwise is sufficiently large to distinguish values of type
690 .IR double .
691 The digit before the decimal point is unspecified for non-normalized
692 numbers, and non-zero but otherwise unspecified for normalized numbers.
693 .TP
694 .B c
695 If no
696 .B l
697 modifier is present, the
698 .I int
699 argument is converted to an
700 .IR "unsigned char" ,
701 and the resulting character is written.
702 If an
703 .B l
704 modifier is present, the
705 .I wint_t
706 (wide character) argument is converted to a multibyte sequence by a call
707 to the
708 .BR wcrtomb (3)
709 function, with a conversion state starting in the initial state, and the
710 resulting multibyte string is written.
711 .TP
712 .B s
713 If no
714 .B l
715 modifier is present: The
716 .I "const char *"
717 argument is expected to be a pointer to an array of character type (pointer
718 to a string).
719 Characters from the array are written up to (but not
720 including) a terminating null byte (\(aq\\0\(aq);
721 if a precision is specified, no more than the number specified
722 are written.
723 If a precision is given, no null byte need be present;
724 if the precision is not specified, or is greater than the size of the
725 array, the array must contain a terminating null byte.
726
727 If an
728 .B l
729 modifier is present: The
730 .I "const wchar_t *"
731 argument is expected to be a pointer to an array of wide characters.
732 Wide characters from the array are converted to multibyte characters
733 (each by a call to the
734 .BR wcrtomb (3)
735 function, with a conversion state starting in the initial state before
736 the first wide character), up to and including a terminating null
737 wide character.
738 The resulting multibyte characters are written up to
739 (but not including) the terminating null byte.
740 If a precision is
741 specified, no more bytes than the number specified are written, but
742 no partial multibyte characters are written.
743 Note that the precision
744 determines the number of
745 .I bytes
746 written, not the number of
747 .I wide characters
748 or
749 .IR "screen positions" .
750 The array must contain a terminating null wide character, unless a
751 precision is given and it is so small that the number of bytes written
752 exceeds it before the end of the array is reached.
753 .TP
754 .B C
755 (Not in C99, but in SUSv2.)
756 Synonym for
757 .BR lc .
758 Don't use.
759 .TP
760 .B S
761 (Not in C99, but in SUSv2.)
762 Synonym for
763 .BR ls .
764 Don't use.
765 .TP
766 .B p
767 The
768 .I "void *"
769 pointer argument is printed in hexadecimal (as if by
770 .B %#x
771 or
772 .BR %#lx ).
773 .TP
774 .B n
775 The number of characters written so far is stored into the integer
776 indicated by the
777 .I "int *"
778 (or variant) pointer argument.
779 No argument is converted.
780 .TP
781 .B m
782 (Glibc extension.)
783 Print output of
784 .IR strerror(errno) .
785 No argument is required.
786 .TP
787 .B %
788 A \(aq%\(aq is written.
789 No argument is converted.
790 The complete conversion
791 specification is \(aq%%\(aq.
792 .SH "CONFORMING TO"
793 The
794 .BR fprintf (),
795 .BR printf (),
796 .BR sprintf (),
797 .BR vprintf (),
798 .BR vfprintf (),
799 and
800 .BR vsprintf ()
801 functions conform to C89 and C99.
802 The
803 .BR snprintf ()
804 and
805 .BR vsnprintf ()
806 functions conform to C99.
807 .PP
808 Concerning the return value of
809 .BR snprintf (),
810 SUSv2 and C99 contradict each other: when
811 .BR snprintf ()
812 is called with
813 .IR size =0
814 then SUSv2 stipulates an unspecified return value less than 1,
815 while C99 allows
816 .I str
817 to be NULL in this case, and gives the return value (as always)
818 as the number of characters that would have been written in case
819 the output string has been large enough.
820 .PP
821 Linux libc4 knows about the five C standard flags.
822 It knows about the length modifiers \fBh\fP, \fBl\fP, \fBL\fP,
823 and the conversions
824 \fBc\fP, \fBd\fP, \fBe\fP, \fBE\fP, \fBf\fP, \fBF\fP,
825 \fBg\fP, \fBG\fP, \fBi\fP, \fBn\fP, \fBo\fP, \fBp\fP,
826 \fBs\fP, \fBu\fP, \fBx\fP, and \fBX\fP,
827 where \fBF\fP is a synonym for \fBf\fP.
828 Additionally, it accepts \fBD\fP, \fBO\fP, and \fBU\fP as synonyms
829 for \fBld\fP, \fBlo\fP, and \fBlu\fP.
830 (This is bad, and caused serious bugs later, when
831 support for \fB%D\fP disappeared.)
832 No locale-dependent radix character,
833 no thousands' separator, no NaN or infinity, no "%m$" and "*m$".
834 .PP
835 Linux libc5 knows about the five C standard flags and the \(aq flag,
836 locale, "%m$" and "*m$".
837 It knows about the length modifiers \fBh\fP, \fBl\fP, \fBL\fP,
838 \fBZ\fP, iand \fBq\fP, but accepts \fBL\fP and \fBq\fP
839 both for \fIlong double\fP and for \fIlong long int\fP (this is a bug).
840 It no longer recognizes \fBF\fP, \fBD\fP, \fBO\fP, and \fBU\fP,
841 but adds the conversion character
842 .BR m ,
843 which outputs
844 .IR strerror(errno) .
845 .PP
846 glibc 2.0 adds conversion characters \fBC\fP and \fBS\fP.
847 .PP
848 glibc 2.1 adds length modifiers \fBhh\fP, \fBj\fP, \fBt\fP, and \fBz\fP
849 and conversion characters \fBa\fP and\fBA\fP.
850 .PP
851 glibc 2.2 adds the conversion character \fBF\fP with C99 semantics,
852 and the flag character \fBI\fP.
853 .SH NOTES
854 The glibc implementation of the functions
855 .BR snprintf ()
856 and
857 .BR vsnprintf ()
858 conforms to the C99 standard, that is, behaves as described above,
859 since glibc version 2.1.
860 Until glibc 2.0.6 they would return \-1
861 when the output was truncated.
862 .\" .SH HISTORY
863 .\" Unix V7 defines the three routines
864 .\" .BR printf (),
865 .\" .BR fprintf (),
866 .\" .BR sprintf (),
867 .\" and has the flag \-, the width or precision *, the length modifier l,
868 .\" and the conversions doxfegcsu, and also D,O,U,X as synonyms for ld,lo,lu,lx.
869 .\" This is still true for 2.9.1BSD, but 2.10BSD has the flags
870 .\" #, + and <space> and no longer mentions D,O,U,X.
871 .\" 2.11BSD has
872 .\" .BR vprintf (),
873 .\" .BR vfprintf (),
874 .\" .BR vsprintf (),
875 .\" and warns not to use D,O,U,X.
876 .\" 4.3BSD Reno has the flag 0, the length modifiers h and L,
877 .\" and the conversions n, p, E, G, X (with current meaning)
878 .\" and deprecates D,O,U.
879 .\" 4.4BSD introduces the functions
880 .\" .BR snprintf ()
881 .\" and
882 .\" .BR vsnprintf (),
883 .\" and the length modifier q.
884 .\" FreeBSD also has functions
885 .\" .BR asprintf ()
886 .\" and
887 .\" .BR vasprintf (),
888 .\" that allocate a buffer large enough for
889 .\" .BR sprintf ().
890 .\" In glibc there are functions
891 .\" .BR dprintf ()
892 .\" and
893 .\" .BR vdprintf ()
894 .\" that print to a file descriptor instead of a stream.
895 .SH BUGS
896 Because
897 .BR sprintf ()
898 and
899 .BR vsprintf ()
900 assume an arbitrarily long string, callers must be careful not to overflow
901 the actual space; this is often impossible to assure.
902 Note that the length
903 of the strings produced is locale-dependent and difficult to predict.
904 Use
905 .BR snprintf ()
906 and
907 .BR vsnprintf ()
908 instead (or
909 .BR asprintf (3)
910 and
911 .BR vasprintf (3)).
912 .PP
913 Linux libc4.[45] does not have a
914 .BR snprintf (),
915 but provides a libbsd that contains an
916 .BR snprintf ()
917 equivalent to
918 .BR sprintf (),
919 that is, one that ignores the
920 .I size
921 argument.
922 Thus, the use of
923 .BR snprintf ()
924 with early libc4 leads to serious security problems.
925 .PP
926 Code such as
927 .BI printf( foo );
928 often indicates a bug, since
929 .I foo
930 may contain a % character.
931 If
932 .I foo
933 comes from untrusted user input, it may contain \fB%n\fP, causing the
934 .BR printf ()
935 call to write to memory and creating a security hole.
936 .\" .PP
937 .\" Some floating point conversions under early libc4
938 .\" caused memory leaks.
939 .SH EXAMPLE
940 .if \w'\*(Pi'=0 .ds Pi pi
941 To print \*(Pi to five decimal places:
942 .in +4n
943 .nf
944
945 #include <math.h>
946 #include <stdio.h>
947 fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0));
948 .fi
949 .in
950 .PP
951 To print a date and time in the form "Sunday, July 3, 10:02",
952 where
953 .I weekday
954 and
955 .I month
956 are pointers to strings:
957 .in +4n
958 .nf
959
960 #include <stdio.h>
961 fprintf(stdout, "%s, %s %d, %.2d:%.2d\en",
962 weekday, month, day, hour, min);
963 .fi
964 .in
965 .PP
966 Many countries use the day-month-year order.
967 Hence, an internationalized version must be able to print
968 the arguments in an order specified by the format:
969 .in +4n
970 .nf
971
972 #include <stdio.h>
973 fprintf(stdout, format,
974 weekday, month, day, hour, min);
975
976 .fi
977 .in
978 where
979 .I format
980 depends on locale, and may permute the arguments.
981 With the value:
982 .in +4n
983 .nf
984
985 "%1$s, %3$d. %2$s, %4$d:%5$.2d\en"
986
987 .fi
988 .in
989 one might obtain "Sonntag, 3. Juli, 10:02".
990 .PP
991 To allocate a sufficiently large string and print into it
992 (code correct for both glibc 2.0 and glibc 2.1):
993 .nf
994
995 #include <stdio.h>
996 #include <stdlib.h>
997 #include <stdarg.h>
998
999 char *
1000 make_message(const char *fmt, ...)
1001 {
1002 /* Guess we need no more than 100 bytes. */
1003 int n, size = 100;
1004 char *p, *np;
1005 va_list ap;
1006
1007 if ((p = malloc(size)) == NULL)
1008 return NULL;
1009
1010 while (1) {
1011 /* Try to print in the allocated space. */
1012 va_start(ap, fmt);
1013 n = vsnprintf(p, size, fmt, ap);
1014 va_end(ap);
1015 /* If that worked, return the string. */
1016 if (n > \-1 && n < size)
1017 return p;
1018 /* Else try again with more space. */
1019 if (n > \-1) /* glibc 2.1 */
1020 size = n+1; /* precisely what is needed */
1021 else /* glibc 2.0 */
1022 size *= 2; /* twice the old size */
1023 if ((np = realloc (p, size)) == NULL) {
1024 free(p);
1025 return NULL;
1026 } else {
1027 p = np;
1028 }
1029 }
1030 }
1031 .fi
1032 .SH "SEE ALSO"
1033 .BR printf (1),
1034 .BR asprintf (3),
1035 .BR dprintf (3),
1036 .BR scanf (3),
1037 .BR setlocale (3),
1038 .BR wcrtomb (3),
1039 .BR wprintf (3),
1040 .BR locale (5)