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