]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/printf.3
posix_openpt.3: Reword text regarding systems that don't have posix_openpt()
[thirdparty/man-pages.git] / man3 / printf.3
CommitLineData
fea681da
MK
1.\" Copyright (c) 1999 Andries Brouwer (aeb@cwi.nl)
2.\"
2e2d817c
MK
3.\" Earlier versions of this page influenced the present text.
4.\" It was derived from a Berkeley page with version
5.\" @(#)printf.3 6.14 (Berkeley) 7/30/91
6.\" converted for Linux by faith@cs.unc.edu, updated by
7.\" Helmut.Geyer@iwr.uni-heidelberg.de, agulbra@troll.no and Bruno Haible.
8.\"
1dd72f9c 9.\" %%%LICENSE_START(GPLv2+_DOC_FULL)
fea681da
MK
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.
14.\"
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.
19.\"
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.
24.\"
25.\" You should have received a copy of the GNU General Public
c715f741
MK
26.\" License along with this manual; if not, see
27.\" <http://www.gnu.org/licenses/>.
6a8d8745 28.\" %%%LICENSE_END
fea681da 29.\"
fea681da
MK
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.\"
c73595c2 34.TH PRINTF 3 2015-04-19 "GNU" "Linux Programmer's Manual"
fea681da 35.SH NAME
5ffdc2fd 36printf, fprintf, dprintf, sprintf, snprintf, vprintf, vfprintf, vdprintf,
62730046 37vsprintf, vsnprintf \- formatted output conversion
fea681da
MK
38.SH SYNOPSIS
39.B #include <stdio.h>
40.sp
41.BI "int printf(const char *" format ", ...);"
42.br
43.BI "int fprintf(FILE *" stream ", const char *" format ", ...);"
44.br
62730046
MK
45.BI "int dprintf(int " fd ", const char *" format ", ...);"
46.br
fea681da
MK
47.BI "int sprintf(char *" str ", const char *" format ", ...);"
48.br
49.BI "int snprintf(char *" str ", size_t " size ", const char *" format ", ...);"
50.sp
51.B #include <stdarg.h>
52.sp
53.BI "int vprintf(const char *" format ", va_list " ap );
54.br
55.BI "int vfprintf(FILE *" stream ", const char *" format ", va_list " ap );
56.br
62730046
MK
57.BI "int vdprintf(int " fd ", const char *" format ", va_list " ap );
58.br
fea681da
MK
59.BI "int vsprintf(char *" str ", const char *" format ", va_list " ap );
60.br
3d54a910
MK
61.BI "int vsnprintf(char *" str ", size_t " size ", const char *" format \
62", va_list " ap );
cc4615cc
MK
63.sp
64.in -4n
65Feature Test Macro Requirements for glibc (see
66.BR feature_test_macros (7)):
67.in
68.sp
69.ad l
70.BR snprintf (),
71.BR vsnprintf ():
a1d525f2 72.RS 4
8f33dbe8
MK
73_BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 || _ISOC99_SOURCE ||
74_POSIX_C_SOURCE\ >=\ 200112L;
a1d525f2
MK
75.br
76or
cc4615cc 77.I "cc -std=c99"
a1d525f2 78.RE
62730046
MK
79.sp
80.BR dprintf (),
81.BR vdprintf ():
82.PD 0
83.RS 4
84.TP 4
85Since glibc 2.10:
86_XOPEN_SOURCE\ >=\ 700 || _POSIX_C_SOURCE\ >=\ 200809L
87.TP
88Before glibc 2.10:
89_GNU_SOURCE
90.RE
a1d525f2 91.ad
62730046 92.PD
fea681da
MK
93.SH DESCRIPTION
94The functions in the
e511ffb6 95.BR printf ()
fea681da
MK
96family produce output according to a
97.I format
c13182ef
MK
98as described below.
99The functions
e511ffb6 100.BR printf ()
fea681da 101and
e511ffb6 102.BR vprintf ()
fea681da
MK
103write output to
104.IR stdout ,
105the standard output stream;
e511ffb6 106.BR fprintf ()
fea681da 107and
e511ffb6 108.BR vfprintf ()
fea681da
MK
109write output to the given output
110.IR stream ;
e511ffb6 111.BR sprintf (),
c13182ef 112.BR snprintf (),
e511ffb6 113.BR vsprintf ()
fea681da 114and
e511ffb6 115.BR vsnprintf ()
fea681da 116write to the character string
3cf81850 117.IR str .
62730046
MK
118
119The function
120.BR dprintf ()
121is the same as
122.BR fprintf (3)
123except that it outputs to a file descriptor,
124.IR fd ,
125instead of to a
126.I stdio
127stream.
128
fea681da 129The functions
11050e19
MK
130.BR snprintf ()
131and
132.BR vsnprintf ()
133write at most
134.I size
31a6818e 135bytes (including the terminating null byte (\(aq\e0\(aq)) to
11050e19 136.IR str .
62730046 137
11050e19 138The functions
e511ffb6
MK
139.BR vprintf (),
140.BR vfprintf (),
62730046 141.BR vdprintf (),
e511ffb6
MK
142.BR vsprintf (),
143.BR vsnprintf ()
fea681da 144are equivalent to the functions
e511ffb6
MK
145.BR printf (),
146.BR fprintf (),
62730046 147.BR dprintf (),
e511ffb6
MK
148.BR sprintf (),
149.BR snprintf (),
f6b55d82
MK
150respectively, except that they are called with a
151.I va_list
152instead of a variable number of arguments.
c13182ef 153These functions do not call the
fea681da 154.I va_end
c13182ef 155macro.
f6b55d82
MK
156Because they invoke the
157.I va_arg
158macro, the value of
fea681da 159.I ap
c13182ef 160is undefined after the call.
f6b55d82 161See
31b640cb 162.BR stdarg (3).
62730046
MK
163
164All of these functions write the output under the control of a
fea681da
MK
165.I format
166string that specifies how subsequent arguments (or arguments accessed via
167the variable-length argument facilities of
168.BR stdarg (3))
169are converted for output.
08f88257
MK
170
171C99 and POSIX.1-2001 specify that the results are undefined if a call to
172.BR sprintf (),
173.BR snprintf (),
174.BR vsprintf (),
175or
176.BR vsnprintf ()
ed35aae6 177would cause copying to take place between objects that overlap
08f88257
MK
178(e.g., if the target string array and one of the supplied input arguments
179refer to the same buffer).
180See NOTES.
73d8cece 181.SS Format of the format string
fea681da
MK
182The format string is a character string, beginning and ending
183in its initial shift state, if any.
184The format string is composed of zero or more directives: ordinary
185characters (not
186.BR % ),
187which are copied unchanged to the output stream;
188and conversion specifications, each of which results in fetching zero or
c13182ef
MK
189more subsequent arguments.
190Each conversion specification is introduced by
fea681da
MK
191the character
192.BR % ,
193and ends with a
194.IR "conversion specifier" .
195In between there may be (in this order) zero or more
196.IR flags ,
197an optional minimum
198.IR "field width" ,
199an optional
200.I precision
201and an optional
202.IR "length modifier" .
203
204The arguments must correspond properly (after type promotion) with the
c13182ef
MK
205conversion specifier.
206By default, the arguments are used in the order
bbdc3380
MK
207given, where each \(aq*\(aq (see
208.I "Field width"
209and
210.I Precision
211below) and each conversion specifier asks for the next
fea681da
MK
212argument (and it is an error if insufficiently many arguments are given).
213One can also specify explicitly which argument is taken,
333a424b 214at each place where an argument is required, by writing "%m$" instead
c45660d7 215of \(aq%\(aq and "*m$" instead of \(aq*\(aq,
c38fc25e 216where the decimal integer \fIm\fP denotes
fea681da 217the position in the argument list of the desired argument, indexed starting
c13182ef
MK
218from 1.
219Thus,
bd191423 220.in +4n
fea681da 221.nf
7295b7ed 222
bd191423 223printf("%*d", width, num);
7295b7ed 224
fea681da 225.fi
bd191423 226.in
fea681da 227and
bd191423 228.in +4n
fea681da 229.nf
7295b7ed 230
bd191423 231printf("%2$*1$d", width, num);
7295b7ed 232
fea681da 233.fi
bd191423 234.in
c13182ef
MK
235are equivalent.
236The second style allows repeated references to the
237same argument.
333a424b 238The C99 standard does not include the style using \(aq$\(aq,
008f1ecc 239which comes from the Single UNIX Specification.
c13182ef 240If the style using
333a424b 241\(aq$\(aq is used, it must be used throughout for all conversions taking an
fea681da 242argument and all width and precision arguments, but it may be mixed
79306ba1 243with "%%" formats, which do not consume an argument.
c13182ef 244There may be no
333a424b 245gaps in the numbers of arguments specified using \(aq$\(aq; for example, if
fea681da
MK
246arguments 1 and 3 are specified, argument 2 must also be specified
247somewhere in the format string.
248
333a424b 249For some numeric conversions a radix character ("decimal point") or
c13182ef
MK
250thousands' grouping character is used.
251The actual character used
2f0af33b
MK
252depends on the
253.B LC_NUMERIC
254part of the locale.
c13182ef 255The POSIX locale
333a424b 256uses \(aq.\(aq as radix character, and does not have a grouping character.
fea681da 257Thus,
a6e2f128 258.in +4n
fea681da 259.nf
36306005 260
333a424b 261 printf("%\(aq.2f", 1234567.89);
36306005 262
fea681da 263.fi
a6e2f128 264.in
333a424b
MK
265results in "1234567.89" in the POSIX locale, in "1234567,89" in the
266nl_NL locale, and in "1.234.567,89" in the da_DK locale.
5c1685bd 267.SS Flag characters
fea681da
MK
268The character % is followed by zero or more of the following flags:
269.TP
270.B #
324633ae 271The value should be converted to an "alternate form".
fea681da 272For
0daa9e92 273.B o
fea681da
MK
274conversions, the first character of the output string is made zero
275(by prefixing a 0 if it was not zero already).
276For
277.B x
278and
279.B X
c7094399 280conversions, a nonzero result has the string "0x" (or "0X" for
fea681da 281.B X
c13182ef
MK
282conversions) prepended to it.
283For
fea681da
MK
284.BR a ,
285.BR A ,
286.BR e ,
287.BR E ,
288.BR f ,
289.BR F ,
290.BR g ,
291and
292.B G
293conversions, the result will always contain a decimal point, even if no
294digits follow it (normally, a decimal point appears in the results of those
c13182ef
MK
295conversions only if a digit follows).
296For
fea681da
MK
297.B g
298and
299.B G
300conversions, trailing zeros are not removed from the result as they would
301otherwise be.
302For other conversions, the result is undefined.
303.TP
304.B \&0
c13182ef
MK
305The value should be zero padded.
306For
fea681da
MK
307.BR d ,
308.BR i ,
309.BR o ,
310.BR u ,
311.BR x ,
312.BR X ,
313.BR a ,
314.BR A ,
315.BR e ,
316.BR E ,
317.BR f ,
318.BR F ,
319.BR g ,
320and
321.B G
322conversions, the converted value is padded on the left with zeros rather
323than blanks.
324If the
325.B \&0
326and
327.B \-
328flags both appear, the
329.B \&0
330flag is ignored.
331If a precision is given with a numeric conversion
b9d200ce 332.RB ( d ,
fea681da
MK
333.BR i ,
334.BR o ,
335.BR u ,
336.BR x ,
337and
338.BR X ),
339the
340.B \&0
341flag is ignored.
342For other conversions, the behavior is undefined.
343.TP
344.B \-
345The converted value is to be left adjusted on the field boundary.
c13182ef 346(The default is right justification.)
aacd17b1 347The converted value is padded on the right with blanks, rather
c13182ef
MK
348than on the left with blanks or zeros.
349A
fea681da
MK
350.B \-
351overrides a
352.B \&0
353if both are given.
354.TP
333a424b 355.B \(aq \(aq
fea681da
MK
356(a space) A blank should be left before a positive number
357(or empty string) produced by a signed conversion.
358.TP
359.B +
4897420e 360A sign (+ or \-) should always be placed before a number produced by a signed
c13182ef 361conversion.
a7676884 362By default, a sign is used only for negative numbers.
c13182ef 363A
fea681da
MK
364.B +
365overrides a space if both are used.
366.PP
f078c3fa
MK
367The five flag characters above are defined in the C99 standard.
368The Single UNIX Specification specifies one further flag character.
c13182ef 369.TP
333a424b 370.B \(aq
fea681da 371For decimal conversion
b9d200ce 372.RB ( i ,
fea681da
MK
373.BR d ,
374.BR u ,
375.BR f ,
376.BR F ,
377.BR g ,
378.BR G )
379the output is to be grouped with thousands' grouping characters
c13182ef
MK
380if the locale information indicates any.
381Note that many versions of
8478ee02 382.BR gcc (1)
c13182ef 383cannot parse this option and will issue a warning.
f078c3fa
MK
384(SUSv2 did not
385include \fI%\(aqF\fP, but SUSv3 added it.)
fea681da
MK
386.PP
387glibc 2.2 adds one further flag character.
388.TP
389.B I
390For decimal integer conversion
b9d200ce 391.RB ( i ,
fea681da
MK
392.BR d ,
393.BR u )
394the output uses the locale's alternative output digits, if any.
395For example, since glibc 2.2.3 this will give Arabic-Indic digits
333a424b 396in the Persian ("fa_IR") locale.
fea681da 397.\" outdigits keyword in locale file
5c1685bd 398.SS Field width
c7094399 399An optional decimal digit string (with nonzero first digit) specifying
c13182ef
MK
400a minimum field width.
401If the converted value has fewer characters
fea681da
MK
402than the field width, it will be padded with spaces on the left
403(or right, if the left-adjustment flag has been given).
333a424b
MK
404Instead of a decimal digit string one may write "*" or "*m$"
405(for some decimal integer \fIm\fP) to specify that the field width
406is given in the next argument, or in the \fIm\fP-th argument, respectively,
fea681da
MK
407which must be of type
408.IR int .
333a424b 409A negative field width is taken as a \(aq\-\(aq flag followed by a
fea681da 410positive field width.
c382a365 411In no case does a nonexistent or small field width cause truncation of a
fea681da
MK
412field; if the result of a conversion is wider than the field width, the
413field is expanded to contain the conversion result.
5c1685bd 414.SS Precision
333a424b 415An optional precision, in the form of a period (\(aq.\(aq) followed by an
fea681da 416optional decimal digit string.
333a424b 417Instead of a decimal digit string one may write "*" or "*m$"
c38fc25e
MK
418(for some decimal integer \fIm\fP) to specify that the precision
419is given in the next argument, or in the \fIm\fP-th argument, respectively,
fea681da
MK
420which must be of type
421.IR int .
abbcef30 422If the precision is given as just \(aq.\(aq, the precision is taken to
533b9c5c
MK
423be zero.
424A negative precision is taken as if the precision were omitted.
fea681da
MK
425This gives the minimum number of digits to appear for
426.BR d ,
427.BR i ,
428.BR o ,
429.BR u ,
430.BR x ,
431and
432.B X
433conversions, the number of digits to appear after the radix character for
434.BR a ,
435.BR A ,
436.BR e ,
437.BR E ,
438.BR f ,
439and
440.B F
441conversions, the maximum number of significant digits for
442.B g
443and
444.B G
445conversions, or the maximum number of characters to be printed from a
446string for
447.B s
448and
449.B S
450conversions.
5c1685bd 451.SS Length modifier
333a424b 452Here, "integer conversion" stands for
fea681da
MK
453.BR d ,
454.BR i ,
455.BR o ,
456.BR u ,
457.BR x ,
458or
0daa9e92 459.B X
fea681da
MK
460conversion.
461.TP
462.B hh
463A following integer conversion corresponds to a
464.I signed char
465or
466.I unsigned char
467argument, or a following
468.B n
469conversion corresponds to a pointer to a
470.I signed char
471argument.
472.TP
473.B h
474A following integer conversion corresponds to a
475.I short int
476or
477.I unsigned short int
478argument, or a following
479.B n
480conversion corresponds to a pointer to a
481.I short int
482argument.
483.TP
484.B l
485(ell) A following integer conversion corresponds to a
486.I long int
487or
488.I unsigned long int
489argument, or a following
490.B n
491conversion corresponds to a pointer to a
492.I long int
493argument, or a following
494.B c
495conversion corresponds to a
496.I wint_t
497argument, or a following
498.B s
499conversion corresponds to a pointer to
500.I wchar_t
501argument.
502.TP
503.B ll
504(ell-ell).
505A following integer conversion corresponds to a
506.I long long int
507or
508.I unsigned long long int
509argument, or a following
510.B n
511conversion corresponds to a pointer to a
512.I long long int
513argument.
514.TP
0daa9e92 515.B L
fea681da
MK
516A following
517.BR a ,
518.BR A ,
519.BR e ,
520.BR E ,
521.BR f ,
522.BR F ,
523.BR g ,
524or
525.B G
526conversion corresponds to a
527.I long double
528argument.
529(C99 allows %LF, but SUSv2 does not.)
7905872b
MK
530.\" .TP
531.\" .B q
532.\" ("quad". 4.4BSD and Linux libc5 only.
533.\" Don't use.)
fea681da
MK
534This is a synonym for
535.BR ll .
536.TP
537.B j
538A following integer conversion corresponds to an
539.I intmax_t
540or
541.I uintmax_t
c271056c
MK
542argument, or a following
543.B n
544conversion corresponds to a pointer to an
545.I intmax_t
fea681da
MK
546argument.
547.TP
548.B z
549A following integer conversion corresponds to a
550.I size_t
551or
552.I ssize_t
c271056c
MK
553argument, or a following
554.B n
555conversion corresponds to a pointer to a
556.I size_t
c13182ef 557argument.
7905872b
MK
558.\" (Linux libc5 has
559.\" .B Z
560.\" with this meaning.
561.\" Don't use it.)
fea681da
MK
562.TP
563.B t
564A following integer conversion corresponds to a
565.I ptrdiff_t
c271056c
MK
566argument, or a following
567.B n
568conversion corresponds to a pointer to a
569.I ptrdiff_t
fea681da
MK
570argument.
571.PP
f078c3fa
MK
572SUSv3 specifies all of the above.
573SUSv2 specified only the length modifiers
fea681da
MK
574.B h
575(in
576.BR hd ,
577.BR hi ,
578.BR ho ,
579.BR hx ,
580.BR hX ,
581.BR hn )
582and
583.B l
584(in
585.BR ld ,
586.BR li ,
587.BR lo ,
588.BR lx ,
589.BR lX ,
590.BR ln ,
591.BR lc ,
592.BR ls )
593and
594.B L
595(in
596.BR Le ,
597.BR LE ,
598.BR Lf ,
599.BR Lg ,
600.BR LG ).
5c1685bd 601.SS Conversion specifiers
fea681da
MK
602A character that specifies the type of conversion to be applied.
603The conversion specifiers and their meanings are:
604.TP
512c4c1a 605.BR d ", " i
fea681da
MK
606The
607.I int
608argument is converted to signed decimal notation.
609The precision, if any, gives the minimum number of digits
610that must appear; if the converted value requires fewer digits, it is
c13182ef
MK
611padded on the left with zeros.
612The default precision is 1.
fea681da
MK
613When 0 is printed with an explicit precision 0, the output is empty.
614.TP
512c4c1a 615.BR o ", " u ", " x ", " X
fea681da
MK
616The
617.I "unsigned int"
618argument is converted to unsigned octal
b9d200ce 619.RB ( o ),
fea681da 620unsigned decimal
b9d200ce 621.RB ( u ),
fea681da 622or unsigned hexadecimal
b9d200ce 623.RB ( x
fea681da
MK
624and
625.BR X )
c13182ef
MK
626notation.
627The letters
fea681da
MK
628.B abcdef
629are used for
630.B x
631conversions; the letters
632.B ABCDEF
633are used for
634.B X
c13182ef
MK
635conversions.
636The precision, if any, gives the minimum number of digits
fea681da 637that must appear; if the converted value requires fewer digits, it is
c13182ef
MK
638padded on the left with zeros.
639The default precision is 1.
fea681da
MK
640When 0 is printed with an explicit precision 0, the output is empty.
641.TP
512c4c1a 642.BR e ", " E
fea681da
MK
643The
644.I double
645argument is rounded and converted in the style
ee9d4801 646.RB [\-]d \&. ddd e \(+-dd
ca5ac066 647where there is one digit before the decimal-point character and the number
fea681da 648of digits after it is equal to the precision; if the precision is missing,
ca5ac066 649it is taken as 6; if the precision is zero, no decimal-point character
c13182ef
MK
650appears.
651An
fea681da
MK
652.B E
653conversion uses the letter
654.B E
655(rather than
656.BR e )
c13182ef
MK
657to introduce the exponent.
658The exponent always contains at least two
fea681da
MK
659digits; if the value is zero, the exponent is 00.
660.TP
512c4c1a 661.BR f ", " F
fea681da
MK
662The
663.I double
664argument is rounded and converted to decimal notation in the style
b9d200ce 665.RB [\-]ddd \&. ddd,
ca5ac066 666where the number of digits after the decimal-point character is equal to
c13182ef
MK
667the precision specification.
668If the precision is missing, it is taken as
ca5ac066 6696; if the precision is explicitly zero, no decimal-point character appears.
fea681da
MK
670If a decimal point appears, at least one digit appears before it.
671
f835f2c4 672(SUSv2 does not know about
fea681da
MK
673.B F
674and says that character string representations for infinity and NaN
c13182ef 675may be made available.
f078c3fa
MK
676SUSv3 adds a specification for
677.BR F .
333a424b
MK
678The C99 standard specifies "[\-]inf" or "[\-]infinity"
679for infinity, and a string starting with "nan" for NaN, in the case of
fea681da 680.B f
333a424b 681conversion, and "[\-]INF" or "[\-]INFINITY" or "NAN*" in the case of
fea681da
MK
682.B F
683conversion.)
684.TP
512c4c1a 685.BR g ", " G
fea681da
MK
686The
687.I double
688argument is converted in style
689.B f
690or
691.B e
692(or
693.B F
694or
695.B E
696for
697.B G
c13182ef
MK
698conversions).
699The precision specifies the number of significant digits.
fea681da 700If the precision is missing, 6 digits are given; if the precision is zero,
c13182ef
MK
701it is treated as 1.
702Style
fea681da
MK
703.B e
704is used if the exponent from its conversion is less than \-4 or greater
c13182ef
MK
705than or equal to the precision.
706Trailing zeros are removed from the
fea681da
MK
707fractional part of the result; a decimal point appears only if it is
708followed by at least one digit.
709.TP
512c4c1a 710.BR a ", " A
f078c3fa
MK
711(C99; not in SUSv2, but added in SUSv3)
712For
fea681da
MK
713.B a
714conversion, the
715.I double
716argument is converted to hexadecimal notation (using the letters abcdef)
717in the style
ee9d4801 718.RB [\-] 0x h \&. hhhh p \(+-;
fea681da
MK
719for
720.B A
721conversion the prefix
722.BR 0X ,
723the letters ABCDEF, and the exponent separator
724.B P
725is used.
726There is one hexadecimal digit before the decimal point,
727and the number of digits after it is equal to the precision.
728The default precision suffices for an exact representation of the value
729if an exact representation in base 2 exists
730and otherwise is sufficiently large to distinguish values of type
731.IR double .
24b74457 732The digit before the decimal point is unspecified for nonnormalized
c7094399 733numbers, and nonzero but otherwise unspecified for normalized numbers.
fea681da
MK
734.TP
735.B c
736If no
737.B l
738modifier is present, the
739.I int
740argument is converted to an
741.IR "unsigned char" ,
742and the resulting character is written.
743If an
744.B l
745modifier is present, the
746.I wint_t
747(wide character) argument is converted to a multibyte sequence by a call
748to the
fb186734 749.BR wcrtomb (3)
fea681da
MK
750function, with a conversion state starting in the initial state, and the
751resulting multibyte string is written.
752.TP
753.B s
754If no
755.B l
756modifier is present: The
5049da5b 757.I "const char\ *"
fea681da 758argument is expected to be a pointer to an array of character type (pointer
c13182ef
MK
759to a string).
760Characters from the array are written up to (but not
333a424b 761including) a terminating null byte (\(aq\\0\(aq);
28d88c17 762if a precision is specified, no more than the number specified
c13182ef
MK
763are written.
764If a precision is given, no null byte need be present;
fea681da 765if the precision is not specified, or is greater than the size of the
28d88c17 766array, the array must contain a terminating null byte.
fea681da
MK
767
768If an
769.B l
770modifier is present: The
5049da5b 771.I "const wchar_t\ *"
fea681da
MK
772argument is expected to be a pointer to an array of wide characters.
773Wide characters from the array are converted to multibyte characters
774(each by a call to the
fb186734 775.BR wcrtomb (3)
fea681da
MK
776function, with a conversion state starting in the initial state before
777the first wide character), up to and including a terminating null
c13182ef
MK
778wide character.
779The resulting multibyte characters are written up to
780(but not including) the terminating null byte.
781If a precision is
fea681da 782specified, no more bytes than the number specified are written, but
c13182ef
MK
783no partial multibyte characters are written.
784Note that the precision
fea681da
MK
785determines the number of
786.I bytes
787written, not the number of
788.I wide characters
789or
790.IR "screen positions" .
791The array must contain a terminating null wide character, unless a
792precision is given and it is so small that the number of bytes written
793exceeds it before the end of the array is reached.
794.TP
795.B C
81204056 796(Not in C99 or C11, but in SUSv2, SUSv3, and SUSv4.)
fea681da
MK
797Synonym for
798.BR lc .
799Don't use.
800.TP
801.B S
81204056 802(Not in C99 or C11, but in SUSv2, SUSv3, and SUSv4.)
fea681da
MK
803Synonym for
804.BR ls .
805Don't use.
806.TP
807.B p
808The
5049da5b 809.I "void\ *"
fea681da
MK
810pointer argument is printed in hexadecimal (as if by
811.B %#x
812or
edcf162e 813.BR %#lx ).
fea681da
MK
814.TP
815.B n
816The number of characters written so far is stored into the integer
aacd17b1
MK
817pointed to by the corresponding argument.
818That argument shall be an
bafaef21 819.IR "int\ *" ,
aacd17b1
MK
820or variant whose size matches the (optionally)
821supplied integer length modifier.
c13182ef 822No argument is converted.
aacd17b1
MK
823The behavior is undefined if the conversion specification includes
824any flags, a field width, or a precision.
c13182ef 825.TP
c15f96dd 826.B m
c13182ef
MK
827(Glibc extension.)
828Print output of
c15f96dd
MK
829.IR strerror(errno) .
830No argument is required.
fea681da
MK
831.TP
832.B %
333a424b 833A \(aq%\(aq is written.
c13182ef
MK
834No argument is converted.
835The complete conversion
333a424b 836specification is \(aq%%\(aq.
648fdb79
MK
837.SH RETURN VALUE
838Upon successful return, these functions return the number of characters
839printed (excluding the null byte used to end output to strings).
840
841The functions
842.BR snprintf ()
843and
844.BR vsnprintf ()
845do not write more than
846.I size
847bytes (including the terminating null byte (\(aq\e0\(aq)).
848If the output was truncated due to this limit, then the return value
849is the number of characters (excluding the terminating null byte)
850which would have been written to the final string if enough space
851had been available.
852Thus, a return value of
853.I size
854or more means that the output was truncated.
855(See also below under NOTES.)
856
857If an output error is encountered, a negative value is returned.
1567901b
ZL
858.SH ATTRIBUTES
859For an explanation of the terms used in this section, see
860.BR attributes (7).
861.TS
862allbox;
863lbw23 lb lb
864l l l.
865Interface Attribute Value
866T{
867.BR printf (),
868.BR fprintf (),
869.br
870.BR sprintf (),
871.BR snprintf (),
872.br
873.BR vprintf (),
874.BR vfprintf (),
875.br
876.BR vsprintf (),
877.BR vsnprintf ()
878T} Thread safety MT-Safe locale
879.TE
880
47297adb 881.SH CONFORMING TO
fea681da 882The
e511ffb6
MK
883.BR fprintf (),
884.BR printf (),
885.BR sprintf (),
886.BR vprintf (),
887.BR vfprintf (),
fea681da 888and
e511ffb6 889.BR vsprintf ()
68e1685c 890functions conform to C89 and C99.
62730046 891
fea681da 892The
e511ffb6 893.BR snprintf ()
fea681da 894and
e511ffb6 895.BR vsnprintf ()
68e1685c 896functions conform to C99.
62730046
MK
897
898The
899.BR dprintf ()
900and
901.BR vdprintf ()
902functions were originally GNU extensions that were later standardized
903in POSIX.1-2008.
fea681da
MK
904.PP
905Concerning the return value of
e511ffb6 906.BR snprintf (),
68e1685c 907SUSv2 and C99 contradict each other: when
e511ffb6 908.BR snprintf ()
fea681da
MK
909is called with
910.IR size =0
911then SUSv2 stipulates an unspecified return value less than 1,
912while C99 allows
913.I str
914to be NULL in this case, and gives the return value (as always)
915as the number of characters that would have been written in case
916the output string has been large enough.
588d27ea
MK
917SUSv3 and later align their specification of
918.BR snprintf ()
919with C99.
7905872b
MK
920.\" .PP
921.\" Linux libc4 knows about the five C standard flags.
922.\" It knows about the length modifiers \fBh\fP, \fBl\fP, \fBL\fP,
923.\" and the conversions
924.\" \fBc\fP, \fBd\fP, \fBe\fP, \fBE\fP, \fBf\fP, \fBF\fP,
925.\" \fBg\fP, \fBG\fP, \fBi\fP, \fBn\fP, \fBo\fP, \fBp\fP,
926.\" \fBs\fP, \fBu\fP, \fBx\fP, and \fBX\fP,
927.\" where \fBF\fP is a synonym for \fBf\fP.
928.\" Additionally, it accepts \fBD\fP, \fBO\fP, and \fBU\fP as synonyms
929.\" for \fBld\fP, \fBlo\fP, and \fBlu\fP.
930.\" (This is bad, and caused serious bugs later, when
931.\" support for \fB%D\fP disappeared.)
932.\" No locale-dependent radix character,
933.\" no thousands' separator, no NaN or infinity, no "%m$" and "*m$".
934.\" .PP
935.\" Linux libc5 knows about the five C standard flags and the \(aq flag,
936.\" locale, "%m$" and "*m$".
937.\" It knows about the length modifiers \fBh\fP, \fBl\fP, \fBL\fP,
938.\" \fBZ\fP, and \fBq\fP, but accepts \fBL\fP and \fBq\fP
939.\" both for \fIlong double\fP and for \fIlong long int\fP (this is a bug).
940.\" It no longer recognizes \fBF\fP, \fBD\fP, \fBO\fP, and \fBU\fP,
941.\" but adds the conversion character
942.\" .BR m ,
943.\" which outputs
944.\" .IR strerror(errno) .
5afad698
MK
945.\" .PP
946.\" glibc 2.0 adds conversion characters \fBC\fP and \fBS\fP.
fea681da 947.PP
512c4c1a 948glibc 2.1 adds length modifiers \fBhh\fP, \fBj\fP, \fBt\fP, and \fBz\fP