OSDN Git Service

LDP: Update original to LDP v3.79
[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  2014-07-08 "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 The converted value is padded on the right with blanks, rather
336 than on the left with blanks or zeros.
337 A
338 .B \-
339 overrides a
340 .B \&0
341 if both are given.
342 .TP
343 .B \(aq \(aq
344 (a space) A blank should be left before a positive number
345 (or empty string) produced by a signed conversion.
346 .TP
347 .B +
348 A sign (+ or \-) should always be placed before a number produced by a signed
349 conversion.
350 By default a sign is used only for negative numbers.
351 A
352 .B +
353 overrides a space if both are used.
354 .PP
355 The five flag characters above are defined in the C99 standard.
356 The Single UNIX Specification specifies one further flag character.
357 .TP
358 .B \(aq
359 For decimal conversion
360 .RB ( i ,
361 .BR d ,
362 .BR u ,
363 .BR f ,
364 .BR F ,
365 .BR g ,
366 .BR G )
367 the output is to be grouped with thousands' grouping characters
368 if the locale information indicates any.
369 Note that many versions of
370 .BR gcc (1)
371 cannot parse this option and will issue a warning.
372 (SUSv2 did not
373 include \fI%\(aqF\fP, but SUSv3 added it.)
374 .PP
375 glibc 2.2 adds one further flag character.
376 .TP
377 .B I
378 For decimal integer conversion
379 .RB ( i ,
380 .BR d ,
381 .BR u )
382 the output uses the locale's alternative output digits, if any.
383 For example, since glibc 2.2.3 this will give Arabic-Indic digits
384 in the Persian ("fa_IR") locale.
385 .\" outdigits keyword in locale file
386 .SS The field width
387 An optional decimal digit string (with nonzero first digit) specifying
388 a minimum field width.
389 If the converted value has fewer characters
390 than the field width, it will be padded with spaces on the left
391 (or right, if the left-adjustment flag has been given).
392 Instead of a decimal digit string one may write "*" or "*m$"
393 (for some decimal integer \fIm\fP) to specify that the field width
394 is given in the next argument, or in the \fIm\fP-th argument, respectively,
395 which must be of type
396 .IR int .
397 A negative field width is taken as a \(aq\-\(aq flag followed by a
398 positive field width.
399 In no case does a nonexistent or small field width cause truncation of a
400 field; if the result of a conversion is wider than the field width, the
401 field is expanded to contain the conversion result.
402 .SS The precision
403 An optional precision, in the form of a period (\(aq.\(aq)  followed by an
404 optional decimal digit string.
405 Instead of a decimal digit string one may write "*" or "*m$"
406 (for some decimal integer m) to specify that the precision
407 is given in the next argument, or in the m-th argument, respectively,
408 which must be of type
409 .IR int .
410 If the precision is given as just \(aq.\(aq, the precision is taken to
411 be zero.
412 A negative precision is taken as if the precision were omitted.
413 This gives the minimum number of digits to appear for
414 .BR d ,
415 .BR i ,
416 .BR o ,
417 .BR u ,
418 .BR x ,
419 and
420 .B X
421 conversions, the number of digits to appear after the radix character for
422 .BR a ,
423 .BR A ,
424 .BR e ,
425 .BR E ,
426 .BR f ,
427 and
428 .B F
429 conversions, the maximum number of significant digits for
430 .B g
431 and
432 .B G
433 conversions, or the maximum number of characters to be printed from a
434 string for
435 .B s
436 and
437 .B S
438 conversions.
439 .SS The length modifier
440 Here, "integer conversion" stands for
441 .BR d ,
442 .BR i ,
443 .BR o ,
444 .BR u ,
445 .BR x ,
446 or
447 .B X
448 conversion.
449 .TP
450 .B hh
451 A following integer conversion corresponds to a
452 .I signed char
453 or
454 .I unsigned char
455 argument, or a following
456 .B n
457 conversion corresponds to a pointer to a
458 .I signed char
459 argument.
460 .TP
461 .B h
462 A following integer conversion corresponds to a
463 .I short int
464 or
465 .I unsigned short int
466 argument, or a following
467 .B n
468 conversion corresponds to a pointer to a
469 .I short int
470 argument.
471 .TP
472 .B l
473 (ell) A following integer conversion corresponds to a
474 .I long int
475 or
476 .I unsigned long int
477 argument, or a following
478 .B n
479 conversion corresponds to a pointer to a
480 .I long int
481 argument, or a following
482 .B c
483 conversion corresponds to a
484 .I wint_t
485 argument, or a following
486 .B s
487 conversion corresponds to a pointer to
488 .I wchar_t
489 argument.
490 .TP
491 .B ll
492 (ell-ell).
493 A following integer conversion corresponds to a
494 .I long long int
495 or
496 .I unsigned long long int
497 argument, or a following
498 .B n
499 conversion corresponds to a pointer to a
500 .I long long int
501 argument.
502 .TP
503 .B L
504 A following
505 .BR a ,
506 .BR A ,
507 .BR e ,
508 .BR E ,
509 .BR f ,
510 .BR F ,
511 .BR g ,
512 or
513 .B G
514 conversion corresponds to a
515 .I long double
516 argument.
517 (C99 allows %LF, but SUSv2 does not.)
518 .\" .TP
519 .\" .B q
520 .\" ("quad". 4.4BSD and Linux libc5 only.
521 .\" Don't use.)
522 This is a synonym for
523 .BR ll .
524 .TP
525 .B j
526 A following integer conversion corresponds to an
527 .I intmax_t
528 or
529 .I uintmax_t
530 argument, or a following
531 .B n
532 conversion corresponds to a pointer to an
533 .I intmax_t
534 argument.
535 .TP
536 .B z
537 A following integer conversion corresponds to a
538 .I size_t
539 or
540 .I ssize_t
541 argument, or a following
542 .B n
543 conversion corresponds to a pointer to a
544 .I size_t
545 argument.
546 .\" (Linux libc5 has
547 .\" .B Z
548 .\" with this meaning.
549 .\" Don't use it.)
550 .TP
551 .B t
552 A following integer conversion corresponds to a
553 .I ptrdiff_t
554 argument, or a following
555 .B n
556 conversion corresponds to a pointer to a
557 .I ptrdiff_t
558 argument.
559 .PP
560 SUSv3 specifies all of the above.
561 SUSv2 specified only the length modifiers
562 .B h
563 (in
564 .BR hd ,
565 .BR hi ,
566 .BR ho ,
567 .BR hx ,
568 .BR hX ,
569 .BR hn )
570 and
571 .B l
572 (in
573 .BR ld ,
574 .BR li ,
575 .BR lo ,
576 .BR lx ,
577 .BR lX ,
578 .BR ln ,
579 .BR lc ,
580 .BR ls )
581 and
582 .B L
583 (in
584 .BR Le ,
585 .BR LE ,
586 .BR Lf ,
587 .BR Lg ,
588 .BR LG ).
589 .SS The conversion specifier
590 A character that specifies the type of conversion to be applied.
591 The conversion specifiers and their meanings are:
592 .TP
593 .BR d ", " i
594 The
595 .I int
596 argument is converted to signed decimal notation.
597 The precision, if any, gives the minimum number of digits
598 that must appear; if the converted value requires fewer digits, it is
599 padded on the left with zeros.
600 The default precision is 1.
601 When 0 is printed with an explicit precision 0, the output is empty.
602 .TP
603 .BR o ", " u ", " x ", " X
604 The
605 .I "unsigned int"
606 argument is converted to unsigned octal
607 .RB ( o ),
608 unsigned decimal
609 .RB ( u ),
610 or unsigned hexadecimal
611 .RB ( x
612 and
613 .BR X )
614 notation.
615 The letters
616 .B abcdef
617 are used for
618 .B x
619 conversions; the letters
620 .B ABCDEF
621 are used for
622 .B X
623 conversions.
624 The precision, if any, gives the minimum number of digits
625 that must appear; if the converted value requires fewer digits, it is
626 padded on the left with zeros.
627 The default precision is 1.
628 When 0 is printed with an explicit precision 0, the output is empty.
629 .TP
630 .BR e ", " E
631 The
632 .I double
633 argument is rounded and converted in the style
634 .RB [\-]d \&. ddd e \(+-dd
635 where there is one digit before the decimal-point character and the number
636 of digits after it is equal to the precision; if the precision is missing,
637 it is taken as 6; if the precision is zero, no decimal-point character
638 appears.
639 An
640 .B E
641 conversion uses the letter
642 .B E
643 (rather than
644 .BR e )
645 to introduce the exponent.
646 The exponent always contains at least two
647 digits; if the value is zero, the exponent is 00.
648 .TP
649 .BR f ", " F
650 The
651 .I double
652 argument is rounded and converted to decimal notation in the style
653 .RB [\-]ddd \&. ddd,
654 where the number of digits after the decimal-point character is equal to
655 the precision specification.
656 If the precision is missing, it is taken as
657 6; if the precision is explicitly zero, no decimal-point character appears.
658 If a decimal point appears, at least one digit appears before it.
659
660 (SUSv2 does not know about
661 .B F
662 and says that character string representations for infinity and NaN
663 may be made available.
664 SUSv3 adds a specification for
665 .BR F .
666 The C99 standard specifies "[\-]inf" or "[\-]infinity"
667 for infinity, and a string starting with "nan" for NaN, in the case of
668 .B f
669 conversion, and "[\-]INF" or "[\-]INFINITY" or "NAN*" in the case of
670 .B F
671 conversion.)
672 .TP
673 .BR g ", " G
674 The
675 .I double
676 argument is converted in style
677 .B f
678 or
679 .B e
680 (or
681 .B F
682 or
683 .B E
684 for
685 .B G
686 conversions).
687 The precision specifies the number of significant digits.
688 If the precision is missing, 6 digits are given; if the precision is zero,
689 it is treated as 1.
690 Style
691 .B e
692 is used if the exponent from its conversion is less than \-4 or greater
693 than or equal to the precision.
694 Trailing zeros are removed from the
695 fractional part of the result; a decimal point appears only if it is
696 followed by at least one digit.
697 .TP
698 .BR a ", " A
699 (C99; not in SUSv2, but added in SUSv3)
700 For
701 .B a
702 conversion, the
703 .I double
704 argument is converted to hexadecimal notation (using the letters abcdef)
705 in the style
706 .RB [\-] 0x h \&. hhhh p \(+-;
707 for
708 .B A
709 conversion the prefix
710 .BR 0X ,
711 the letters ABCDEF, and the exponent separator
712 .B P
713 is used.
714 There is one hexadecimal digit before the decimal point,
715 and the number of digits after it is equal to the precision.
716 The default precision suffices for an exact representation of the value
717 if an exact representation in base 2 exists
718 and otherwise is sufficiently large to distinguish values of type
719 .IR double .
720 The digit before the decimal point is unspecified for nonnormalized
721 numbers, and nonzero but otherwise unspecified for normalized numbers.
722 .TP
723 .B c
724 If no
725 .B l
726 modifier is present, the
727 .I int
728 argument is converted to an
729 .IR "unsigned char" ,
730 and the resulting character is written.
731 If an
732 .B l
733 modifier is present, the
734 .I wint_t
735 (wide character) argument is converted to a multibyte sequence by a call
736 to the
737 .BR wcrtomb (3)
738 function, with a conversion state starting in the initial state, and the
739 resulting multibyte string is written.
740 .TP
741 .B s
742 If no
743 .B l
744 modifier is present: The
745 .I "const char\ *"
746 argument is expected to be a pointer to an array of character type (pointer
747 to a string).
748 Characters from the array are written up to (but not
749 including) a terminating null byte (\(aq\\0\(aq);
750 if a precision is specified, no more than the number specified
751 are written.
752 If a precision is given, no null byte need be present;
753 if the precision is not specified, or is greater than the size of the
754 array, the array must contain a terminating null byte.
755
756 If an
757 .B l
758 modifier is present: The
759 .I "const wchar_t\ *"
760 argument is expected to be a pointer to an array of wide characters.
761 Wide characters from the array are converted to multibyte characters
762 (each by a call to the
763 .BR wcrtomb (3)
764 function, with a conversion state starting in the initial state before
765 the first wide character), up to and including a terminating null
766 wide character.
767 The resulting multibyte characters are written up to
768 (but not including) the terminating null byte.
769 If a precision is
770 specified, no more bytes than the number specified are written, but
771 no partial multibyte characters are written.
772 Note that the precision
773 determines the number of
774 .I bytes
775 written, not the number of
776 .I wide characters
777 or
778 .IR "screen positions" .
779 The array must contain a terminating null wide character, unless a
780 precision is given and it is so small that the number of bytes written
781 exceeds it before the end of the array is reached.
782 .TP
783 .B C
784 (Not in C99 or C11, but in SUSv2, SUSv3, and SUSv4.)
785 Synonym for
786 .BR lc .
787 Don't use.
788 .TP
789 .B S
790 (Not in C99 or C11, but in SUSv2, SUSv3, and SUSv4.)
791 Synonym for
792 .BR ls .
793 Don't use.
794 .TP
795 .B p
796 The
797 .I "void\ *"
798 pointer argument is printed in hexadecimal (as if by
799 .B %#x
800 or
801 .BR %#lx ).
802 .TP
803 .B n
804 The number of characters written so far is stored into the integer
805 pointed to by the corresponding argument.
806 That argument shall be an
807 .IR "int\ *" ,
808 or variant whose size matches the (optionally)
809 supplied integer length modifier.
810 No argument is converted.
811 The behavior is undefined if the conversion specification includes
812 any flags, a field width, or a precision.
813 .TP
814 .B m
815 (Glibc extension.)
816 Print output of
817 .IR strerror(errno) .
818 No argument is required.
819 .TP
820 .B %
821 A \(aq%\(aq is written.
822 No argument is converted.
823 The complete conversion
824 specification is \(aq%%\(aq.
825 .SH CONFORMING TO
826 The
827 .BR fprintf (),
828 .BR printf (),
829 .BR sprintf (),
830 .BR vprintf (),
831 .BR vfprintf (),
832 and
833 .BR vsprintf ()
834 functions conform to C89 and C99.
835 The
836 .BR snprintf ()
837 and
838 .BR vsnprintf ()
839 functions conform to C99.
840 .PP
841 Concerning the return value of
842 .BR snprintf (),
843 SUSv2 and C99 contradict each other: when
844 .BR snprintf ()
845 is called with
846 .IR size =0
847 then SUSv2 stipulates an unspecified return value less than 1,
848 while C99 allows
849 .I str
850 to be NULL in this case, and gives the return value (as always)
851 as the number of characters that would have been written in case
852 the output string has been large enough.
853 SUSv3 and later align their specification of
854 .BR snprintf ()
855 with C99.
856 .\" .PP
857 .\" Linux libc4 knows about the five C standard flags.
858 .\" It knows about the length modifiers \fBh\fP, \fBl\fP, \fBL\fP,
859 .\" and the conversions
860 .\" \fBc\fP, \fBd\fP, \fBe\fP, \fBE\fP, \fBf\fP, \fBF\fP,
861 .\" \fBg\fP, \fBG\fP, \fBi\fP, \fBn\fP, \fBo\fP, \fBp\fP,
862 .\" \fBs\fP, \fBu\fP, \fBx\fP, and \fBX\fP,
863 .\" where \fBF\fP is a synonym for \fBf\fP.
864 .\" Additionally, it accepts \fBD\fP, \fBO\fP, and \fBU\fP as synonyms
865 .\" for \fBld\fP, \fBlo\fP, and \fBlu\fP.
866 .\" (This is bad, and caused serious bugs later, when
867 .\" support for \fB%D\fP disappeared.)
868 .\" No locale-dependent radix character,
869 .\" no thousands' separator, no NaN or infinity, no "%m$" and "*m$".
870 .\" .PP
871 .\" Linux libc5 knows about the five C standard flags and the \(aq flag,
872 .\" locale, "%m$" and "*m$".
873 .\" It knows about the length modifiers \fBh\fP, \fBl\fP, \fBL\fP,
874 .\" \fBZ\fP, and \fBq\fP, but accepts \fBL\fP and \fBq\fP
875 .\" both for \fIlong double\fP and for \fIlong long int\fP (this is a bug).
876 .\" It no longer recognizes \fBF\fP, \fBD\fP, \fBO\fP, and \fBU\fP,
877 .\" but adds the conversion character
878 .\" .BR m ,
879 .\" which outputs
880 .\" .IR strerror(errno) .
881 .\" .PP
882 .\" glibc 2.0 adds conversion characters \fBC\fP and \fBS\fP.
883 .PP
884 glibc 2.1 adds length modifiers \fBhh\fP, \fBj\fP, \fBt\fP, and \fBz\fP
885 and conversion characters \fBa\fP and \fBA\fP.
886 .PP
887 glibc 2.2 adds the conversion character \fBF\fP with C99 semantics,
888 and the flag character \fBI\fP.
889 .SH NOTES
890 Some programs imprudently rely on code such as the following
891
892     sprintf(buf, "%s some further text", buf);
893
894 to append text to
895 .IR buf .
896 However, the standards explicitly note that the results are undefined
897 if source and destination buffers overlap when calling
898 .BR sprintf (),
899 .BR snprintf (),
900 .BR vsprintf (),
901 and
902 .BR vsnprintf ().
903 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=7075
904 Depending on the version of
905 .BR gcc (1)
906 used, and the compiler options employed, calls such as the above will
907 .B not
908 produce the expected results.
909
910 The glibc implementation of the functions
911 .BR snprintf ()
912 and
913 .BR vsnprintf ()
914 conforms to the C99 standard, that is, behaves as described above,
915 since glibc version 2.1.
916 Until glibc 2.0.6, they would return \-1
917 when the output was truncated.
918 .\" .SH HISTORY
919 .\" UNIX V7 defines the three routines
920 .\" .BR printf (),
921 .\" .BR fprintf (),
922 .\" .BR sprintf (),
923 .\" and has the flag \-, the width or precision *, the length modifier l,
924 .\" and the conversions doxfegcsu, and also D,O,U,X as synonyms for ld,lo,lu,lx.
925 .\" This is still true for 2.9.1BSD, but 2.10BSD has the flags
926 .\" #, + and <space> and no longer mentions D,O,U,X.
927 .\" 2.11BSD has
928 .\" .BR vprintf (),
929 .\" .BR vfprintf (),
930 .\" .BR vsprintf (),
931 .\" and warns not to use D,O,U,X.
932 .\" 4.3BSD Reno has the flag 0, the length modifiers h and L,
933 .\" and the conversions n, p, E, G, X (with current meaning)
934 .\" and deprecates D,O,U.
935 .\" 4.4BSD introduces the functions
936 .\" .BR snprintf ()
937 .\" and
938 .\" .BR vsnprintf (),
939 .\" and the length modifier q.
940 .\" FreeBSD also has functions
941 .\" .BR asprintf ()
942 .\" and
943 .\" .BR vasprintf (),
944 .\" that allocate a buffer large enough for
945 .\" .BR sprintf ().
946 .\" In glibc there are functions
947 .\" .BR dprintf ()
948 .\" and
949 .\" .BR vdprintf ()
950 .\" that print to a file descriptor instead of a stream.
951 .SH BUGS
952 Because
953 .BR sprintf ()
954 and
955 .BR vsprintf ()
956 assume an arbitrarily long string, callers must be careful not to overflow
957 the actual space; this is often impossible to assure.
958 Note that the length
959 of the strings produced is locale-dependent and difficult to predict.
960 Use
961 .BR snprintf ()
962 and
963 .BR vsnprintf ()
964 instead (or
965 .BR asprintf (3)
966 and
967 .BR vasprintf (3)).
968 .\" .PP
969 .\" Linux libc4.[45] does not have a
970 .\" .BR snprintf (),
971 .\" but provides a libbsd that contains an
972 .\" .BR snprintf ()
973 .\" equivalent to
974 .\" .BR sprintf (),
975 .\" that is, one that ignores the
976 .\" .I size
977 .\" argument.
978 .\" Thus, the use of
979 .\" .BR snprintf ()
980 .\" with early libc4 leads to serious security problems.
981 .PP
982 Code such as
983 .BI printf( foo );
984 often indicates a bug, since
985 .I foo
986 may contain a % character.
987 If
988 .I foo
989 comes from untrusted user input, it may contain \fB%n\fP, causing the
990 .BR printf ()
991 call to write to memory and creating a security hole.
992 .\" .PP
993 .\" Some floating-point conversions under early libc4
994 .\" caused memory leaks.
995 .SH EXAMPLE
996 To print
997 .I Pi
998 to five decimal places:
999 .in +4n
1000 .nf
1001
1002 #include <math.h>
1003 #include <stdio.h>
1004 fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0));
1005 .fi
1006 .in
1007 .PP
1008 To print a date and time in the form "Sunday, July 3, 10:02",
1009 where
1010 .I weekday
1011 and
1012 .I month
1013 are pointers to strings:
1014 .in +4n
1015 .nf
1016
1017 #include <stdio.h>
1018 fprintf(stdout, "%s, %s %d, %.2d:%.2d\en",
1019         weekday, month, day, hour, min);
1020 .fi
1021 .in
1022 .PP
1023 Many countries use the day-month-year order.
1024 Hence, an internationalized version must be able to print
1025 the arguments in an order specified by the format:
1026 .in +4n
1027 .nf
1028
1029 #include <stdio.h>
1030 fprintf(stdout, format,
1031         weekday, month, day, hour, min);
1032
1033 .fi
1034 .in
1035 where
1036 .I format
1037 depends on locale, and may permute the arguments.
1038 With the value:
1039 .in +4n
1040 .nf
1041
1042 "%1$s, %3$d. %2$s, %4$d:%5$.2d\en"
1043
1044 .fi
1045 .in
1046 one might obtain "Sonntag, 3. Juli, 10:02".
1047 .PP
1048 To allocate a sufficiently large string and print into it
1049 (code correct for both glibc 2.0 and glibc 2.1):
1050 .nf
1051
1052 #include <stdio.h>
1053 #include <stdlib.h>
1054 #include <stdarg.h>
1055
1056 char *
1057 make_message(const char *fmt, ...)
1058 {
1059     int n;
1060     int size = 100;     /* Guess we need no more than 100 bytes */
1061     char *p, *np;
1062     va_list ap;
1063
1064     p = malloc(size);
1065     if (p == NULL)
1066         return NULL;
1067
1068     while (1) {
1069
1070         /* Try to print in the allocated space */
1071
1072         va_start(ap, fmt);
1073         n = vsnprintf(p, size, fmt, ap);
1074         va_end(ap);
1075
1076         /* Check error code */
1077
1078         if (n < 0) {
1079             free(p);
1080             return NULL;
1081         }
1082
1083         /* If that worked, return the string */
1084
1085         if (n < size)
1086             return p;
1087
1088         /* Else try again with more space */
1089
1090         size = n + 1;       /* Precisely what is needed */
1091
1092
1093         np = realloc(p, size);
1094         if (np == NULL) {
1095             free(p);
1096             return NULL;
1097         } else {
1098             p = np;
1099         }
1100     }
1101 }
1102 .fi
1103 .PP
1104 If truncation occurs in glibc versions prior to 2.0.6, this is treated as an
1105 error instead of being handled gracefully.
1106 .SH SEE ALSO
1107 .BR printf (1),
1108 .BR asprintf (3),
1109 .BR dprintf (3),
1110 .BR scanf (3),
1111 .BR setlocale (3),
1112 .BR wcrtomb (3),
1113 .BR wprintf (3),
1114 .BR locale (5)
1115 .SH COLOPHON
1116 This page is part of release 3.79 of the Linux
1117 .I man-pages
1118 project.
1119 A description of the project,
1120 information about reporting bugs,
1121 and the latest version of this page,
1122 can be found at
1123 \%http://www.kernel.org/doc/man\-pages/.