]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/printf.3
Updated CONFORMING TO section
[thirdparty/man-pages.git] / man3 / printf.3
CommitLineData
fea681da
MK
1.\" Copyright (c) 1999 Andries Brouwer (aeb@cwi.nl)
2.\"
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.
7.\"
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.
12.\"
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.
17.\"
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,
21.\" USA.
22.\"
23.\"
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.
29.\"
30.\" 1999-11-25 aeb - Rewritten, using SUSv2 and C99.
31.\" 2000-07-26 jsm28@hermes.cam.ac.uk - three small fixes
32.\" 2000-10-16 jsm28@hermes.cam.ac.uk - more fixes
33.\"
34.TH PRINTF 3 2000-10-16 "Linux Manpage" "Linux Programmer's Manual"
35.SH NAME
36printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf \- formatted output conversion
37.SH SYNOPSIS
38.B #include <stdio.h>
39.sp
40.BI "int printf(const char *" format ", ...);"
41.br
42.BI "int fprintf(FILE *" stream ", const char *" format ", ...);"
43.br
44.BI "int sprintf(char *" str ", const char *" format ", ...);"
45.br
46.BI "int snprintf(char *" str ", size_t " size ", const char *" format ", ...);"
47.sp
48.B #include <stdarg.h>
49.sp
50.BI "int vprintf(const char *" format ", va_list " ap );
51.br
52.BI "int vfprintf(FILE *" stream ", const char *" format ", va_list " ap );
53.br
54.BI "int vsprintf(char *" str ", const char *" format ", va_list " ap );
55.br
56.BI "int vsnprintf(char *" str ", size_t " size ", const char *" format ", va_list " ap );
57.SH DESCRIPTION
58The functions in the
e511ffb6 59.BR printf ()
fea681da
MK
60family produce output according to a
61.I format
62as described below. The functions
e511ffb6 63.BR printf ()
fea681da 64and
e511ffb6 65.BR vprintf ()
fea681da
MK
66write output to
67.IR stdout ,
68the standard output stream;
e511ffb6 69.BR fprintf ()
fea681da 70and
e511ffb6 71.BR vfprintf ()
fea681da
MK
72write output to the given output
73.IR stream ;
e511ffb6
MK
74.BR sprintf (),
75.BR snprintf (),
76.BR vsprintf ()
fea681da 77and
e511ffb6 78.BR vsnprintf ()
fea681da
MK
79write to the character string
80.IR str .
81.PP
82The functions
e511ffb6
MK
83.BR vprintf (),
84.BR vfprintf (),
85.BR vsprintf (),
86.BR vsnprintf ()
fea681da 87are equivalent to the functions
e511ffb6
MK
88.BR printf (),
89.BR fprintf (),
90.BR sprintf (),
91.BR snprintf (),
fea681da
MK
92respectively, except that they are called with a va_list instead
93of a variable number of arguments. These functions do not call the
94.I va_end
95macro. Consequently, the value of
96.I ap
97is undefined after the call. The application should call
98.I va_end(ap)
99itself afterwards.
100.PP
101These eight functions write the output under the control of a
102.I format
103string that specifies how subsequent arguments (or arguments accessed via
104the variable-length argument facilities of
105.BR stdarg (3))
106are converted for output.
107.SS "Return value"
108Upon successful return, these functions return the number of characters
109printed (not including the trailing '\e0' used to end output to strings).
110The functions
6e1ffb98
MK
111.BR snprintf ()
112and
113.BR vsnprintf ()
fea681da
MK
114do not write more than
115.I size
116bytes (including the trailing '\e0').
117If the output was truncated due to this limit then the return value
118is the number of characters (not including the trailing '\e0')
119which would have been written to the final string if enough space
120had been available. Thus, a return value of
121.I size
122or more means that the output was truncated. (See also below
123under NOTES.)
124If an output error is encountered, a negative value is returned.
125.SS "Format of the format string"
126The format string is a character string, beginning and ending
127in its initial shift state, if any.
128The format string is composed of zero or more directives: ordinary
129characters (not
130.BR % ),
131which are copied unchanged to the output stream;
132and conversion specifications, each of which results in fetching zero or
133more subsequent arguments. Each conversion specification is introduced by
134the character
135.BR % ,
136and ends with a
137.IR "conversion specifier" .
138In between there may be (in this order) zero or more
139.IR flags ,
140an optional minimum
141.IR "field width" ,
142an optional
143.I precision
144and an optional
145.IR "length modifier" .
146
147The arguments must correspond properly (after type promotion) with the
148conversion specifier. By default, the arguments are used in the order
149given, where each `*' and each conversion specifier asks for the next
150argument (and it is an error if insufficiently many arguments are given).
151One can also specify explicitly which argument is taken,
152at each place where an argument is required, by writing `%m$' instead
153of `%' and `*m$' instead of `*', where the decimal integer m denotes
154the position in the argument list of the desired argument, indexed starting
155from 1. Thus,
156.RS
157.nf
158 printf("%*d", width, num);
159.fi
160.RE
161and
162.RS
163.nf
164 printf("%2$*1$d", width, num);
165.fi
166.RE
167are equivalent. The second style allows repeated references to the
168same argument. The C99 standard does not include the style using `$',
169which comes from the Single Unix Specification. If the style using
170`$' is used, it must be used throughout for all conversions taking an
171argument and all width and precision arguments, but it may be mixed
172with `%%' formats which do not consume an argument. There may be no
173gaps in the numbers of arguments specified using `$'; for example, if
174arguments 1 and 3 are specified, argument 2 must also be specified
175somewhere in the format string.
176
177For some numeric conversions a radix character (`decimal point') or
178thousands' grouping character is used. The actual character used
179depends on the LC_NUMERIC part of the locale. The POSIX locale
180uses `.' as radix character, and does not have a grouping character.
181Thus,
182.RS
183.nf
184 printf("%'.2f", 1234567.89);
185.fi
186.RE
187results in `1234567.89' in the POSIX locale, in `1234567,89' in the
188nl_NL locale, and in `1.234.567,89' in the da_DK locale.
189.SS "The flag characters"
190The character % is followed by zero or more of the following flags:
191.TP
192.B #
193The value should be converted to an ``alternate form''.
194For
195.BR o
196conversions, the first character of the output string is made zero
197(by prefixing a 0 if it was not zero already).
198For
199.B x
200and
201.B X
202conversions, a non\-zero result has the string `0x' (or `0X' for
203.B X
204conversions) prepended to it. For
205.BR a ,
206.BR A ,
207.BR e ,
208.BR E ,
209.BR f ,
210.BR F ,
211.BR g ,
212and
213.B G
214conversions, the result will always contain a decimal point, even if no
215digits follow it (normally, a decimal point appears in the results of those
216conversions only if a digit follows). For
217.B g
218and
219.B G
220conversions, trailing zeros are not removed from the result as they would
221otherwise be.
222For other conversions, the result is undefined.
223.TP
224.B \&0
225The value should be zero padded. For
226.BR d ,
227.BR i ,
228.BR o ,
229.BR u ,
230.BR x ,
231.BR X ,
232.BR a ,
233.BR A ,
234.BR e ,
235.BR E ,
236.BR f ,
237.BR F ,
238.BR g ,
239and
240.B G
241conversions, the converted value is padded on the left with zeros rather
242than blanks.
243If the
244.B \&0
245and
246.B \-
247flags both appear, the
248.B \&0
249flag is ignored.
250If a precision is given with a numeric conversion
251.BR "" ( d ,
252.BR i ,
253.BR o ,
254.BR u ,
255.BR x ,
256and
257.BR X ),
258the
259.B \&0
260flag is ignored.
261For other conversions, the behavior is undefined.
262.TP
263.B \-
264The converted value is to be left adjusted on the field boundary.
265(The default is right justification.) Except for
266.B n
267conversions, the converted value is padded on the right with blanks, rather
268than on the left with blanks or zeros. A
269.B \-
270overrides a
271.B \&0
272if both are given.
273.TP
274.B ' '
275(a space) A blank should be left before a positive number
276(or empty string) produced by a signed conversion.
277.TP
278.B +
4897420e 279A sign (+ or \-) should always be placed before a number produced by a signed
fea681da
MK
280conversion. By default a sign is used only for negative numbers. A
281.B +
282overrides a space if both are used.
283.PP
284The five flag characters above are defined in the C standard.
285The SUSv2 specifies one further flag character.
286.TP
287.B '
288For decimal conversion
289.BR "" ( i ,
290.BR d ,
291.BR u ,
292.BR f ,
293.BR F ,
294.BR g ,
295.BR G )
296the output is to be grouped with thousands' grouping characters
297if the locale information indicates any. Note that many versions of
8478ee02 298.BR gcc (1)
fea681da
MK
299cannot parse this option and will issue a warning. SUSv2 does not
300include %'F.
301.PP
302glibc 2.2 adds one further flag character.
303.TP
304.B I
305For decimal integer conversion
306.BR "" ( i ,
307.BR d ,
308.BR u )
309the output uses the locale's alternative output digits, if any.
310For example, since glibc 2.2.3 this will give Arabic-Indic digits
311in the Persian (`fa_IR') locale.
312.\" outdigits keyword in locale file
313.SS "The field width"
f59a3f19 314An optional decimal digit string (with non-zero first digit) specifying
fea681da
MK
315a minimum field width. If the converted value has fewer characters
316than the field width, it will be padded with spaces on the left
317(or right, if the left-adjustment flag has been given).
318Instead of a decimal digit string one may write `*' or `*m$'
319(for some decimal integer m) to specify that the field width
320is given in the next argument, or in the m-th argument, respectively,
321which must be of type
322.IR int .
8c383102 323A negative field width is taken as a `\-' flag followed by a
fea681da
MK
324positive field width.
325In no case does a non-existent or small field width cause truncation of a
326field; if the result of a conversion is wider than the field width, the
327field is expanded to contain the conversion result.
328.SS "The precision"
329An optional precision, in the form of a period (`\&.') followed by an
330optional decimal digit string.
331Instead of a decimal digit string one may write `*' or `*m$'
332(for some decimal integer m) to specify that the precision
333is given in the next argument, or in the m-th argument, respectively,
334which must be of type
335.IR int .
336If the precision is given as just `.', or the precision is negative,
337the precision is taken to be zero.
338This gives the minimum number of digits to appear for
339.BR d ,
340.BR i ,
341.BR o ,
342.BR u ,
343.BR x ,
344and
345.B X
346conversions, the number of digits to appear after the radix character for
347.BR a ,
348.BR A ,
349.BR e ,
350.BR E ,
351.BR f ,
352and
353.B F
354conversions, the maximum number of significant digits for
355.B g
356and
357.B G
358conversions, or the maximum number of characters to be printed from a
359string for
360.B s
361and
362.B S
363conversions.
364.SS "The length modifier"
365Here, `integer conversion' stands for
366.BR d ,
367.BR i ,
368.BR o ,
369.BR u ,
370.BR x ,
371or
372.BR X
373conversion.
374.TP
375.B hh
376A following integer conversion corresponds to a
377.I signed char
378or
379.I unsigned char
380argument, or a following
381.B n
382conversion corresponds to a pointer to a
383.I signed char
384argument.
385.TP
386.B h
387A following integer conversion corresponds to a
388.I short int
389or
390.I unsigned short int
391argument, or a following
392.B n
393conversion corresponds to a pointer to a
394.I short int
395argument.
396.TP
397.B l
398(ell) A following integer conversion corresponds to a
399.I long int
400or
401.I unsigned long int
402argument, or a following
403.B n
404conversion corresponds to a pointer to a
405.I long int
406argument, or a following
407.B c
408conversion corresponds to a
409.I wint_t
410argument, or a following
411.B s
412conversion corresponds to a pointer to
413.I wchar_t
414argument.
415.TP
416.B ll
417(ell-ell).
418A following integer conversion corresponds to a
419.I long long int
420or
421.I unsigned long long int
422argument, or a following
423.B n
424conversion corresponds to a pointer to a
425.I long long int
426argument.
427.TP
428.BR L
429A following
430.BR a ,
431.BR A ,
432.BR e ,
433.BR E ,
434.BR f ,
435.BR F ,
436.BR g ,
437or
438.B G
439conversion corresponds to a
440.I long double
441argument.
442(C99 allows %LF, but SUSv2 does not.)
443.TP
444.B q
b14d4aa5 445(`quad'. 4.4BSD and Linux libc5 only. Don't use.)
fea681da
MK
446This is a synonym for
447.BR ll .
448.TP
449.B j
450A following integer conversion corresponds to an
451.I intmax_t
452or
453.I uintmax_t
454argument.
455.TP
456.B z
457A following integer conversion corresponds to a
458.I size_t
459or
460.I ssize_t
461argument. (Linux libc5 has
462.B Z
463with this meaning. Don't use it.)
464.TP
465.B t
466A following integer conversion corresponds to a
467.I ptrdiff_t
468argument.
469.PP
470The SUSv2 only knows about the length modifiers
471.B h
472(in
473.BR hd ,
474.BR hi ,
475.BR ho ,
476.BR hx ,
477.BR hX ,
478.BR hn )
479and
480.B l
481(in
482.BR ld ,
483.BR li ,
484.BR lo ,
485.BR lx ,
486.BR lX ,
487.BR ln ,
488.BR lc ,
489.BR ls )
490and
491.B L
492(in
493.BR Le ,
494.BR LE ,
495.BR Lf ,
496.BR Lg ,
497.BR LG ).
498
499.SS "The conversion specifier"
500A character that specifies the type of conversion to be applied.
501The conversion specifiers and their meanings are:
502.TP
503.BR d , i
504The
505.I int
506argument is converted to signed decimal notation.
507The precision, if any, gives the minimum number of digits
508that must appear; if the converted value requires fewer digits, it is
509padded on the left with zeros. The default precision is 1.
510When 0 is printed with an explicit precision 0, the output is empty.
511.TP
512.BR o , u , x , X
513The
514.I "unsigned int"
515argument is converted to unsigned octal
516.BR "" ( o ),
517unsigned decimal
518.BR "" ( u ),
519or unsigned hexadecimal
520.BR "" ( x
521and
522.BR X )
523notation. The letters
524.B abcdef
525are used for
526.B x
527conversions; the letters
528.B ABCDEF
529are used for
530.B X
531conversions. The precision, if any, gives the minimum number of digits
532that must appear; if the converted value requires fewer digits, it is
533padded on the left with zeros. The default precision is 1.
534When 0 is printed with an explicit precision 0, the output is empty.
535.TP
536.BR e , E
537The
538.I double
539argument is rounded and converted in the style
540.if \w'\*(Pm'=0 .ds Pm \(+-
541.BR "" [\-]d \&. ddd e \\*(Pmdd
542where there is one digit before the decimal\-point character and the number
543of digits after it is equal to the precision; if the precision is missing,
544it is taken as 6; if the precision is zero, no decimal\-point character
545appears. An
546.B E
547conversion uses the letter
548.B E
549(rather than
550.BR e )
551to introduce the exponent. The exponent always contains at least two
552digits; if the value is zero, the exponent is 00.
553.TP
554.BR f , F
555The
556.I double
557argument is rounded and converted to decimal notation in the style
558.BR "" [\-]ddd \&. ddd,
559where the number of digits after the decimal\-point character is equal to
560the precision specification. If the precision is missing, it is taken as
5616; if the precision is explicitly zero, no decimal\-point character appears.
562If a decimal point appears, at least one digit appears before it.
563
564(The SUSv2 does not know about
565.B F
566and says that character string representations for infinity and NaN
8c383102 567may be made available. The C99 standard specifies `[\-]inf' or `[\-]infinity'
fea681da
MK
568for infinity, and a string starting with `nan' for NaN, in the case of
569.B f
8c383102 570conversion, and `[\-]INF' or `[\-]INFINITY' or `NAN*' in the case of
fea681da
MK
571.B F
572conversion.)
573.TP
574.BR g , G
575The
576.I double
577argument is converted in style
578.B f
579or
580.B e
581(or
582.B F
583or
584.B E
585for
586.B G
587conversions). The precision specifies the number of significant digits.
588If the precision is missing, 6 digits are given; if the precision is zero,
589it is treated as 1. Style
590.B e
591is used if the exponent from its conversion is less than \-4 or greater
592than or equal to the precision. Trailing zeros are removed from the
593fractional part of the result; a decimal point appears only if it is
594followed by at least one digit.
595.TP
596.BR a , A
597(C99; not in SUSv2) For
598.B a
599conversion, the
600.I double
601argument is converted to hexadecimal notation (using the letters abcdef)
602in the style
8c383102 603.BR "" [\-] 0x h \&. hhhh p \\*(Pmd;
fea681da
MK
604for
605.B A
606conversion the prefix
607.BR 0X ,
608the letters ABCDEF, and the exponent separator
609.B P
610is used.
611There is one hexadecimal digit before the decimal point,
612and the number of digits after it is equal to the precision.
613The default precision suffices for an exact representation of the value
614if an exact representation in base 2 exists
615and otherwise is sufficiently large to distinguish values of type
616.IR double .
617The digit before the decimal point is unspecified for non-normalized
f59a3f19 618numbers, and non-zero but otherwise unspecified for normalized numbers.
fea681da
MK
619.TP
620.B c
621If no
622.B l
623modifier is present, the
624.I int
625argument is converted to an
626.IR "unsigned char" ,
627and the resulting character is written.
628If an
629.B l
630modifier is present, the
631.I wint_t
632(wide character) argument is converted to a multibyte sequence by a call
633to the
e1d6264d 634.BR wcrtomb ()
fea681da
MK
635function, with a conversion state starting in the initial state, and the
636resulting multibyte string is written.
637.TP
638.B s
639If no
640.B l
641modifier is present: The
642.I "const char *"
643argument is expected to be a pointer to an array of character type (pointer
644to a string). Characters from the array are written up to (but not
28d88c17
MK
645including) a terminating null byte ('\\0');
646if a precision is specified, no more than the number specified
647are written. If a precision is given, no null byte need be present;
fea681da 648if the precision is not specified, or is greater than the size of the
28d88c17 649array, the array must contain a terminating null byte.
fea681da
MK
650
651If an
652.B l
653modifier is present: The
654.I "const wchar_t *"
655argument is expected to be a pointer to an array of wide characters.
656Wide characters from the array are converted to multibyte characters
657(each by a call to the
e1d6264d 658.BR wcrtomb ()
fea681da
MK
659function, with a conversion state starting in the initial state before
660the first wide character), up to and including a terminating null
661wide character. The resulting multibyte characters are written up to
662(but not including) the terminating null byte. If a precision is
663specified, no more bytes than the number specified are written, but
664no partial multibyte characters are written. Note that the precision
665determines the number of
666.I bytes
667written, not the number of
668.I wide characters
669or
670.IR "screen positions" .
671The array must contain a terminating null wide character, unless a
672precision is given and it is so small that the number of bytes written
673exceeds it before the end of the array is reached.
674.TP
675.B C
676(Not in C99, but in SUSv2.)
677Synonym for
678.BR lc .
679Don't use.
680.TP
681.B S
682(Not in C99, but in SUSv2.)
683Synonym for
684.BR ls .
685Don't use.
686.TP
687.B p
688The
689.I "void *"
690pointer argument is printed in hexadecimal (as if by
691.B %#x
692or
693.BR %#lx ).
694.TP
695.B n
696The number of characters written so far is stored into the integer
697indicated by the
698.I "int *"
699(or variant) pointer argument. No argument is converted.
c15f96dd
MK
700.TP
701.B m
702(Glibc extension.) Print output of
703.IR strerror(errno) .
704No argument is required.
fea681da
MK
705.TP
706.B %
707A `%' is written. No argument is converted. The complete conversion
708specification is `%%'.
709.PP
9b336505 710.SH EXAMPLE
fea681da
MK
711.br
712.if \w'\*(Pi'=0 .ds Pi pi
713To print \*(Pi to five decimal places:
714.RS
715.nf
716#include <math.h>
717#include <stdio.h>
718fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0));
719.fi
720.RE
721.PP
722To print a date and time in the form `Sunday, July 3, 10:02',
723where
724.I weekday
725and
726.I month
727are pointers to strings:
728.RS
729.nf
730#include <stdio.h>
731fprintf(stdout, "%s, %s %d, %.2d:%.2d\en",
732 weekday, month, day, hour, min);
733.fi
734.RE
735.PP
736Many countries use the day-month-year order.
737Hence, an internationalized version must be able to print
738the arguments in an order specified by the format:
739.RS
740.nf
741#include <stdio.h>
742fprintf(stdout, format,
743 weekday, month, day, hour, min);
744.fi
745.RE
746where
747.I format
748depends on locale, and may permute the arguments. With the value
749.RS
750.nf
751"%1$s, %3$d. %2$s, %4$d:%5$.2d\en"
752.fi
753.RE
754one might obtain `Sonntag, 3. Juli, 10:02'.
755.PP
756To allocate a sufficiently large string and print into it
757(code correct for both glibc 2.0 and glibc 2.1):
758.RS
759.nf
760#include <stdio.h>
761#include <stdlib.h>
762#include <stdarg.h>
898e9a87 763
fea681da
MK
764char *
765make_message(const char *fmt, ...) {
766 /* Guess we need no more than 100 bytes. */
767 int n, size = 100;
898e9a87 768 char *p, *np;
fea681da 769 va_list ap;
898e9a87 770
fea681da
MK
771 if ((p = malloc (size)) == NULL)
772 return NULL;
898e9a87 773
fea681da
MK
774 while (1) {
775 /* Try to print in the allocated space. */
776 va_start(ap, fmt);
777 n = vsnprintf (p, size, fmt, ap);
778 va_end(ap);
779 /* If that worked, return the string. */
2bc2f479 780 if (n > \-1 && n < size)
fea681da
MK
781 return p;
782 /* Else try again with more space. */
2bc2f479 783 if (n > \-1) /* glibc 2.1 */
fea681da
MK
784 size = n+1; /* precisely what is needed */
785 else /* glibc 2.0 */
786 size *= 2; /* twice the old size */
898e9a87
MK
787 if ((np = realloc (p, size)) == NULL) {
788 free(p);
fea681da 789 return NULL;
898e9a87
MK
790 } else {
791 p = np;
792 }
fea681da
MK
793 }
794}
795.fi
796.RE
797
798.SH NOTES
799The glibc implementation of the functions
e511ffb6 800.BR snprintf ()
fea681da 801and
e511ffb6 802.BR vsnprintf ()
fea681da
MK
803conforms to the C99 standard, i.e., behaves as described above,
804since glibc version 2.1. Until glibc 2.0.6 they would return \-1
805when the output was truncated.
806.SH "CONFORMING TO"
807The
e511ffb6
MK
808.BR fprintf (),
809.BR printf (),
810.BR sprintf (),
811.BR vprintf (),
812.BR vfprintf (),
fea681da 813and
e511ffb6 814.BR vsprintf ()
fea681da
MK
815functions conform to ANSI X3.159-1989 (``ANSI C'') and ISO/IEC 9899:1999
816(``ISO C99'').
817The
e511ffb6 818.BR snprintf ()
fea681da 819and
e511ffb6 820.BR vsnprintf ()
fea681da
MK
821functions conform to ISO/IEC 9899:1999.
822.PP
823Concerning the return value of
e511ffb6 824.BR snprintf (),
fea681da 825the SUSv2 and the C99 standard contradict each other: when
e511ffb6 826.BR snprintf ()
fea681da
MK
827is called with
828.IR size =0
829then SUSv2 stipulates an unspecified return value less than 1,
830while C99 allows
831.I str
832to be NULL in this case, and gives the return value (as always)
833as the number of characters that would have been written in case
834the output string has been large enough.
835.PP
836Linux libc4 knows about the five C standard flags.
837It knows about the length modifiers h,l,L, and the conversions
838cdeEfFgGinopsuxX, where F is a synonym for f.
839Additionally, it accepts D,O,U as synonyms for ld,lo,lu.
840(This is bad, and caused serious bugs later, when
841support for %D disappeared.) No locale-dependent radix character,
842no thousands' separator, no NaN or infinity, no %m$ and *m$.
843.PP
844Linux libc5 knows about the five C standard flags and the ' flag,
845locale, %m$ and *m$.
846It knows about the length modifiers h,l,L,Z,q, but accepts L and q
847both for long doubles and for long long integers (this is a bug).
c15f96dd 848It no longer recognizes FDOU, but adds the conversion character
fea681da
MK
849.BR m ,
850which outputs
851.IR strerror(errno) .
852.PP
853glibc 2.0 adds conversion characters C and S.
854.PP
855glibc 2.1 adds length modifiers hh,j,t,z and conversion characters a,A.
856.PP
857glibc 2.2 adds the conversion character F with C99 semantics, and the
858flag character I.
859.SH HISTORY
860Unix V7 defines the three routines
e511ffb6
MK
861.BR printf (),
862.BR fprintf (),
863.BR sprintf (),
c65433e6 864and has the flag \-, the width or precision *, the length modifier l,
fea681da 865and the conversions doxfegcsu, and also D,O,U,X as synonyms for ld,lo,lu,lx.
b14d4aa5 866This is still true for 2.9.1BSD, but 2.10BSD has the flags
fea681da 867#, + and <space> and no longer mentions D,O,U,X.
b14d4aa5 8682.11BSD has
e511ffb6
MK
869.BR vprintf (),
870.BR vfprintf (),
871.BR vsprintf (),
fea681da 872and warns not to use D,O,U,X.
b14d4aa5 8734.3BSD Reno has the flag 0, the length modifiers h and L,
fea681da
MK
874and the conversions n, p, E, G, X (with current meaning)
875and deprecates D,O,U.
b14d4aa5 8764.4BSD introduces the functions
e511ffb6 877.BR snprintf ()
fea681da 878and
e511ffb6 879.BR vsnprintf (),
fea681da
MK
880and the length modifier q.
881FreeBSD also has functions
31e9a9ec 882.BR asprintf ()
fea681da 883and
31e9a9ec 884.BR vasprintf (),
fea681da 885that allocate a buffer large enough for
e511ffb6 886.BR sprintf ().
fea681da 887In glibc there are functions
31e9a9ec 888.BR dprintf ()
fea681da 889and
31e9a9ec 890.BR vdprintf ()
fea681da
MK
891that print to a file descriptor instead of a stream.
892.SH BUGS
893Because
e511ffb6 894.BR sprintf ()
fea681da 895and
e511ffb6 896.BR vsprintf ()
fea681da
MK
897assume an arbitrarily long string, callers must be careful not to overflow
898the actual space; this is often impossible to assure. Note that the length
899of the strings produced is locale-dependent and difficult to predict.
900Use
e511ffb6 901.BR snprintf ()
fea681da 902and
e511ffb6 903.BR vsnprintf ()
fea681da 904instead (or
e1d6264d 905.BR asprintf ()
fea681da
MK
906and
907.BR vasprintf ).
908.PP
909Linux libc4.[45] does not have a
e511ffb6 910.BR snprintf (),
fea681da 911but provides a libbsd that contains an
e511ffb6 912.BR snprintf ()
fea681da 913equivalent to
e511ffb6 914.BR sprintf (),
fea681da
MK
915i.e., one that ignores the
916.I size
917argument.
918Thus, the use of
e511ffb6 919.BR snprintf ()
fea681da
MK
920with early libc4 leads to serious security problems.
921.PP
922Code such as
923.BI printf( foo );
924often indicates a bug, since
925.I foo
926may contain a % character. If
927.I foo
928comes from untrusted user input, it may contain %n, causing the
e511ffb6 929.BR printf ()
fea681da
MK
930call to write to memory and creating a security hole.
931.\" .PP
932.\" Some floating point conversions under early libc4
933.\" caused memory leaks.
934
935.SH "SEE ALSO"
936.BR printf (1),
937.BR asprintf (3),
938.BR dprintf (3),
939.BR scanf (3),
9fcfcba0 940.BR setlocale (3),
fea681da
MK
941.BR wcrtomb (3),
942.BR wprintf (3),
943.BR locale (5)