2 .\" Copyright (c) 1999 Andries Brouwer (aeb@cwi.nl)
4 .\" Earlier versions of this page influenced the present text.
5 .\" It was derived from a Berkeley page with version
6 .\" @(#)printf.3 6.14 (Berkeley) 7/30/91
7 .\" converted for Linux by faith@cs.unc.edu, updated by
8 .\" Helmut.Geyer@iwr.uni-heidelberg.de, agulbra@troll.no and Bruno Haible.
10 .\" SPDX-License-Identifier: GPL-2.0-or-later
12 .\" 1999-11-25 aeb - Rewritten, using SUSv2 and C99.
13 .\" 2000-07-26 jsm28@hermes.cam.ac.uk - three small fixes
14 .\" 2000-10-16 jsm28@hermes.cam.ac.uk - more fixes
16 .TH printf 3 (date) "Linux man-pages (unreleased)"
18 printf, fprintf, dprintf, sprintf, snprintf, vprintf, vfprintf, vdprintf,
19 vsprintf, vsnprintf \- formatted output conversion
22 .RI ( libc ", " \-lc )
27 .BI "int printf(const char *restrict " format ", ...);"
28 .BI "int fprintf(FILE *restrict " stream ,
29 .BI " const char *restrict " format ", ...);"
30 .BI "int dprintf(int " fd ,
31 .BI " const char *restrict " format ", ...);"
32 .BI "int sprintf(char *restrict " str ,
33 .BI " const char *restrict " format ", ...);"
34 .BI "int snprintf(char " str "[restrict ." size "], size_t " size ,
35 .BI " const char *restrict " format ", ...);"
37 .BI "int vprintf(const char *restrict " format ", va_list " ap );
38 .BI "int vfprintf(FILE *restrict " stream ,
39 .BI " const char *restrict " format ", va_list " ap );
40 .BI "int vdprintf(int " fd ,
41 .BI " const char *restrict " format ", va_list " ap );
42 .BI "int vsprintf(char *restrict " str ,
43 .BI " const char *restrict " format ", va_list " ap );
44 .BI "int vsnprintf(char " str "[restrict ." size "], size_t " size ,
45 .BI " const char *restrict " format ", va_list " ap );
49 Feature Test Macro Requirements for glibc (see
50 .BR feature_test_macros (7)):
56 _XOPEN_SOURCE >= 500 || _ISOC99_SOURCE
57 || /* glibc <= 2.19: */ _BSD_SOURCE
64 _POSIX_C_SOURCE >= 200809L
71 family produce output according to a
80 the standard output stream;
84 write output to the given output
91 write to the character string
98 except that it outputs to a file descriptor,
110 bytes (including the terminating null byte (\[aq]\e0\[aq])) to
119 are equivalent to the functions
125 respectively, except that they are called with a
127 instead of a variable number of arguments.
128 These functions do not call the
131 Because they invoke the
135 is undefined after the call.
139 All of these functions write the output under the control of a
141 string that specifies how subsequent arguments (or arguments accessed via
142 the variable-length argument facilities of
144 are converted for output.
146 C99 and POSIX.1-2001 specify that the results are undefined if a call to
152 would cause copying to take place between objects that overlap
153 (e.g., if the target string array and one of the supplied input arguments
154 refer to the same buffer).
156 .SS Format of the format string
157 The format string is a character string, beginning and ending
158 in its initial shift state, if any.
159 The format string is composed of zero or more directives: ordinary
162 which are copied unchanged to the output stream;
163 and conversion specifications, each of which results in fetching zero or
164 more subsequent arguments.
165 Each conversion specification is introduced by
169 .IR "conversion specifier" .
170 In between there may be (in this order) zero or more
177 .IR "length modifier" .
179 The overall syntax of a conversion specification is:
183 %[$][flags][width][.precision][length modifier]conversion
187 The arguments must correspond properly (after type promotion) with the
188 conversion specifier.
189 By default, the arguments are used in the order
190 given, where each \[aq]*\[aq] (see
194 below) and each conversion specifier asks for the next
195 argument (and it is an error if insufficiently many arguments are given).
196 One can also specify explicitly which argument is taken,
197 at each place where an argument is required, by writing "%m$" instead
198 of \[aq]%\[aq] and "*m$" instead of \[aq]*\[aq],
199 where the decimal integer \fIm\fP denotes
200 the position in the argument list of the desired argument, indexed starting
206 printf("%*d", width, num);
214 printf("%2$*1$d", width, num);
219 The second style allows repeated references to the
221 The C99 standard does not include the style using \[aq]$\[aq],
222 which comes from the Single UNIX Specification.
224 \[aq]$\[aq] is used, it must be used throughout for all conversions taking an
225 argument and all width and precision arguments, but it may be mixed
226 with "%%" formats, which do not consume an argument.
228 gaps in the numbers of arguments specified using \[aq]$\[aq]; for example, if
229 arguments 1 and 3 are specified, argument 2 must also be specified
230 somewhere in the format string.
232 For some numeric conversions a radix character ("decimal point") or
233 thousands' grouping character is used.
234 The actual character used
241 uses \[aq].\[aq] as radix character, and does not have a grouping character.
246 printf("%\[aq].2f", 1234567.89);
250 results in "1234567.89" in the POSIX locale, in "1234567,89" in the
251 nl_NL locale, and in "1.234.567,89" in the da_DK locale.
253 The character % is followed by zero or more of the following flags:
256 The value should be converted to an "alternate form".
259 conversions, the first character of the output string is made zero
260 (by prefixing a 0 if it was not zero already).
265 conversions, a nonzero result has the string "0x" (or "0X" for
267 conversions) prepended to it.
278 conversions, the result will always contain a decimal point, even if no
279 digits follow it (normally, a decimal point appears in the results of those
280 conversions only if a digit follows).
285 conversions, trailing zeros are not removed from the result as they would
291 contains a valid error code,
293 .I strerrorname_np(errno)
295 otherwise, the value stored in
297 is printed as a decimal number.
298 For other conversions, the result is undefined.
301 The value should be zero padded.
318 conversions, the converted value is padded on the left with zeros rather
324 flags both appear, the
327 If a precision is given with an integer conversion
338 For other conversions, the behavior is undefined.
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.
352 (a space) A blank should be left before a positive number
353 (or empty string) produced by a signed conversion.
356 A sign (+ or \-) should always be placed before a number produced by a signed
358 By default, a sign is used only for negative numbers.
361 overrides a space if both are used.
363 The five flag characters above are defined in the C99 standard.
364 The Single UNIX Specification specifies one further flag character.
367 For decimal conversion
375 the output is to be grouped with thousands' grouping characters
376 if the locale information indicates any.
379 Note that many versions of
381 cannot parse this option and will issue a warning.
383 include \fI%\[aq]F\fP, but SUSv3 added it.)
384 Note also that the default locale of a C program is "C"
385 whose locale information indicates no thousands' grouping character.
386 Therefore, without a prior call to
388 no thousands' grouping characters will be printed.
390 glibc 2.2 adds one further flag character.
393 For decimal integer conversion
397 the output uses the locale's alternative output digits, if any.
398 For example, since glibc 2.2.3 this will give Arabic-Indic digits
399 in the Persian ("fa_IR") locale.
400 .\" outdigits keyword in locale file
402 An optional decimal digit string (with nonzero first digit) specifying
403 a minimum field width.
404 If the converted value has fewer characters
405 than the field width, it will be padded with spaces on the left
406 (or right, if the left-adjustment flag has been given).
407 Instead of a decimal digit string one may write "*" or "*m$"
408 (for some decimal integer \fIm\fP) to specify that the field width
409 is given in the next argument, or in the \fIm\fP-th argument, respectively,
410 which must be of type
412 A negative field width is taken as a \[aq]\-\[aq] flag followed by a
413 positive field width.
414 In no case does a nonexistent or small field width cause truncation of a
415 field; if the result of a conversion is wider than the field width, the
416 field is expanded to contain the conversion result.
418 An optional precision, in the form of a period (\[aq].\[aq]) followed by an
419 optional decimal digit string.
420 Instead of a decimal digit string one may write "*" or "*m$"
421 (for some decimal integer \fIm\fP) to specify that the precision
422 is given in the next argument, or in the \fIm\fP-th argument, respectively,
423 which must be of type
425 If the precision is given as just \[aq].\[aq], the precision is taken to
427 A negative precision is taken as if the precision were omitted.
428 This gives the minimum number of digits to appear for
436 conversions, the number of digits to appear after the radix character for
444 conversions, the maximum number of significant digits for
448 conversions, or the maximum number of characters to be printed from a
455 Here, "integer conversion" stands for
466 A following integer conversion corresponds to a
470 argument, or a following
472 conversion corresponds to a pointer to a
477 A following integer conversion corresponds to a
481 argument, or a following
483 conversion corresponds to a pointer to a
488 (ell) A following integer conversion corresponds to a
492 argument, or a following
494 conversion corresponds to a pointer to a
496 argument, or a following
498 conversion corresponds to a
500 argument, or a following
502 conversion corresponds to a pointer to
515 conversion, this length modifier is ignored (C99; not in SUSv2).
519 A following integer conversion corresponds to a
522 .I unsigned long long
523 argument, or a following
525 conversion corresponds to a pointer to a
532 This is a nonstandard extension, derived from BSD;
533 avoid its use in new code.
546 conversion corresponds to a
549 (C99 allows %LF, but SUSv2 does not.)
552 A following integer conversion corresponds to an
556 argument, or a following
558 conversion corresponds to a pointer to an
563 A following integer conversion corresponds to a
567 argument, or a following
569 conversion corresponds to a pointer to a
574 A nonstandard synonym for
576 that predates the appearance of
578 Do not use in new code.
581 A following integer conversion corresponds to a
583 argument, or a following
585 conversion corresponds to a pointer to a
589 SUSv3 specifies all of the above,
590 except for those modifiers explicitly noted as being nonstandard extensions.
591 SUSv2 specified only the length modifiers
620 As a nonstandard extension, the GNU implementations treats
624 as synonyms, so that one can, for example, write
626 (as a synonym for the standards-compliant
630 (as a synonym for the standards compliant
632 Such usage is nonportable.
634 .SS Conversion specifiers
635 A character that specifies the type of conversion to be applied.
636 The conversion specifiers and their meanings are:
641 argument is converted to signed decimal notation.
642 The precision, if any, gives the minimum number of digits
643 that must appear; if the converted value requires fewer digits, it is
644 padded on the left with zeros.
645 The default precision is 1.
646 When 0 is printed with an explicit precision 0, the output is empty.
648 .BR o ", " u ", " x ", " X
651 argument is converted to unsigned octal
655 or unsigned hexadecimal
664 conversions; the letters
669 The precision, if any, gives the minimum number of digits
670 that must appear; if the converted value requires fewer digits, it is
671 padded on the left with zeros.
672 The default precision is 1.
673 When 0 is printed with an explicit precision 0, the output is empty.
678 argument is rounded and converted in the style
679 .RB [\-]d \&. ddd e \(+-dd
680 where there is one digit (which is nonzero if the argument is nonzero)
681 before the decimal-point character and the number
682 of digits after it is equal to the precision; if the precision is missing,
683 it is taken as 6; if the precision is zero, no decimal-point character
687 conversion uses the letter
691 to introduce the exponent.
692 The exponent always contains at least two
693 digits; if the value is zero, the exponent is 00.
698 argument is rounded and converted to decimal notation in the style
700 where the number of digits after the decimal-point character is equal to
701 the precision specification.
702 If the precision is missing, it is taken as
703 6; if the precision is explicitly zero, no decimal-point character appears.
704 If a decimal point appears, at least one digit appears before it.
706 (SUSv2 does not know about
708 and says that character string representations for infinity and NaN
709 may be made available.
710 SUSv3 adds a specification for
712 The C99 standard specifies "[\-]inf" or "[\-]infinity"
713 for infinity, and a string starting with "nan" for NaN, in the case of
715 conversion, and "[\-]INF" or "[\-]INFINITY" or "NAN" in the case of
722 argument is converted in style
733 The precision specifies the number of significant digits.
734 If the precision is missing, 6 digits are given; if the precision is zero,
738 is used if the exponent from its conversion is less than \-4 or greater
739 than or equal to the precision.
740 Trailing zeros are removed from the
741 fractional part of the result; a decimal point appears only if it is
742 followed by at least one digit.
745 (C99; not in SUSv2, but added in SUSv3)
750 argument is converted to hexadecimal notation (using the letters abcdef)
752 .RB [\-] 0x h \&. hhhh p \(+-d;
755 conversion the prefix
757 the letters ABCDEF, and the exponent separator
760 There is one hexadecimal digit before the decimal point,
761 and the number of digits after it is equal to the precision.
762 The default precision suffices for an exact representation of the value
763 if an exact representation in base 2 exists
764 and otherwise is sufficiently large to distinguish values of type
766 The digit before the decimal point is unspecified for nonnormalized
767 numbers, and nonzero but otherwise unspecified for normalized numbers.
768 The exponent always contains at least one
769 digit; if the value is zero, the exponent is 0.
774 modifier is present, the
776 argument is converted to an
777 .IR "unsigned char" ,
778 and the resulting character is written.
781 modifier is present, the
783 (wide character) argument is converted to a multibyte sequence by a call
786 function, with a conversion state starting in the initial state, and the
787 resulting multibyte string is written.
792 modifier is present: the
794 argument is expected to be a pointer to an array of character type (pointer
796 Characters from the array are written up to (but not
797 including) a terminating null byte (\[aq]\e0\[aq]);
798 if a precision is specified, no more than the number specified
800 If a precision is given, no null byte need be present;
801 if the precision is not specified, or is greater than the size of the
802 array, the array must contain a terminating null byte.
806 modifier is present: the
807 .I "const wchar_t\ *"
808 argument is expected to be a pointer to an array of wide characters.
809 Wide characters from the array are converted to multibyte characters
810 (each by a call to the
812 function, with a conversion state starting in the initial state before
813 the first wide character), up to and including a terminating null
815 The resulting multibyte characters are written up to
816 (but not including) the terminating null byte.
818 specified, no more bytes than the number specified are written, but
819 no partial multibyte characters are written.
820 Note that the precision
821 determines the number of
823 written, not the number of
826 .IR "screen positions" .
827 The array must contain a terminating null wide character, unless a
828 precision is given and it is so small that the number of bytes written
829 exceeds it before the end of the array is reached.
832 (Not in C99 or C11, but in SUSv2, SUSv3, and SUSv4.)
838 (Not in C99 or C11, but in SUSv2, SUSv3, and SUSv4.)
846 pointer argument is printed in hexadecimal (as if by
852 The number of characters written so far is stored into the integer
853 pointed to by the corresponding argument.
854 That argument shall be an
856 or variant whose size matches the (optionally)
857 supplied integer length modifier.
858 No argument is converted.
859 (This specifier is not supported by the bionic C library.)
860 The behavior is undefined if the conversion specification includes
861 any flags, a field width, or a precision.
864 (glibc extension; supported by uClibc and musl.)
868 .I strerrorname_np(errno)
869 in the alternate form).
870 No argument is required.
873 A \[aq]%\[aq] is written.
874 No argument is converted.
875 The complete conversion
876 specification is \[aq]%%\[aq].
878 Upon successful return, these functions return the number of bytes
879 printed (excluding the null byte used to end output to strings).
885 do not write more than
887 bytes (including the terminating null byte (\[aq]\e0\[aq])).
888 If the output was truncated due to this limit, then the return value
889 is the number of characters (excluding the terminating null byte)
890 which would have been written to the final string if enough space
892 Thus, a return value of
894 or more means that the output was truncated.
895 (See also below under CAVEATS.)
897 If an output error is encountered, a negative value is returned.
899 For an explanation of the terms used in this section, see
905 Interface Attribute Value
917 T} Thread safety MT-Safe locale
960 SUSv2, C99, POSIX.1-2001.
962 Concerning the return value of
964 SUSv2 and C99 contradict each other: when
968 then SUSv2 stipulates an unspecified return value less than 1,
971 to be NULL in this case, and gives the return value (as always)
972 as the number of characters that would have been written in case
973 the output string has been large enough.
974 POSIX.1-2001 and later align their specification of
983 .\" Linux libc4 knows about the five C standard flags.
984 .\" It knows about the length modifiers \fBh\fP, \fBl\fP, \fBL\fP,
985 .\" and the conversions
986 .\" \fBc\fP, \fBd\fP, \fBe\fP, \fBE\fP, \fBf\fP, \fBF\fP,
987 .\" \fBg\fP, \fBG\fP, \fBi\fP, \fBn\fP, \fBo\fP, \fBp\fP,
988 .\" \fBs\fP, \fBu\fP, \fBx\fP, and \fBX\fP,
989 .\" where \fBF\fP is a synonym for \fBf\fP.
990 .\" Additionally, it accepts \fBD\fP, \fBO\fP, and \fBU\fP as synonyms
991 .\" for \fBld\fP, \fBlo\fP, and \fBlu\fP.
992 .\" (This is bad, and caused serious bugs later, when
993 .\" support for \fB%D\fP disappeared.)
994 .\" No locale-dependent radix character,
995 .\" no thousands' separator, no NaN or infinity, no "%m$" and "*m$".
997 .\" Linux libc5 knows about the five C standard flags and the \[aq] flag,
998 .\" locale, "%m$" and "*m$".
999 .\" It knows about the length modifiers \fBh\fP, \fBl\fP, \fBL\fP,
1000 .\" \fBZ\fP, and \fBq\fP, but accepts \fBL\fP and \fBq\fP
1001 .\" both for \fIlong double\fP and for \fIlong long\fP (this is a bug).
1002 .\" It no longer recognizes \fBF\fP, \fBD\fP, \fBO\fP, and \fBU\fP,
1003 .\" but adds the conversion character
1006 .\" .IR strerror(errno) .
1008 .\" glibc 2.0 adds conversion characters \fBC\fP and \fBS\fP.
1010 glibc 2.1 adds length modifiers \fBhh\fP, \fBj\fP, \fBt\fP, and \fBz\fP
1011 and conversion characters \fBa\fP and \fBA\fP.
1013 glibc 2.2 adds the conversion character \fBF\fP with C99 semantics,
1014 and the flag character \fBI\fP.
1016 glibc 2.35 gives a meaning to the alternate form
1020 conversion specifier, that is
1023 Some programs imprudently rely on code such as the following
1027 sprintf(buf, "%s some further text", buf);
1033 However, the standards explicitly note that the results are undefined
1034 if source and destination buffers overlap when calling
1040 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=7075
1041 Depending on the version of
1043 used, and the compiler options employed, calls such as the above will
1045 produce the expected results.
1047 The glibc implementation of the functions
1051 conforms to the C99 standard, that is, behaves as described above,
1053 Until glibc 2.0.6, they would return \-1
1054 when the output was truncated.
1056 .\" UNIX V7 defines the three routines
1060 .\" and has the flag \-, the width or precision *, the length modifier l,
1061 .\" and the conversions doxfegcsu, and also D,O,U,X as synonyms for ld,lo,lu,lx.
1062 .\" This is still true for 2.9.1BSD, but 2.10BSD has the flags
1063 .\" #, + and <space> and no longer mentions D,O,U,X.
1066 .\" .BR vfprintf (),
1067 .\" .BR vsprintf (),
1068 .\" and warns not to use D,O,U,X.
1069 .\" 4.3BSD Reno has the flag 0, the length modifiers h and L,
1070 .\" and the conversions n, p, E, G, X (with current meaning)
1071 .\" and deprecates D,O,U.
1072 .\" 4.4BSD introduces the functions
1075 .\" .BR vsnprintf (),
1076 .\" and the length modifier q.
1077 .\" FreeBSD also has functions
1080 .\" .BR vasprintf (),
1081 .\" that allocate a buffer large enough for
1083 .\" In glibc there are functions
1087 .\" that print to a file descriptor instead of a stream.
1093 assume an arbitrarily long string, callers must be careful not to overflow
1094 the actual space; this is often impossible to assure.
1095 Note that the length
1096 of the strings produced is locale-dependent and difficult to predict.
1106 .\" Linux libc4.[45] does not have a
1107 .\" .BR snprintf (),
1108 .\" but provides a libbsd that contains an
1112 .\" that is, one that ignores the
1115 .\" Thus, the use of
1117 .\" with early libc4 leads to serious security problems.
1121 often indicates a bug, since
1123 may contain a % character.
1126 comes from untrusted user input, it may contain \fB%n\fP, causing the
1128 call to write to memory and creating a security hole.
1130 .\" Some floating-point conversions under early libc4
1131 .\" caused memory leaks.
1135 to five decimal places:
1141 fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0));
1145 To print a date and time in the form "Sunday, July 3, 10:02",
1150 are pointers to strings:
1155 fprintf(stdout, "%s, %s %d, %.2d:%.2d\en",
1156 weekday, month, day, hour, min);
1160 Many countries use the day-month-year order.
1161 Hence, an internationalized version must be able to print
1162 the arguments in an order specified by the format:
1167 fprintf(stdout, format,
1168 weekday, month, day, hour, min);
1174 depends on locale, and may permute the arguments.
1179 "%1$s, %3$d. %2$s, %4$d:%5$.2d\en"
1183 one might obtain "Sonntag, 3. Juli, 10:02".
1185 To allocate a sufficiently large string and print into it
1186 (code correct for both glibc 2.0 and glibc 2.1):
1194 make_message(const char *fmt, ...)
1201 /* Determine required size. */
1204 n = vsnprintf(p, size, fmt, ap);
1210 size = (size_t) n + 1; /* One extra byte for \[aq]\e0\[aq] */
1216 n = vsnprintf(p, size, fmt, ap);
1228 If truncation occurs in glibc versions prior to glibc 2.0.6,
1229 this is treated as an error instead of being handled gracefully.