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