OSDN Git Service

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