OSDN Git Service

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