OSDN Git Service

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