1 .\" Copyright (c) 1999 Andries Brouwer (aeb@cwi.nl)
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.
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.
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.
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.
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/>.
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
34 .TH PRINTF 3 2015-08-08 "GNU" "Linux Programmer's Manual"
36 printf, fprintf, dprintf, sprintf, snprintf, vprintf, vfprintf, vdprintf,
37 vsprintf, vsnprintf \- formatted output conversion
41 .BI "int printf(const char *" format ", ...);"
43 .BI "int fprintf(FILE *" stream ", const char *" format ", ...);"
45 .BI "int dprintf(int " fd ", const char *" format ", ...);"
47 .BI "int sprintf(char *" str ", const char *" format ", ...);"
49 .BI "int snprintf(char *" str ", size_t " size ", const char *" format ", ...);"
51 .B #include <stdarg.h>
53 .BI "int vprintf(const char *" format ", va_list " ap );
55 .BI "int vfprintf(FILE *" stream ", const char *" format ", va_list " ap );
57 .BI "int vdprintf(int " fd ", const char *" format ", va_list " ap );
59 .BI "int vsprintf(char *" str ", const char *" format ", va_list " ap );
61 .BI "int vsnprintf(char *" str ", size_t " size ", const char *" format \
65 Feature Test Macro Requirements for glibc (see
66 .BR feature_test_macros (7)):
73 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 || _ISOC99_SOURCE ||
74 _POSIX_C_SOURCE\ >=\ 200112L;
86 _XOPEN_SOURCE\ >=\ 700 || _POSIX_C_SOURCE\ >=\ 200809L
96 family produce output according to a
105 the standard output stream;
109 write output to the given output
116 write to the character string
123 except that it outputs to a file descriptor,
135 bytes (including the terminating null byte (\(aq\e0\(aq)) to
144 are equivalent to the functions
150 respectively, except that they are called with a
152 instead of a variable number of arguments.
153 These functions do not call the
156 Because they invoke the
160 is undefined after the call.
164 All of these functions write the output under the control of a
166 string that specifies how subsequent arguments (or arguments accessed via
167 the variable-length argument facilities of
169 are converted for output.
171 C99 and POSIX.1-2001 specify that the results are undefined if a call to
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).
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
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
194 .IR "conversion specifier" .
195 In between there may be (in this order) zero or more
202 .IR "length modifier" .
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 (see
211 below) and each conversion specifier asks for the next
212 argument (and it is an error if insufficiently many arguments are given).
213 One can also specify explicitly which argument is taken,
214 at each place where an argument is required, by writing "%m$" instead
215 of \(aq%\(aq and "*m$" instead of \(aq*\(aq,
216 where the decimal integer \fIm\fP denotes
217 the position in the argument list of the desired argument, indexed starting
223 printf("%*d", width, num);
231 printf("%2$*1$d", width, num);
236 The second style allows repeated references to the
238 The C99 standard does not include the style using \(aq$\(aq,
239 which comes from the Single UNIX Specification.
241 \(aq$\(aq is used, it must be used throughout for all conversions taking an
242 argument and all width and precision arguments, but it may be mixed
243 with "%%" formats, which do not consume an argument.
245 gaps in the numbers of arguments specified using \(aq$\(aq; for example, if
246 arguments 1 and 3 are specified, argument 2 must also be specified
247 somewhere in the format string.
249 For some numeric conversions a radix character ("decimal point") or
250 thousands' grouping character is used.
251 The actual character used
256 uses \(aq.\(aq as radix character, and does not have a grouping character.
261 printf("%\(aq.2f", 1234567.89);
265 results in "1234567.89" in the POSIX locale, in "1234567,89" in the
266 nl_NL locale, and in "1.234.567,89" in the da_DK locale.
268 The character % is followed by zero or more of the following flags:
271 The value should be converted to an "alternate form".
274 conversions, the first character of the output string is made zero
275 (by prefixing a 0 if it was not zero already).
280 conversions, a nonzero result has the string "0x" (or "0X" for
282 conversions) prepended to it.
293 conversions, the result will always contain a decimal point, even if no
294 digits follow it (normally, a decimal point appears in the results of those
295 conversions only if a digit follows).
300 conversions, trailing zeros are not removed from the result as they would
302 For other conversions, the result is undefined.
305 The value should be zero padded.
322 conversions, the converted value is padded on the left with zeros rather
328 flags both appear, the
331 If a precision is given with a numeric conversion
342 For other conversions, the behavior is undefined.
345 The converted value is to be left adjusted on the field boundary.
346 (The default is right justification.)
347 The converted value is padded on the right with blanks, rather
348 than on the left with blanks or zeros.
356 (a space) A blank should be left before a positive number
357 (or empty string) produced by a signed conversion.
360 A sign (+ or \-) should always be placed before a number produced by a signed
362 By default, a sign is used only for negative numbers.
365 overrides a space if both are used.
367 The five flag characters above are defined in the C99 standard.
368 The Single UNIX Specification specifies one further flag character.
371 For decimal conversion
379 the output is to be grouped with thousands' grouping characters
380 if the locale information indicates any.
381 Note that many versions of
383 cannot parse this option and will issue a warning.
385 include \fI%\(aqF\fP, but SUSv3 added it.)
387 glibc 2.2 adds one further flag character.
390 For decimal integer conversion
394 the output uses the locale's alternative output digits, if any.
395 For example, since glibc 2.2.3 this will give Arabic-Indic digits
396 in the Persian ("fa_IR") locale.
397 .\" outdigits keyword in locale file
399 An optional decimal digit string (with nonzero first digit) specifying
400 a minimum field width.
401 If the converted value has fewer characters
402 than the field width, it will be padded with spaces on the left
403 (or right, if the left-adjustment flag has been given).
404 Instead of a decimal digit string one may write "*" or "*m$"
405 (for some decimal integer \fIm\fP) to specify that the field width
406 is given in the next argument, or in the \fIm\fP-th argument, respectively,
407 which must be of type
409 A negative field width is taken as a \(aq\-\(aq flag followed by a
410 positive field width.
411 In no case does a nonexistent or small field width cause truncation of a
412 field; if the result of a conversion is wider than the field width, the
413 field is expanded to contain the conversion result.
415 An optional precision, in the form of a period (\(aq.\(aq) followed by an
416 optional decimal digit string.
417 Instead of a decimal digit string one may write "*" or "*m$"
418 (for some decimal integer \fIm\fP) to specify that the precision
419 is given in the next argument, or in the \fIm\fP-th argument, respectively,
420 which must be of type
422 If the precision is given as just \(aq.\(aq, the precision is taken to
424 A negative precision is taken as if the precision were omitted.
425 This gives the minimum number of digits to appear for
433 conversions, the number of digits to appear after the radix character for
441 conversions, the maximum number of significant digits for
445 conversions, or the maximum number of characters to be printed from a
452 Here, "integer conversion" stands for
463 A following integer conversion corresponds to a
467 argument, or a following
469 conversion corresponds to a pointer to a
474 A following integer conversion corresponds to a
477 .I unsigned short int
478 argument, or a following
480 conversion corresponds to a pointer to a
485 (ell) A following integer conversion corresponds to a
489 argument, or a following
491 conversion corresponds to a pointer to a
493 argument, or a following
495 conversion corresponds to a
497 argument, or a following
499 conversion corresponds to a pointer to
505 A following integer conversion corresponds to a
508 .I unsigned long long int
509 argument, or a following
511 conversion corresponds to a pointer to a
526 conversion corresponds to a
529 (C99 allows %LF, but SUSv2 does not.)
532 .\" ("quad". 4.4BSD and Linux libc5 only.
534 This is a synonym for
538 A following integer conversion corresponds to an
542 argument, or a following
544 conversion corresponds to a pointer to an
549 A following integer conversion corresponds to a
553 argument, or a following
555 conversion corresponds to a pointer to a
560 .\" with this meaning.
564 A following integer conversion corresponds to a
566 argument, or a following
568 conversion corresponds to a pointer to a
572 SUSv3 specifies all of the above.
573 SUSv2 specified only the length modifiers
601 .SS Conversion specifiers
602 A character that specifies the type of conversion to be applied.
603 The conversion specifiers and their meanings are:
608 argument is converted to signed decimal notation.
609 The precision, if any, gives the minimum number of digits
610 that must appear; if the converted value requires fewer digits, it is
611 padded on the left with zeros.
612 The default precision is 1.
613 When 0 is printed with an explicit precision 0, the output is empty.
615 .BR o ", " u ", " x ", " X
618 argument is converted to unsigned octal
622 or unsigned hexadecimal
631 conversions; the letters
636 The precision, if any, gives the minimum number of digits
637 that must appear; if the converted value requires fewer digits, it is
638 padded on the left with zeros.
639 The default precision is 1.
640 When 0 is printed with an explicit precision 0, the output is empty.
645 argument is rounded and converted in the style
646 .RB [\-]d \&. ddd e \(+-dd
647 where there is one digit before the decimal-point character and the number
648 of digits after it is equal to the precision; if the precision is missing,
649 it is taken as 6; if the precision is zero, no decimal-point character
653 conversion uses the letter
657 to introduce the exponent.
658 The exponent always contains at least two
659 digits; if the value is zero, the exponent is 00.
664 argument is rounded and converted to decimal notation in the style
666 where the number of digits after the decimal-point character is equal to
667 the precision specification.
668 If the precision is missing, it is taken as
669 6; if the precision is explicitly zero, no decimal-point character appears.
670 If a decimal point appears, at least one digit appears before it.
672 (SUSv2 does not know about
674 and says that character string representations for infinity and NaN
675 may be made available.
676 SUSv3 adds a specification for
678 The C99 standard specifies "[\-]inf" or "[\-]infinity"
679 for infinity, and a string starting with "nan" for NaN, in the case of
681 conversion, and "[\-]INF" or "[\-]INFINITY" or "NAN*" in the case of
688 argument is converted in style
699 The precision specifies the number of significant digits.
700 If the precision is missing, 6 digits are given; if the precision is zero,
704 is used if the exponent from its conversion is less than \-4 or greater
705 than or equal to the precision.
706 Trailing zeros are removed from the
707 fractional part of the result; a decimal point appears only if it is
708 followed by at least one digit.
711 (C99; not in SUSv2, but added in SUSv3)
716 argument is converted to hexadecimal notation (using the letters abcdef)
718 .RB [\-] 0x h \&. hhhh p \(+-;
721 conversion the prefix
723 the letters ABCDEF, and the exponent separator
726 There is one hexadecimal digit before the decimal point,
727 and the number of digits after it is equal to the precision.
728 The default precision suffices for an exact representation of the value
729 if an exact representation in base 2 exists
730 and otherwise is sufficiently large to distinguish values of type
732 The digit before the decimal point is unspecified for nonnormalized
733 numbers, and nonzero but otherwise unspecified for normalized numbers.
738 modifier is present, the
740 argument is converted to an
741 .IR "unsigned char" ,
742 and the resulting character is written.
745 modifier is present, the
747 (wide character) argument is converted to a multibyte sequence by a call
750 function, with a conversion state starting in the initial state, and the
751 resulting multibyte string is written.
756 modifier is present: The
758 argument is expected to be a pointer to an array of character type (pointer
760 Characters from the array are written up to (but not
761 including) a terminating null byte (\(aq\\0\(aq);
762 if a precision is specified, no more than the number specified
764 If a precision is given, no null byte need be present;
765 if the precision is not specified, or is greater than the size of the
766 array, the array must contain a terminating null byte.
770 modifier is present: The
771 .I "const wchar_t\ *"
772 argument is expected to be a pointer to an array of wide characters.
773 Wide characters from the array are converted to multibyte characters
774 (each by a call to the
776 function, with a conversion state starting in the initial state before
777 the first wide character), up to and including a terminating null
779 The resulting multibyte characters are written up to
780 (but not including) the terminating null byte.
782 specified, no more bytes than the number specified are written, but
783 no partial multibyte characters are written.
784 Note that the precision
785 determines the number of
787 written, not the number of
790 .IR "screen positions" .
791 The array must contain a terminating null wide character, unless a
792 precision is given and it is so small that the number of bytes written
793 exceeds it before the end of the array is reached.
796 (Not in C99 or C11, but in SUSv2, SUSv3, and SUSv4.)
802 (Not in C99 or C11, but in SUSv2, SUSv3, and SUSv4.)
810 pointer argument is printed in hexadecimal (as if by
816 The number of characters written so far is stored into the integer
817 pointed to by the corresponding argument.
818 That argument shall be an
820 or variant whose size matches the (optionally)
821 supplied integer length modifier.
822 No argument is converted.
823 The behavior is undefined if the conversion specification includes
824 any flags, a field width, or a precision.
829 .IR strerror(errno) .
830 No argument is required.
833 A \(aq%\(aq is written.
834 No argument is converted.
835 The complete conversion
836 specification is \(aq%%\(aq.
838 Upon successful return, these functions return the number of characters
839 printed (excluding the null byte used to end output to strings).
845 do not write more than
847 bytes (including the terminating null byte (\(aq\e0\(aq)).
848 If the output was truncated due to this limit, then the return value
849 is the number of characters (excluding the terminating null byte)
850 which would have been written to the final string if enough space
852 Thus, a return value of
854 or more means that the output was truncated.
855 (See also below under NOTES.)
857 If an output error is encountered, a negative value is returned.
859 For an explanation of the terms used in this section, see
865 Interface Attribute Value
878 T} Thread safety MT-Safe locale
888 POSIX.1-2001, POSIX.1-2008, C89, C99.
892 POSIX.1-2001, POSIX.1-2008, C99.
898 functions were originally GNU extensions that were later standardized
901 Concerning the return value of
903 SUSv2 and C99 contradict each other: when
907 then SUSv2 stipulates an unspecified return value less than 1,
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 POSIX.1-2001 and later align their specification of
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$".
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
940 .\" .IR strerror(errno) .
942 .\" glibc 2.0 adds conversion characters \fBC\fP and \fBS\fP.
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.
947 glibc 2.2 adds the conversion character \fBF\fP with C99 semantics,
948 and the flag character \fBI\fP.
950 Some programs imprudently rely on code such as the following
952 sprintf(buf, "%s some further text", buf);
956 However, the standards explicitly note that the results are undefined
957 if source and destination buffers overlap when calling
963 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=7075
964 Depending on the version of
966 used, and the compiler options employed, calls such as the above will
968 produce the expected results.
970 The glibc implementation of the functions
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.
979 .\" UNIX V7 defines the three routines
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.
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
998 .\" .BR vsnprintf (),
999 .\" and the length modifier q.
1000 .\" FreeBSD also has functions
1003 .\" .BR vasprintf (),
1004 .\" that allocate a buffer large enough for
1006 .\" In glibc there are functions
1010 .\" that print to a file descriptor instead of a stream.
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.
1029 .\" Linux libc4.[45] does not have a
1030 .\" .BR snprintf (),
1031 .\" but provides a libbsd that contains an
1035 .\" that is, one that ignores the
1038 .\" Thus, the use of
1040 .\" with early libc4 leads to serious security problems.
1044 often indicates a bug, since
1046 may contain a % character.
1049 comes from untrusted user input, it may contain \fB%n\fP, causing the
1051 call to write to memory and creating a security hole.
1053 .\" Some floating-point conversions under early libc4
1054 .\" caused memory leaks.
1058 to five decimal places:
1064 fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0));
1068 To print a date and time in the form "Sunday, July 3, 10:02",
1073 are pointers to strings:
1078 fprintf(stdout, "%s, %s %d, %.2d:%.2d\en",
1079 weekday, month, day, hour, min);
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:
1090 fprintf(stdout, format,
1091 weekday, month, day, hour, min);
1097 depends on locale, and may permute the arguments.
1102 "%1$s, %3$d. %2$s, %4$d:%5$.2d\en"
1106 one might obtain "Sonntag, 3. Juli, 10:02".
1108 To allocate a sufficiently large string and print into it
1109 (code correct for both glibc 2.0 and glibc 2.1):
1117 make_message(const char *fmt, ...)
1123 /* Determine required size */
1126 size = vsnprintf(p, size, fmt, ap);
1132 size++; /* For '\\0' */
1138 size = vsnprintf(p, size, fmt, ap);
1149 If truncation occurs in glibc versions prior to 2.0.6, this is treated as an
1150 error instead of being handled gracefully.