1 .\" Copyright (c) 1999 Andries Brouwer (aeb@cwi.nl)
3 .\" This is free documentation; you can redistribute it and/or
4 .\" modify it under the terms of the GNU General Public License as
5 .\" published by the Free Software Foundation; either version 2 of
6 .\" the License, or (at your option) any later version.
8 .\" The GNU General Public License's references to "object code"
9 .\" and "executables" are to be interpreted as the output of any
10 .\" document formatting or typesetting system, including
11 .\" intermediate and printed output.
13 .\" This manual is distributed in the hope that it will be useful,
14 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
15 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 .\" GNU General Public License for more details.
18 .\" You should have received a copy of the GNU General Public
19 .\" License along with this manual; if not, write to the Free
20 .\" Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111,
24 .\" Earlier versions of this page influenced the present text.
25 .\" It was derived from a Berkeley page with version
26 .\" @(#)printf.3 6.14 (Berkeley) 7/30/91
27 .\" converted for Linux by faith@cs.unc.edu, updated by
28 .\" Helmut.Geyer@iwr.uni-heidelberg.de, agulbra@troll.no and Bruno Haible.
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 2000-10-16 "Linux Manpage" "Linux Programmer's Manual"
36 printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf \- formatted output conversion
40 .BI "int printf(const char *" format ", ...);"
42 .BI "int fprintf(FILE *" stream ", const char *" format ", ...);"
44 .BI "int sprintf(char *" str ", const char *" format ", ...);"
46 .BI "int snprintf(char *" str ", size_t " size ", const char *" format ", ...);"
48 .B #include <stdarg.h>
50 .BI "int vprintf(const char *" format ", va_list " ap );
52 .BI "int vfprintf(FILE *" stream ", const char *" format ", va_list " ap );
54 .BI "int vsprintf(char *" str ", const char *" format ", va_list " ap );
56 .BI "int vsnprintf(char *" str ", size_t " size ", const char *" format ", va_list " ap );
60 family produce output according to a
62 as described below. The functions
68 the standard output stream;
72 write output to the given output
79 write to the character string
87 are equivalent to the functions
92 respectively, except that they are called with a va_list instead
93 of a variable number of arguments. These functions do not call the
95 macro. Consequently, the value of
97 is undefined after the call. The application should call
101 These eight functions write the output under the control of a
103 string that specifies how subsequent arguments (or arguments accessed via
104 the variable-length argument facilities of
106 are converted for output.
108 Upon successful return, these functions return the number of characters
109 printed (not including the trailing '\e0' used to end output to strings).
111 .BR snprintf " and " vsnprintf
112 do not write more than
114 bytes (including the trailing '\e0').
115 If the output was truncated due to this limit then the return value
116 is the number of characters (not including the trailing '\e0')
117 which would have been written to the final string if enough space
118 had been available. Thus, a return value of
120 or more means that the output was truncated. (See also below
122 If an output error is encountered, a negative value is returned.
123 .SS "Format of the format string"
124 The format string is a character string, beginning and ending
125 in its initial shift state, if any.
126 The format string is composed of zero or more directives: ordinary
129 which are copied unchanged to the output stream;
130 and conversion specifications, each of which results in fetching zero or
131 more subsequent arguments. Each conversion specification is introduced by
135 .IR "conversion specifier" .
136 In between there may be (in this order) zero or more
143 .IR "length modifier" .
145 The arguments must correspond properly (after type promotion) with the
146 conversion specifier. By default, the arguments are used in the order
147 given, where each `*' and each conversion specifier asks for the next
148 argument (and it is an error if insufficiently many arguments are given).
149 One can also specify explicitly which argument is taken,
150 at each place where an argument is required, by writing `%m$' instead
151 of `%' and `*m$' instead of `*', where the decimal integer m denotes
152 the position in the argument list of the desired argument, indexed starting
156 printf("%*d", width, num);
162 printf("%2$*1$d", width, num);
165 are equivalent. The second style allows repeated references to the
166 same argument. The C99 standard does not include the style using `$',
167 which comes from the Single Unix Specification. If the style using
168 `$' is used, it must be used throughout for all conversions taking an
169 argument and all width and precision arguments, but it may be mixed
170 with `%%' formats which do not consume an argument. There may be no
171 gaps in the numbers of arguments specified using `$'; for example, if
172 arguments 1 and 3 are specified, argument 2 must also be specified
173 somewhere in the format string.
175 For some numeric conversions a radix character (`decimal point') or
176 thousands' grouping character is used. The actual character used
177 depends on the LC_NUMERIC part of the locale. The POSIX locale
178 uses `.' as radix character, and does not have a grouping character.
182 printf("%'.2f", 1234567.89);
185 results in `1234567.89' in the POSIX locale, in `1234567,89' in the
186 nl_NL locale, and in `1.234.567,89' in the da_DK locale.
187 .SS "The flag characters"
188 The character % is followed by zero or more of the following flags:
191 The value should be converted to an ``alternate form''.
194 conversions, the first character of the output string is made zero
195 (by prefixing a 0 if it was not zero already).
200 conversions, a non\-zero result has the string `0x' (or `0X' for
202 conversions) prepended to it. For
212 conversions, the result will always contain a decimal point, even if no
213 digits follow it (normally, a decimal point appears in the results of those
214 conversions only if a digit follows). For
218 conversions, trailing zeros are not removed from the result as they would
220 For other conversions, the result is undefined.
223 The value should be zero padded. For
239 conversions, the converted value is padded on the left with zeros rather
245 flags both appear, the
248 If a precision is given with a numeric conversion
259 For other conversions, the behavior is undefined.
262 The converted value is to be left adjusted on the field boundary.
263 (The default is right justification.) Except for
265 conversions, the converted value is padded on the right with blanks, rather
266 than on the left with blanks or zeros. A
273 (a space) A blank should be left before a positive number
274 (or empty string) produced by a signed conversion.
277 A sign (+ or -) always be placed before a number produced by a signed
278 conversion. By default a sign is used only for negative numbers. A
280 overrides a space if both are used.
282 The five flag characters above are defined in the C standard.
283 The SUSv2 specifies one further flag character.
286 For decimal conversion
294 the output is to be grouped with thousands' grouping characters
295 if the locale information indicates any. Note that many versions of
297 cannot parse this option and will issue a warning. SUSv2 does not
300 glibc 2.2 adds one further flag character.
303 For decimal integer conversion
307 the output uses the locale's alternative output digits, if any.
308 For example, since glibc 2.2.3 this will give Arabic-Indic digits
309 in the Persian (`fa_IR') locale.
310 .\" outdigits keyword in locale file
311 .SS "The field width"
312 An optional decimal digit string (with non-zero first digit) specifying
313 a minimum field width. If the converted value has fewer characters
314 than the field width, it will be padded with spaces on the left
315 (or right, if the left-adjustment flag has been given).
316 Instead of a decimal digit string one may write `*' or `*m$'
317 (for some decimal integer m) to specify that the field width
318 is given in the next argument, or in the m-th argument, respectively,
319 which must be of type
321 A negative field width is taken as a `-' flag followed by a
322 positive field width.
323 In no case does a non-existent or small field width cause truncation of a
324 field; if the result of a conversion is wider than the field width, the
325 field is expanded to contain the conversion result.
327 An optional precision, in the form of a period (`\&.') followed by an
328 optional decimal digit string.
329 Instead of a decimal digit string one may write `*' or `*m$'
330 (for some decimal integer m) to specify that the precision
331 is given in the next argument, or in the m-th argument, respectively,
332 which must be of type
334 If the precision is given as just `.', or the precision is negative,
335 the precision is taken to be zero.
336 This gives the minimum number of digits to appear for
344 conversions, the number of digits to appear after the radix character for
352 conversions, the maximum number of significant digits for
356 conversions, or the maximum number of characters to be printed from a
362 .SS "The length modifier"
363 Here, `integer conversion' stands for
374 A following integer conversion corresponds to a
378 argument, or a following
380 conversion corresponds to a pointer to a
385 A following integer conversion corresponds to a
388 .I unsigned short int
389 argument, or a following
391 conversion corresponds to a pointer to a
396 (ell) A following integer conversion corresponds to a
400 argument, or a following
402 conversion corresponds to a pointer to a
404 argument, or a following
406 conversion corresponds to a
408 argument, or a following
410 conversion corresponds to a pointer to
416 A following integer conversion corresponds to a
419 .I unsigned long long int
420 argument, or a following
422 conversion corresponds to a pointer to a
437 conversion corresponds to a
440 (C99 allows %LF, but SUSv2 does not.)
443 (`quad'. BSD 4.4 and Linux libc5 only. Don't use.)
444 This is a synonym for
448 A following integer conversion corresponds to an
455 A following integer conversion corresponds to a
459 argument. (Linux libc5 has
461 with this meaning. Don't use it.)
464 A following integer conversion corresponds to a
468 The SUSv2 only knows about the length modifiers
497 .SS "The conversion specifier"
498 A character that specifies the type of conversion to be applied.
499 The conversion specifiers and their meanings are:
504 argument is converted to signed decimal notation.
505 The precision, if any, gives the minimum number of digits
506 that must appear; if the converted value requires fewer digits, it is
507 padded on the left with zeros. The default precision is 1.
508 When 0 is printed with an explicit precision 0, the output is empty.
513 argument is converted to unsigned octal
517 or unsigned hexadecimal
521 notation. The letters
525 conversions; the letters
529 conversions. The precision, if any, gives the minimum number of digits
530 that must appear; if the converted value requires fewer digits, it is
531 padded on the left with zeros. The default precision is 1.
532 When 0 is printed with an explicit precision 0, the output is empty.
537 argument is rounded and converted in the style
538 .if \w'\*(Pm'=0 .ds Pm \(+-
539 .BR "" [\-]d \&. ddd e \\*(Pmdd
540 where there is one digit before the decimal\-point character and the number
541 of digits after it is equal to the precision; if the precision is missing,
542 it is taken as 6; if the precision is zero, no decimal\-point character
545 conversion uses the letter
549 to introduce the exponent. The exponent always contains at least two
550 digits; if the value is zero, the exponent is 00.
555 argument is rounded and converted to decimal notation in the style
556 .BR "" [\-]ddd \&. ddd,
557 where the number of digits after the decimal\-point character is equal to
558 the precision specification. If the precision is missing, it is taken as
559 6; if the precision is explicitly zero, no decimal\-point character appears.
560 If a decimal point appears, at least one digit appears before it.
562 (The SUSv2 does not know about
564 and says that character string representations for infinity and NaN
565 may be made available. The C99 standard specifies `[-]inf' or `[-]infinity'
566 for infinity, and a string starting with `nan' for NaN, in the case of
568 conversion, and `[-]INF' or `[-]INFINITY' or `NAN*' in the case of
575 argument is converted in style
585 conversions). The precision specifies the number of significant digits.
586 If the precision is missing, 6 digits are given; if the precision is zero,
587 it is treated as 1. Style
589 is used if the exponent from its conversion is less than \-4 or greater
590 than or equal to the precision. Trailing zeros are removed from the
591 fractional part of the result; a decimal point appears only if it is
592 followed by at least one digit.
595 (C99; not in SUSv2) For
599 argument is converted to hexadecimal notation (using the letters abcdef)
601 .BR "" [-] 0x h \&. hhhh p \\*(Pmd;
604 conversion the prefix
606 the letters ABCDEF, and the exponent separator
609 There is one hexadecimal digit before the decimal point,
610 and the number of digits after it is equal to the precision.
611 The default precision suffices for an exact representation of the value
612 if an exact representation in base 2 exists
613 and otherwise is sufficiently large to distinguish values of type
615 The digit before the decimal point is unspecified for non-normalized
616 numbers, and non-zero but otherwise unspecified for normalized numbers.
621 modifier is present, the
623 argument is converted to an
624 .IR "unsigned char" ,
625 and the resulting character is written.
628 modifier is present, the
630 (wide character) argument is converted to a multibyte sequence by a call
633 function, with a conversion state starting in the initial state, and the
634 resulting multibyte string is written.
639 modifier is present: The
641 argument is expected to be a pointer to an array of character type (pointer
642 to a string). Characters from the array are written up to (but not
643 including) a terminating
645 character; if a precision is specified, no more than the number specified
646 are written. If a precision is given, no null character need be present;
647 if the precision is not specified, or is greater than the size of the
648 array, the array must contain a terminating
654 modifier is present: The
656 argument is expected to be a pointer to an array of wide characters.
657 Wide characters from the array are converted to multibyte characters
658 (each by a call to the
660 function, with a conversion state starting in the initial state before
661 the first wide character), up to and including a terminating null
662 wide character. The resulting multibyte characters are written up to
663 (but not including) the terminating null byte. If a precision is
664 specified, no more bytes than the number specified are written, but
665 no partial multibyte characters are written. Note that the precision
666 determines the number of
668 written, not the number of
671 .IR "screen positions" .
672 The array must contain a terminating null wide character, unless a
673 precision is given and it is so small that the number of bytes written
674 exceeds it before the end of the array is reached.
677 (Not in C99, but in SUSv2.)
683 (Not in C99, but in SUSv2.)
691 pointer argument is printed in hexadecimal (as if by
697 The number of characters written so far is stored into the integer
700 (or variant) pointer argument. No argument is converted.
703 A `%' is written. No argument is converted. The complete conversion
704 specification is `%%'.
708 .if \w'\*(Pi'=0 .ds Pi pi
709 To print \*(Pi to five decimal places:
714 fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0));
718 To print a date and time in the form `Sunday, July 3, 10:02',
723 are pointers to strings:
727 fprintf(stdout, "%s, %s %d, %.2d:%.2d\en",
728 weekday, month, day, hour, min);
732 Many countries use the day-month-year order.
733 Hence, an internationalized version must be able to print
734 the arguments in an order specified by the format:
738 fprintf(stdout, format,
739 weekday, month, day, hour, min);
744 depends on locale, and may permute the arguments. With the value
747 "%1$s, %3$d. %2$s, %4$d:%5$.2d\en"
750 one might obtain `Sonntag, 3. Juli, 10:02'.
752 To allocate a sufficiently large string and print into it
753 (code correct for both glibc 2.0 and glibc 2.1):
761 make_message(const char *fmt, ...) {
762 /* Guess we need no more than 100 bytes. */
767 if ((p = malloc (size)) == NULL)
771 /* Try to print in the allocated space. */
773 n = vsnprintf (p, size, fmt, ap);
775 /* If that worked, return the string. */
776 if (n > -1 && n < size)
778 /* Else try again with more space. */
779 if (n > -1) /* glibc 2.1 */
780 size = n+1; /* precisely what is needed */
782 size *= 2; /* twice the old size */
783 if ((np = realloc (p, size)) == NULL) {
795 The glibc implementation of the functions
799 conforms to the C99 standard, i.e., behaves as described above,
800 since glibc version 2.1. Until glibc 2.0.6 they would return \-1
801 when the output was truncated.
811 functions conform to ANSI X3.159-1989 (``ANSI C'') and ISO/IEC 9899:1999
817 functions conform to ISO/IEC 9899:1999.
819 Concerning the return value of
821 the SUSv2 and the C99 standard contradict each other: when
825 then SUSv2 stipulates an unspecified return value less than 1,
828 to be NULL in this case, and gives the return value (as always)
829 as the number of characters that would have been written in case
830 the output string has been large enough.
832 Linux libc4 knows about the five C standard flags.
833 It knows about the length modifiers h,l,L, and the conversions
834 cdeEfFgGinopsuxX, where F is a synonym for f.
835 Additionally, it accepts D,O,U as synonyms for ld,lo,lu.
836 (This is bad, and caused serious bugs later, when
837 support for %D disappeared.) No locale-dependent radix character,
838 no thousands' separator, no NaN or infinity, no %m$ and *m$.
840 Linux libc5 knows about the five C standard flags and the ' flag,
842 It knows about the length modifiers h,l,L,Z,q, but accepts L and q
843 both for long doubles and for long long integers (this is a bug).
844 It no longer recognizes FDOU, but adds a new conversion character
847 .IR strerror(errno) .
849 glibc 2.0 adds conversion characters C and S.
851 glibc 2.1 adds length modifiers hh,j,t,z and conversion characters a,A.
853 glibc 2.2 adds the conversion character F with C99 semantics, and the
856 Unix V7 defines the three routines
860 and has the flag -, the width or precision *, the length modifier l,
861 and the conversions doxfegcsu, and also D,O,U,X as synonyms for ld,lo,lu,lx.
862 This is still true for BSD 2.9.1, but BSD 2.10 has the flags
863 #, + and <space> and no longer mentions D,O,U,X.
868 and warns not to use D,O,U,X.
869 BSD 4.3 Reno has the flag 0, the length modifiers h and L,
870 and the conversions n, p, E, G, X (with current meaning)
871 and deprecates D,O,U.
872 BSD 4.4 introduces the functions
876 and the length modifier q.
877 FreeBSD also has functions
881 that allocate a buffer large enough for
883 In glibc there are functions
887 that print to a file descriptor instead of a stream.
893 assume an arbitrarily long string, callers must be careful not to overflow
894 the actual space; this is often impossible to assure. Note that the length
895 of the strings produced is locale-dependent and difficult to predict.
905 Linux libc4.[45] does not have a
907 but provides a libbsd that contains an
911 i.e., one that ignores the
916 with early libc4 leads to serious security problems.
920 often indicates a bug, since
922 may contain a % character. If
924 comes from untrusted user input, it may contain %n, causing the
926 call to write to memory and creating a security hole.
928 .\" Some floating point conversions under early libc4
929 .\" caused memory leaks.