]> git.saurik.com Git - apple/libc.git/blame - stdio/FreeBSD/printf.3
Libc-997.1.1.tar.gz
[apple/libc.git] / stdio / FreeBSD / printf.3
CommitLineData
5b2abdfb
A
1.\" Copyright (c) 1990, 1991, 1993
2.\" The Regents of the University of California. All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
5b2abdfb
A
16.\" 4. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" @(#)printf.3 8.1 (Berkeley) 6/4/93
1f2f436a 33.\" $FreeBSD: src/lib/libc/stdio/printf.3,v 1.64 2009/12/02 07:51:25 brueffer Exp $
5b2abdfb 34.\"
1f2f436a 35.Dd December 2, 2009
5b2abdfb
A
36.Dt PRINTF 3
37.Os
38.Sh NAME
1f2f436a
A
39.Nm printf , fprintf , sprintf , snprintf , asprintf , dprintf ,
40.Nm vprintf , vfprintf, vsprintf , vsnprintf , vasprintf, vdprintf
5b2abdfb
A
41.Nd formatted output conversion
42.Sh LIBRARY
43.Lb libc
44.Sh SYNOPSIS
45.In stdio.h
46.Ft int
9385eb3d 47.Fn printf "const char * restrict format" ...
5b2abdfb 48.Ft int
9385eb3d 49.Fn fprintf "FILE * restrict stream" "const char * restrict format" ...
5b2abdfb 50.Ft int
9385eb3d 51.Fn sprintf "char * restrict str" "const char * restrict format" ...
5b2abdfb 52.Ft int
9385eb3d 53.Fn snprintf "char * restrict str" "size_t size" "const char * restrict format" ...
5b2abdfb
A
54.Ft int
55.Fn asprintf "char **ret" "const char *format" ...
1f2f436a
A
56.Ft int
57.Fn dprintf "int fd" "const char * restrict format" ...
5b2abdfb
A
58.In stdarg.h
59.Ft int
9385eb3d 60.Fn vprintf "const char * restrict format" "va_list ap"
5b2abdfb 61.Ft int
9385eb3d 62.Fn vfprintf "FILE * restrict stream" "const char * restrict format" "va_list ap"
5b2abdfb 63.Ft int
9385eb3d 64.Fn vsprintf "char * restrict str" "const char * restrict format" "va_list ap"
5b2abdfb 65.Ft int
9385eb3d 66.Fn vsnprintf "char * restrict str" "size_t size" "const char * restrict format" "va_list ap"
5b2abdfb
A
67.Ft int
68.Fn vasprintf "char **ret" "const char *format" "va_list ap"
1f2f436a
A
69.Ft int
70.Fn vdprintf "int fd" "const char * restrict format" "va_list ap"
5b2abdfb
A
71.Sh DESCRIPTION
72The
73.Fn printf
74family of functions produces output according to a
75.Fa format
76as described below.
9385eb3d
A
77The
78.Fn printf
5b2abdfb
A
79and
80.Fn vprintf
9385eb3d 81functions
5b2abdfb 82write output to
9385eb3d 83.Dv stdout ,
5b2abdfb
A
84the standard output stream;
85.Fn fprintf
86and
87.Fn vfprintf
88write output to the given output
89.Fa stream ;
1f2f436a
A
90.Fn dprintf
91and
92.Fn vdprintf
93write output to the given file descriptor;
5b2abdfb
A
94.Fn sprintf ,
95.Fn snprintf ,
96.Fn vsprintf ,
97and
98.Fn vsnprintf
99write to the character string
ad3c9f2a 100.Fa s ;
5b2abdfb
A
101and
102.Fn asprintf
103and
104.Fn vasprintf
105dynamically allocate a new string with
106.Xr malloc 3 .
107.Pp
ad3c9f2a
A
108Extended locale versions of these functions are documented in
109.Xr printf_l 3 .
110See
111.Xr xlocale 3
112for more information.
113.Pp
5b2abdfb
A
114These functions write the output under the control of a
115.Fa format
116string that specifies how subsequent arguments
117(or arguments accessed via the variable-length argument facilities of
118.Xr stdarg 3 )
119are converted for output.
120.Pp
121These functions return the number of characters printed
122(not including the trailing
123.Ql \e0
9385eb3d 124used to end output to strings) or a negative value if an output error occurs,
5b2abdfb
A
125except for
126.Fn snprintf
127and
128.Fn vsnprintf ,
129which return the number of characters that would have been printed if the
ad3c9f2a 130.Fa n
5b2abdfb
A
131were unlimited
132(again, not including the final
133.Ql \e0 ) .
134.Pp
9385eb3d
A
135The
136.Fn asprintf
5b2abdfb
A
137and
138.Fn vasprintf
9385eb3d 139functions
5b2abdfb
A
140set
141.Fa *ret
142to be a pointer to a buffer sufficiently large to hold the formatted string.
143This pointer should be passed to
144.Xr free 3
145to release the allocated storage when it is no longer needed.
146If sufficient space cannot be allocated,
147.Fn asprintf
148and
149.Fn vasprintf
9385eb3d 150will return \-1 and set
5b2abdfb
A
151.Fa ret
152to be a
153.Dv NULL
154pointer.
155.Pp
9385eb3d
A
156The
157.Fn snprintf
5b2abdfb
A
158and
159.Fn vsnprintf
9385eb3d 160functions
5b2abdfb 161will write at most
ad3c9f2a 162.Fa n Ns \-1
5b2abdfb
A
163of the characters printed into the output string
164(the
ad3c9f2a 165.Fa n Ns \'th
5b2abdfb
A
166character then gets the terminating
167.Ql \e0 ) ;
168if the return value is greater than or equal to the
ad3c9f2a 169.Fa n
5b2abdfb
A
170argument, the string was too short
171and some of the printed characters were discarded.
9385eb3d 172The output is always null-terminated.
5b2abdfb 173.Pp
9385eb3d
A
174The
175.Fn sprintf
5b2abdfb
A
176and
177.Fn vsprintf
9385eb3d 178functions
5b2abdfb 179effectively assume an infinite
ad3c9f2a
A
180.Fa n .
181.Pp
182For those routines that write to a user-provided character string,
183that string and the format strings should not overlap, as the
184behavior is undefined.
5b2abdfb
A
185.Pp
186The format string is composed of zero or more directives:
187ordinary
188.\" multibyte
189characters (not
190.Cm % ) ,
191which are copied unchanged to the output stream;
192and conversion specifications, each of which results
193in fetching zero or more subsequent arguments.
194Each conversion specification is introduced by
195the
196.Cm %
197character.
198The arguments must correspond properly (after type promotion)
199with the conversion specifier.
200After the
201.Cm % ,
202the following appear in sequence:
203.Bl -bullet
204.It
205An optional field, consisting of a decimal digit string followed by a
206.Cm $ ,
207specifying the next argument to access.
208If this field is not provided, the argument following the last
209argument accessed will be used.
210Arguments are numbered starting at
211.Cm 1 .
212If unaccessed arguments in the format string are interspersed with ones that
213are accessed the results will be indeterminate.
214.It
215Zero or more of the following flags:
9385eb3d
A
216.Bl -tag -width ".So \ Sc (space)"
217.It Sq Cm #
218The value should be converted to an
5b2abdfb
A
219.Dq alternate form .
220For
221.Cm c , d , i , n , p , s ,
222and
223.Cm u
224conversions, this option has no effect.
225For
226.Cm o
227conversions, the precision of the number is increased to force the first
1f2f436a 228character of the output string to a zero.
5b2abdfb
A
229For
230.Cm x
231and
232.Cm X
233conversions, a non-zero result has the string
234.Ql 0x
235(or
236.Ql 0X
237for
238.Cm X
239conversions) prepended to it.
240For
9385eb3d 241.Cm a , A , e , E , f , F , g ,
5b2abdfb
A
242and
243.Cm G
244conversions, the result will always contain a decimal point, even if no
245digits follow it (normally, a decimal point appears in the results of
246those conversions only if a digit follows).
247For
248.Cm g
249and
250.Cm G
251conversions, trailing zeros are not removed from the result as they
252would otherwise be.
9385eb3d
A
253.It So Cm 0 Sc (zero)
254Zero padding.
5b2abdfb
A
255For all conversions except
256.Cm n ,
257the converted value is padded on the left with zeros rather than blanks.
258If a precision is given with a numeric conversion
259.Cm ( d , i , o , u , i , x ,
260and
261.Cm X ) ,
262the
263.Cm 0
264flag is ignored.
9385eb3d
A
265.It Sq Cm \-
266A negative field width flag;
267the converted value is to be left adjusted on the field boundary.
5b2abdfb
A
268Except for
269.Cm n
270conversions, the converted value is padded on the right with blanks,
271rather than on the left with blanks or zeros.
272A
273.Cm \-
274overrides a
275.Cm 0
276if both are given.
9385eb3d
A
277.It So "\ " Sc (space)
278A blank should be left before a positive number
5b2abdfb 279produced by a signed conversion
9385eb3d 280.Cm ( a , A , d , e , E , f , F , g , G ,
5b2abdfb
A
281or
282.Cm i ) .
9385eb3d
A
283.It Sq Cm +
284A sign must always be placed before a
5b2abdfb
A
285number produced by a signed conversion.
286A
287.Cm +
288overrides a space if both are used.
9385eb3d
A
289.It Sq Cm '
290Decimal conversions
291.Cm ( d , u ,
292or
293.Cm i )
294or the integral portion of a floating point conversion
295.Cm ( f
296or
297.Cm F )
298should be grouped and separated by thousands using
299the non-monetary separator returned by
300.Xr localeconv 3 .
5b2abdfb
A
301.El
302.It
ad3c9f2a
A
303An optional separator character (
304.Cm \ , | \; | \ : | _
305) used for separating multiple values when printing an AltiVec or SSE vector,
306or other multi-value unit.
307.Pp
308NOTE: This is an extension to the
309.Fn printf
310specification.
311Behaviour of these values for
312.Fn printf
313is only defined for operating systems conforming to the
314AltiVec Technology Programming Interface Manual.
315(At time of writing this includes only Mac OS X 10.2 and later.)
316.It
5b2abdfb
A
317An optional decimal digit string specifying a minimum field width.
318If the converted value has fewer characters than the field width, it will
319be padded with spaces on the left (or right, if the left-adjustment
320flag has been given) to fill out
321the field width.
322.It
323An optional precision, in the form of a period
324.Cm \&.
325followed by an
326optional digit string.
327If the digit string is omitted, the precision is taken as zero.
328This gives the minimum number of digits to appear for
329.Cm d , i , o , u , x ,
330and
331.Cm X
332conversions, the number of digits to appear after the decimal-point for
9385eb3d 333.Cm a , A , e , E , f ,
5b2abdfb 334and
9385eb3d 335.Cm F
5b2abdfb
A
336conversions, the maximum number of significant digits for
337.Cm g
338and
339.Cm G
340conversions, or the maximum number of characters to be printed from a
341string for
342.Cm s
343conversions.
344.It
9385eb3d
A
345An optional length modifier, that specifies the size of the argument.
346The following length modifiers are valid for the
347.Cm d , i , n , o , u , x ,
5b2abdfb
A
348or
349.Cm X
9385eb3d
A
350conversion:
351.Bl -column ".Cm q Em (deprecated)" ".Vt signed char" ".Vt unsigned long long" ".Vt long long *"
352.It Sy Modifier Ta Cm d , i Ta Cm o , u , x , X Ta Cm n
353.It Cm hh Ta Vt "signed char" Ta Vt "unsigned char" Ta Vt "signed char *"
354.It Cm h Ta Vt short Ta Vt "unsigned short" Ta Vt "short *"
355.It Cm l No (ell) Ta Vt long Ta Vt "unsigned long" Ta Vt "long *"
356.It Cm ll No (ell ell) Ta Vt "long long" Ta Vt "unsigned long long" Ta Vt "long long *"
357.It Cm j Ta Vt intmax_t Ta Vt uintmax_t Ta Vt "intmax_t *"
358.It Cm t Ta Vt ptrdiff_t Ta (see note) Ta Vt "ptrdiff_t *"
359.It Cm z Ta (see note) Ta Vt size_t Ta (see note)
360.It Cm q Em (deprecated) Ta Vt quad_t Ta Vt u_quad_t Ta Vt "quad_t *"
361.El
362.Pp
363Note:
364the
365.Cm t
366modifier, when applied to a
367.Cm o , u , x ,
5b2abdfb
A
368or
369.Cm X
9385eb3d
A
370conversion, indicates that the argument is of an unsigned type
371equivalent in size to a
372.Vt ptrdiff_t .
373The
374.Cm z
375modifier, when applied to a
376.Cm d
5b2abdfb 377or
9385eb3d
A
378.Cm i
379conversion, indicates that the argument is of a signed type equivalent in
380size to a
381.Vt size_t .
382Similarly, when applied to an
5b2abdfb 383.Cm n
9385eb3d
A
384conversion, it indicates that the argument is a pointer to a signed type
385equivalent in size to a
386.Vt size_t .
387.Pp
388The following length modifier is valid for the
389.Cm a , A , e , E , f , F , g ,
5b2abdfb
A
390or
391.Cm G
9385eb3d
A
392conversion:
393.Bl -column ".Sy Modifier" ".Cm a , A , e , E , f , F , g , G"
394.It Sy Modifier Ta Cm a , A , e , E , f , F , g , G
3d9156a7
A
395.It Cm l No (ell) Ta Vt double
396(ignored, same behavior as without it)
9385eb3d
A
397.It Cm L Ta Vt "long double"
398.El
399.Pp
400The following length modifier is valid for the
401.Cm c
402or
403.Cm s
404conversion:
405.Bl -column ".Sy Modifier" ".Vt wint_t" ".Vt wchar_t *"
406.It Sy Modifier Ta Cm c Ta Cm s
407.It Cm l No (ell) Ta Vt wint_t Ta Vt "wchar_t *"
408.El
ad3c9f2a
A
409.Pp
410The AltiVec Technology Programming Interface Manual also defines five additional length modifiers
411which can be used (in place of the conventional length modifiers) for the printing of AltiVec or SSE vectors:
412.Bl -tag -compact
413.It Cm v
414Treat the argument as a vector value, unit length will be determined by the conversion
415specifier (default = 16 8-bit units for all integer conversions,
4164 32-bit units for floating point conversions).
417.It Cm vh, hv
418Treat the argument as a vector of 8 16-bit units.
419.It Cm vl, lv
420Treat the argument as a vector of 4 32-bit units.
421.El
422.Pp
423NOTE: The vector length specifiers are extensions to the
424.Fn printf
425specification.
426Behaviour of these values for
427.Fn printf
428is only defined for operating systems conforming to the
429AltiVec Technology Programming Interface Manual.
430(At time of writing this includes only Mac OS X 10.2 and later.)
431.Pp
432As a further extension, for SSE2 64-bit units:
433.Bl -tag -compact
434.It Cm vll, llv
435Treat the argument as a vector of 2 64-bit units.
436.El
5b2abdfb
A
437.It
438A character that specifies the type of conversion to be applied.
439.El
440.Pp
441A field width or precision, or both, may be indicated by
442an asterisk
443.Ql *
444or an asterisk followed by one or more decimal digits and a
445.Ql $
446instead of a
447digit string.
448In this case, an
449.Vt int
450argument supplies the field width or precision.
451A negative field width is treated as a left adjustment flag followed by a
452positive field width; a negative precision is treated as though it were
453missing.
9385eb3d
A
454If a single format directive mixes positional
455.Pq Li nn$
5b2abdfb
A
456and non-positional arguments, the results are undefined.
457.Pp
458The conversion specifiers and their meanings are:
9385eb3d 459.Bl -tag -width ".Cm diouxX"
5b2abdfb
A
460.It Cm diouxX
461The
462.Vt int
463(or appropriate variant) argument is converted to signed decimal
464.Cm ( d
465and
466.Cm i ) ,
467unsigned octal
468.Pq Cm o ,
469unsigned decimal
470.Pq Cm u ,
471or unsigned hexadecimal
472.Cm ( x
473and
474.Cm X )
475notation.
476The letters
9385eb3d 477.Dq Li abcdef
5b2abdfb
A
478are used for
479.Cm x
480conversions; the letters
9385eb3d 481.Dq Li ABCDEF
5b2abdfb
A
482are used for
483.Cm X
484conversions.
485The precision, if any, gives the minimum number of digits that must
486appear; if the converted value requires fewer digits, it is padded on
487the left with zeros.
488.It Cm DOU
489The
9385eb3d 490.Vt "long int"
5b2abdfb
A
491argument is converted to signed decimal, unsigned octal, or unsigned
492decimal, as if the format had been
493.Cm ld , lo ,
494or
495.Cm lu
496respectively.
497These conversion characters are deprecated, and will eventually disappear.
498.It Cm eE
499The
500.Vt double
501argument is rounded and converted in the style
9385eb3d
A
502.Sm off
503.Oo \- Oc Ar d Li \&. Ar ddd Li e \\*[Pm] Ar dd
504.Sm on
5b2abdfb
A
505where there is one digit before the
506decimal-point character
507and the number of digits after it is equal to the precision;
508if the precision is missing,
509it is taken as 6; if the precision is
510zero, no decimal-point character appears.
511An
512.Cm E
513conversion uses the letter
9385eb3d 514.Ql E
5b2abdfb 515(rather than
9385eb3d 516.Ql e )
5b2abdfb
A
517to introduce the exponent.
518The exponent always contains at least two digits; if the value is zero,
519the exponent is 00.
9385eb3d
A
520.Pp
521For
522.Cm a , A , e , E , f , F , g ,
523and
524.Cm G
525conversions, positive and negative infinity are represented as
526.Li inf
527and
528.Li -inf
529respectively when using the lowercase conversion character, and
530.Li INF
531and
532.Li -INF
533respectively when using the uppercase conversion character.
534Similarly, NaN is represented as
535.Li nan
536when using the lowercase conversion, and
537.Li NAN
538when using the uppercase conversion.
539.It Cm fF
5b2abdfb
A
540The
541.Vt double
542argument is rounded and converted to decimal notation in the style
9385eb3d
A
543.Sm off
544.Oo \- Oc Ar ddd Li \&. Ar ddd ,
545.Sm on
5b2abdfb
A
546where the number of digits after the decimal-point character
547is equal to the precision specification.
548If the precision is missing, it is taken as 6; if the precision is
549explicitly zero, no decimal-point character appears.
550If a decimal point appears, at least one digit appears before it.
551.It Cm gG
552The
553.Vt double
554argument is converted in style
555.Cm f
556or
557.Cm e
558(or
9385eb3d
A
559.Cm F
560or
5b2abdfb
A
561.Cm E
562for
563.Cm G
564conversions).
565The precision specifies the number of significant digits.
566If the precision is missing, 6 digits are given; if the precision is zero,
567it is treated as 1.
568Style
569.Cm e
9385eb3d 570is used if the exponent from its conversion is less than \-4 or greater than
5b2abdfb
A
571or equal to the precision.
572Trailing zeros are removed from the fractional part of the result; a
573decimal point appears only if it is followed by at least one digit.
9385eb3d
A
574.It Cm aA
575The
576.Vt double
3d9156a7 577argument is rounded and converted to hexadecimal notation in the style
9385eb3d
A
578.Sm off
579.Oo \- Oc Li 0x Ar h Li \&. Ar hhhp Oo \\*[Pm] Oc Ar d ,
580.Sm on
581where the number of digits after the hexadecimal-point character
582is equal to the precision specification.
3d9156a7
A
583If the precision is missing, it is taken as enough to represent
584the floating-point number exactly, and no rounding occurs.
585If the precision is zero, no hexadecimal-point character appears.
9385eb3d
A
586The
587.Cm p
588is a literal character
3d9156a7
A
589.Ql p ,
590and the exponent consists of a positive or negative sign
591followed by a decimal number representing an exponent of 2.
9385eb3d
A
592The
593.Cm A
594conversion uses the prefix
595.Dq Li 0X
596(rather than
597.Dq Li 0x ) ,
598the letters
599.Dq Li ABCDEF
600(rather than
601.Dq Li abcdef )
602to represent the hex digits, and the letter
603.Ql P
604(rather than
605.Ql p )
606to separate the mantissa and exponent.
3d9156a7
A
607.Pp
608Note that there may be multiple valid ways to represent floating-point
609numbers in this hexadecimal format.
610For example,
1f2f436a 611.Li 0x1.92p+1 , 0x3.24p+0 , 0x6.48p-1 ,
3d9156a7
A
612and
613.Li 0xc.9p-2
614are all equivalent.
ad3c9f2a
A
615The format chosen depends on the internal representation of the
616number, but the implementation guarantees that the length of the
617mantissa will be minimized.
3d9156a7
A
618Zeroes are always represented with a mantissa of 0 (preceded by a
619.Ql -
620if appropriate) and an exponent of
621.Li +0 .
9385eb3d
A
622.It Cm C
623Treated as
624.Cm c
625with the
626.Cm l
627(ell) modifier.
5b2abdfb
A
628.It Cm c
629The
630.Vt int
631argument is converted to an
9385eb3d 632.Vt "unsigned char" ,
5b2abdfb 633and the resulting character is written.
9385eb3d
A
634.Pp
635If the
636.Cm l
637(ell) modifier is used, the
638.Vt wint_t
639argument shall be converted to a
640.Vt wchar_t ,
641and the (potentially multi-byte) sequence representing the
642single wide character is written, including any shift sequences.
643If a shift sequence is used, the shift state is also restored
644to the original state after the character.
645.It Cm S
646Treated as
647.Cm s
648with the
649.Cm l
650(ell) modifier.
5b2abdfb
A
651.It Cm s
652The
9385eb3d 653.Vt "char *"
5b2abdfb
A
654argument is expected to be a pointer to an array of character type (pointer
655to a string).
656Characters from the array are written up to (but not including)
657a terminating
658.Dv NUL
659character;
660if a precision is specified, no more than the number specified are
661written.
662If a precision is given, no null character
663need be present; if the precision is not specified, or is greater than
664the size of the array, the array must contain a terminating
665.Dv NUL
666character.
9385eb3d
A
667.Pp
668If the
669.Cm l
670(ell) modifier is used, the
671.Vt "wchar_t *"
672argument is expected to be a pointer to an array of wide characters
673(pointer to a wide string).
674For each wide character in the string, the (potentially multi-byte)
675sequence representing the
676wide character is written, including any shift sequences.
677If any shift sequence is used, the shift state is also restored
678to the original state after the string.
679Wide characters from the array are written up to (but not including)
680a terminating wide
681.Dv NUL
682character;
683if a precision is specified, no more than the number of bytes specified are
684written (including shift sequences).
685Partial characters are never written.
686If a precision is given, no null character
687need be present; if the precision is not specified, or is greater than
688the number of bytes required to render the multibyte representation of
689the string, the array must contain a terminating wide
690.Dv NUL
691character.
5b2abdfb
A
692.It Cm p
693The
9385eb3d 694.Vt "void *"
5b2abdfb
A
695pointer argument is printed in hexadecimal (as if by
696.Ql %#x
697or
698.Ql %#lx ) .
699.It Cm n
700The number of characters written so far is stored into the
701integer indicated by the
9385eb3d 702.Vt "int *"
5b2abdfb
A
703(or variant) pointer argument.
704No argument is converted.
705.It Cm %
706A
707.Ql %
708is written.
709No argument is converted.
710The complete conversion specification
711is
712.Ql %% .
713.El
714.Pp
9385eb3d
A
715The decimal point
716character is defined in the program's locale (category
717.Dv LC_NUMERIC ) .
718.Pp
5b2abdfb 719In no case does a non-existent or small field width cause truncation of
9385eb3d
A
720a numeric field; if the result of a conversion is wider than the field
721width, the
5b2abdfb
A
722field is expanded to contain the conversion result.
723.Sh EXAMPLES
724To print a date and time in the form
725.Dq Li "Sunday, July 3, 10:02" ,
726where
727.Fa weekday
728and
729.Fa month
730are pointers to strings:
731.Bd -literal -offset indent
732#include <stdio.h>
733fprintf(stdout, "%s, %s %d, %.2d:%.2d\en",
734 weekday, month, day, hour, min);
735.Ed
736.Pp
737To print \*(Pi
738to five decimal places:
739.Bd -literal -offset indent
740#include <math.h>
741#include <stdio.h>
742fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0));
743.Ed
744.Pp
745To allocate a 128 byte string and print into it:
746.Bd -literal -offset indent
747#include <stdio.h>
748#include <stdlib.h>
749#include <stdarg.h>
750char *newfmt(const char *fmt, ...)
751{
9385eb3d
A
752 char *p;
753 va_list ap;
754 if ((p = malloc(128)) == NULL)
755 return (NULL);
756 va_start(ap, fmt);
757 (void) vsnprintf(p, 128, fmt, ap);
758 va_end(ap);
759 return (p);
760}
761.Ed
762.Sh SECURITY CONSIDERATIONS
763The
764.Fn sprintf
765and
766.Fn vsprintf
767functions are easily misused in a manner which enables malicious users
768to arbitrarily change a running program's functionality through
769a buffer overflow attack.
770Because
771.Fn sprintf
772and
773.Fn vsprintf
774assume an infinitely long string,
775callers must be careful not to overflow the actual space;
776this is often hard to assure.
777For safety, programmers should use the
778.Fn snprintf
779interface instead.
780For example:
781.Bd -literal
782void
783foo(const char *arbitrary_string, const char *and_another)
784{
785 char onstack[8];
786
787#ifdef BAD
788 /*
789 * This first sprintf is bad behavior. Do not use sprintf!
790 */
791 sprintf(onstack, "%s, %s", arbitrary_string, and_another);
792#else
793 /*
794 * The following two lines demonstrate better use of
795 * snprintf().
796 */
797 snprintf(onstack, sizeof(onstack), "%s, %s", arbitrary_string,
798 and_another);
799#endif
5b2abdfb
A
800}
801.Ed
9385eb3d
A
802.Pp
803The
804.Fn printf
805and
806.Fn sprintf
807family of functions are also easily misused in a manner
808allowing malicious users to arbitrarily change a running program's
809functionality by either causing the program
810to print potentially sensitive data
811.Dq "left on the stack" ,
812or causing it to generate a memory fault or bus error
813by dereferencing an invalid pointer.
814.Pp
815.Cm %n
816can be used to write arbitrary data to potentially carefully-selected
817addresses.
818Programmers are therefore strongly advised to never pass untrusted strings
819as the
820.Fa format
821argument, as an attacker can put format specifiers in the string
822to mangle your stack,
823leading to a possible security hole.
824This holds true even if the string was built using a function like
825.Fn snprintf ,
826as the resulting string may still contain user-supplied conversion specifiers
827for later interpolation by
828.Fn printf .
829.Pp
830Always use the proper secure idiom:
831.Pp
832.Dl "snprintf(buffer, sizeof(buffer), \*q%s\*q, string);"
1f2f436a 833.Sh COMPATIBILITY
1f2f436a
A
834The conversion formats
835.Cm \&%D , \&%O ,
836and
837.Cm %U
838are not standard and
839are provided only for backward compatibility.
840The effect of padding the
841.Cm %p
842format with zeros (either by the
843.Cm 0
844flag or by specifying a precision), and the benign effect (i.e., none)
845of the
846.Cm #
847flag on
848.Cm %n
849and
850.Cm %p
851conversions, as well as other
852nonsensical combinations such as
853.Cm %Ld ,
854are not standard; such combinations
855should be avoided.
9385eb3d
A
856.Sh ERRORS
857In addition to the errors documented for the
858.Xr write 2
859system call, the
860.Fn printf
861family of functions may fail if:
862.Bl -tag -width Er
863.It Bq Er EILSEQ
864An invalid wide character code was encountered.
865.It Bq Er ENOMEM
866Insufficient storage space is available.
867.El
5b2abdfb
A
868.Sh SEE ALSO
869.Xr printf 1 ,
ad3c9f2a 870.Xr printf_l 3 ,
9385eb3d
A
871.Xr fmtcheck 3 ,
872.Xr scanf 3 ,
873.Xr setlocale 3 ,
ad3c9f2a 874.Xr stdarg 3 ,
9385eb3d 875.Xr wprintf 3
5b2abdfb 876.Sh STANDARDS
9385eb3d
A
877Subject to the caveats noted in the
878.Sx BUGS
879section below, the
5b2abdfb
A
880.Fn fprintf ,
881.Fn printf ,
882.Fn sprintf ,
883.Fn vprintf ,
884.Fn vfprintf ,
885and
886.Fn vsprintf
887functions
888conform to
9385eb3d
A
889.St -ansiC
890and
891.St -isoC-99 .
892With the same reservation, the
893.Fn snprintf
894and
895.Fn vsnprintf
896functions conform to
1f2f436a
A
897.St -isoC-99 ,
898while
899.Fn dprintf
900and
901.Fn vdprintf
902conform to
903.St -p1003.1-2008 .
5b2abdfb
A
904.Sh HISTORY
905The functions
906.Fn asprintf
907and
908.Fn vasprintf
909first appeared in the
910.Tn GNU C
911library.
912These were implemented by
913.An Peter Wemm Aq peter@FreeBSD.org
914in
915.Fx 2.2 ,
916but were later replaced with a different implementation
917from
918.An Todd C. Miller Aq Todd.Miller@courtesan.com
919for
920.Ox 2.3 .
1f2f436a
A
921The
922.Fn dprintf
5b2abdfb 923and
1f2f436a
A
924.Fn vdprintf
925functions were added in
926.Fx 8.0 .
927.Sh BUGS
9385eb3d
A
928The
929.Nm
9385eb3d
A
930family of functions do not correctly handle multibyte characters in the
931.Fa format
932argument.