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