OSDN Git Service

(split) LDP: Update original to LDP v3.65
[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-12-30 "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, the precision is taken to
413 be zero.
414 A negative precision is taken as if the precision were omitted.
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 knows about only 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 .RB [\-]d \&. ddd e \(+-dd
624 where there is one digit before the decimal-point character and the number
625 of digits after it is equal to the precision; if the precision is missing,
626 it is taken as 6; if the precision is zero, no decimal-point character
627 appears.
628 An
629 .B E
630 conversion uses the letter
631 .B E
632 (rather than
633 .BR e )
634 to introduce the exponent.
635 The exponent always contains at least two
636 digits; if the value is zero, the exponent is 00.
637 .TP
638 .BR f ", " F
639 The
640 .I double
641 argument is rounded and converted to decimal notation in the style
642 .RB [\-]ddd \&. ddd,
643 where the number of digits after the decimal-point character is equal to
644 the precision specification.
645 If the precision is missing, it is taken as
646 6; if the precision is explicitly zero, no decimal-point character appears.
647 If a decimal point appears, at least one digit appears before it.
648
649 (The SUSv2 does not know about
650 .B F
651 and says that character string representations for infinity and NaN
652 may be made available.
653 The C99 standard specifies "[\-]inf" or "[\-]infinity"
654 for infinity, and a string starting with "nan" for NaN, in the case of
655 .B f
656 conversion, and "[\-]INF" or "[\-]INFINITY" or "NAN*" in the case of
657 .B F
658 conversion.)
659 .TP
660 .BR g ", " G
661 The
662 .I double
663 argument is converted in style
664 .B f
665 or
666 .B e
667 (or
668 .B F
669 or
670 .B E
671 for
672 .B G
673 conversions).
674 The precision specifies the number of significant digits.
675 If the precision is missing, 6 digits are given; if the precision is zero,
676 it is treated as 1.
677 Style
678 .B e
679 is used if the exponent from its conversion is less than \-4 or greater
680 than or equal to the precision.
681 Trailing zeros are removed from the
682 fractional part of the result; a decimal point appears only if it is
683 followed by at least one digit.
684 .TP
685 .BR a ", " A
686 (C99; not in SUSv2) For
687 .B a
688 conversion, the
689 .I double
690 argument is converted to hexadecimal notation (using the letters abcdef)
691 in the style
692 .RB [\-] 0x h \&. hhhh p \(+-;
693 for
694 .B A
695 conversion the prefix
696 .BR 0X ,
697 the letters ABCDEF, and the exponent separator
698 .B P
699 is used.
700 There is one hexadecimal digit before the decimal point,
701 and the number of digits after it is equal to the precision.
702 The default precision suffices for an exact representation of the value
703 if an exact representation in base 2 exists
704 and otherwise is sufficiently large to distinguish values of type
705 .IR double .
706 The digit before the decimal point is unspecified for nonnormalized
707 numbers, and nonzero but otherwise unspecified for normalized numbers.
708 .TP
709 .B c
710 If no
711 .B l
712 modifier is present, the
713 .I int
714 argument is converted to an
715 .IR "unsigned char" ,
716 and the resulting character is written.
717 If an
718 .B l
719 modifier is present, the
720 .I wint_t
721 (wide character) argument is converted to a multibyte sequence by a call
722 to the
723 .BR wcrtomb (3)
724 function, with a conversion state starting in the initial state, and the
725 resulting multibyte string is written.
726 .TP
727 .B s
728 If no
729 .B l
730 modifier is present: The
731 .I "const char\ *"
732 argument is expected to be a pointer to an array of character type (pointer
733 to a string).
734 Characters from the array are written up to (but not
735 including) a terminating null byte (\(aq\\0\(aq);
736 if a precision is specified, no more than the number specified
737 are written.
738 If a precision is given, no null byte need be present;
739 if the precision is not specified, or is greater than the size of the
740 array, the array must contain a terminating null byte.
741
742 If an
743 .B l
744 modifier is present: The
745 .I "const wchar_t\ *"
746 argument is expected to be a pointer to an array of wide characters.
747 Wide characters from the array are converted to multibyte characters
748 (each by a call to the
749 .BR wcrtomb (3)
750 function, with a conversion state starting in the initial state before
751 the first wide character), up to and including a terminating null
752 wide character.
753 The resulting multibyte characters are written up to
754 (but not including) the terminating null byte.
755 If a precision is
756 specified, no more bytes than the number specified are written, but
757 no partial multibyte characters are written.
758 Note that the precision
759 determines the number of
760 .I bytes
761 written, not the number of
762 .I wide characters
763 or
764 .IR "screen positions" .
765 The array must contain a terminating null wide character, unless a
766 precision is given and it is so small that the number of bytes written
767 exceeds it before the end of the array is reached.
768 .TP
769 .B C
770 (Not in C99, but in SUSv2.)
771 Synonym for
772 .BR lc .
773 Don't use.
774 .TP
775 .B S
776 (Not in C99, but in SUSv2.)
777 Synonym for
778 .BR ls .
779 Don't use.
780 .TP
781 .B p
782 The
783 .I "void\ *"
784 pointer argument is printed in hexadecimal (as if by
785 .B %#x
786 or
787 .BR %#lx ).
788 .TP
789 .B n
790 The number of characters written so far is stored into the integer
791 indicated by the
792 .I "int\ *"
793 (or variant) pointer argument.
794 No argument is converted.
795 .TP
796 .B m
797 (Glibc extension.)
798 Print output of
799 .IR strerror(errno) .
800 No argument is required.
801 .TP
802 .B %
803 A \(aq%\(aq is written.
804 No argument is converted.
805 The complete conversion
806 specification is \(aq%%\(aq.
807 .SH CONFORMING TO
808 The
809 .BR fprintf (),
810 .BR printf (),
811 .BR sprintf (),
812 .BR vprintf (),
813 .BR vfprintf (),
814 and
815 .BR vsprintf ()
816 functions conform to C89 and C99.
817 The
818 .BR snprintf ()
819 and
820 .BR vsnprintf ()
821 functions conform to C99.
822 .PP
823 Concerning the return value of
824 .BR snprintf (),
825 SUSv2 and C99 contradict each other: when
826 .BR snprintf ()
827 is called with
828 .IR size =0
829 then SUSv2 stipulates an unspecified return value less than 1,
830 while C99 allows
831 .I str
832 to be NULL in this case, and gives the return value (as always)
833 as the number of characters that would have been written in case
834 the output string has been large enough.
835 .PP
836 Linux libc4 knows about the five C standard flags.
837 It knows about the length modifiers \fBh\fP, \fBl\fP, \fBL\fP,
838 and the conversions
839 \fBc\fP, \fBd\fP, \fBe\fP, \fBE\fP, \fBf\fP, \fBF\fP,
840 \fBg\fP, \fBG\fP, \fBi\fP, \fBn\fP, \fBo\fP, \fBp\fP,
841 \fBs\fP, \fBu\fP, \fBx\fP, and \fBX\fP,
842 where \fBF\fP is a synonym for \fBf\fP.
843 Additionally, it accepts \fBD\fP, \fBO\fP, and \fBU\fP as synonyms
844 for \fBld\fP, \fBlo\fP, and \fBlu\fP.
845 (This is bad, and caused serious bugs later, when
846 support for \fB%D\fP disappeared.)
847 No locale-dependent radix character,
848 no thousands' separator, no NaN or infinity, no "%m$" and "*m$".
849 .PP
850 Linux libc5 knows about the five C standard flags and the \(aq flag,
851 locale, "%m$" and "*m$".
852 It knows about the length modifiers \fBh\fP, \fBl\fP, \fBL\fP,
853 \fBZ\fP, and \fBq\fP, but accepts \fBL\fP and \fBq\fP
854 both for \fIlong double\fP and for \fIlong long int\fP (this is a bug).
855 It no longer recognizes \fBF\fP, \fBD\fP, \fBO\fP, and \fBU\fP,
856 but adds the conversion character
857 .BR m ,
858 which outputs
859 .IR strerror(errno) .
860 .PP
861 glibc 2.0 adds conversion characters \fBC\fP and \fBS\fP.
862 .PP
863 glibc 2.1 adds length modifiers \fBhh\fP, \fBj\fP, \fBt\fP, and \fBz\fP
864 and conversion characters \fBa\fP and \fBA\fP.
865 .PP
866 glibc 2.2 adds the conversion character \fBF\fP with C99 semantics,
867 and the flag character \fBI\fP.
868 .SH NOTES
869 Some programs imprudently rely on code such as the following
870
871     sprintf(buf, "%s some further text", buf);
872
873 to append text to
874 .IR buf .
875 However, the standards explicitly note that the results are undefined
876 if source and destination buffers overlap when calling
877 .BR sprintf (),
878 .BR snprintf (),
879 .BR vsprintf (),
880 and
881 .BR vsnprintf ().
882 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=7075
883 Depending on the version of
884 .BR gcc (1)
885 used, and the compiler options employed, calls such as the above will
886 .B not
887 produce the expected results.
888
889 The glibc implementation of the functions
890 .BR snprintf ()
891 and
892 .BR vsnprintf ()
893 conforms to the C99 standard, that is, behaves as described above,
894 since glibc version 2.1.
895 Until glibc 2.0.6 they would return \-1
896 when the output was truncated.
897 .\" .SH HISTORY
898 .\" UNIX V7 defines the three routines
899 .\" .BR printf (),
900 .\" .BR fprintf (),
901 .\" .BR sprintf (),
902 .\" and has the flag \-, the width or precision *, the length modifier l,
903 .\" and the conversions doxfegcsu, and also D,O,U,X as synonyms for ld,lo,lu,lx.
904 .\" This is still true for 2.9.1BSD, but 2.10BSD has the flags
905 .\" #, + and <space> and no longer mentions D,O,U,X.
906 .\" 2.11BSD has
907 .\" .BR vprintf (),
908 .\" .BR vfprintf (),
909 .\" .BR vsprintf (),
910 .\" and warns not to use D,O,U,X.
911 .\" 4.3BSD Reno has the flag 0, the length modifiers h and L,
912 .\" and the conversions n, p, E, G, X (with current meaning)
913 .\" and deprecates D,O,U.
914 .\" 4.4BSD introduces the functions
915 .\" .BR snprintf ()
916 .\" and
917 .\" .BR vsnprintf (),
918 .\" and the length modifier q.
919 .\" FreeBSD also has functions
920 .\" .BR asprintf ()
921 .\" and
922 .\" .BR vasprintf (),
923 .\" that allocate a buffer large enough for
924 .\" .BR sprintf ().
925 .\" In glibc there are functions
926 .\" .BR dprintf ()
927 .\" and
928 .\" .BR vdprintf ()
929 .\" that print to a file descriptor instead of a stream.
930 .SH BUGS
931 Because
932 .BR sprintf ()
933 and
934 .BR vsprintf ()
935 assume an arbitrarily long string, callers must be careful not to overflow
936 the actual space; this is often impossible to assure.
937 Note that the length
938 of the strings produced is locale-dependent and difficult to predict.
939 Use
940 .BR snprintf ()
941 and
942 .BR vsnprintf ()
943 instead (or
944 .BR asprintf (3)
945 and
946 .BR vasprintf (3)).
947 .PP
948 Linux libc4.[45] does not have a
949 .BR snprintf (),
950 but provides a libbsd that contains an
951 .BR snprintf ()
952 equivalent to
953 .BR sprintf (),
954 that is, one that ignores the
955 .I size
956 argument.
957 Thus, the use of
958 .BR snprintf ()
959 with early libc4 leads to serious security problems.
960 .PP
961 Code such as
962 .BI printf( foo );
963 often indicates a bug, since
964 .I foo
965 may contain a % character.
966 If
967 .I foo
968 comes from untrusted user input, it may contain \fB%n\fP, causing the
969 .BR printf ()
970 call to write to memory and creating a security hole.
971 .\" .PP
972 .\" Some floating-point conversions under early libc4
973 .\" caused memory leaks.
974 .SH EXAMPLE
975 To print
976 .I Pi
977 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     int n;
1039     int size = 100;     /* Guess we need no more than 100 bytes */
1040     char *p, *np;
1041     va_list ap;
1042
1043     p = malloc(size);
1044     if (p == NULL)
1045         return NULL;
1046
1047     while (1) {
1048
1049         /* Try to print in the allocated space */
1050
1051         va_start(ap, fmt);
1052         n = vsnprintf(p, size, fmt, ap);
1053         va_end(ap);
1054
1055         /* Check error code */
1056
1057         if (n < 0) {
1058             free(p);
1059             return NULL;
1060         }
1061
1062         /* If that worked, return the string */
1063
1064         if (n < size)
1065             return p;
1066
1067         /* Else try again with more space */
1068
1069         size = n + 1;       /* Precisely what is needed */
1070
1071
1072         np = realloc(p, size);
1073         if (np == NULL) {
1074             free(p);
1075             return NULL;
1076         } else {
1077             p = np;
1078         }
1079     }
1080 }
1081 .fi
1082 .PP
1083 If truncation occurs in glibc versions prior to 2.0.6, this is treated as an
1084 error instead of being handled gracefully.
1085 .SH SEE ALSO
1086 .BR printf (1),
1087 .BR asprintf (3),
1088 .BR dprintf (3),
1089 .BR scanf (3),
1090 .BR setlocale (3),
1091 .BR wcrtomb (3),
1092 .BR wprintf (3),
1093 .BR locale (5)
1094 .SH COLOPHON
1095 This page is part of release 3.65 of the Linux
1096 .I man-pages
1097 project.
1098 A description of the project,
1099 and information about reporting bugs,
1100 can be found at
1101 \%http://www.kernel.org/doc/man\-pages/.