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