xref: /freebsd/lib/libc/stdio/printf.3 (revision 8fa113e5fc65fe6abc757f0089f477a87ee4d185)
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.
16.\" 3. All advertising materials mentioning features or use of this software
17.\"    must display the following acknowledgement:
18.\"	This product includes software developed by the University of
19.\"	California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\"    may be used to endorse or promote products derived from this software
22.\"    without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\"     @(#)printf.3	8.1 (Berkeley) 6/4/93
37.\" $FreeBSD$
38.\"
39.Dd November 8, 2001
40.Dt PRINTF 3
41.Os
42.Sh NAME
43.Nm printf , fprintf , sprintf , snprintf , asprintf ,
44.Nm vprintf , vfprintf, vsprintf , vsnprintf , vasprintf
45.Nd formatted output conversion
46.Sh LIBRARY
47.Lb libc
48.Sh SYNOPSIS
49.In stdio.h
50.Ft int
51.Fn printf "const char *format" ...
52.Ft int
53.Fn fprintf "FILE *stream" "const char *format" ...
54.Ft int
55.Fn sprintf "char *str" "const char *format" ...
56.Ft int
57.Fn snprintf "char *str" "size_t size" "const char *format" ...
58.Ft int
59.Fn asprintf "char **ret" "const char *format" ...
60.In stdarg.h
61.Ft int
62.Fn vprintf "const char *format" "va_list ap"
63.Ft int
64.Fn vfprintf "FILE *stream" "const char *format" "va_list ap"
65.Ft int
66.Fn vsprintf "char *str" "const char *format" "va_list ap"
67.Ft int
68.Fn vsnprintf "char *str" "size_t size" "const char *format" "va_list ap"
69.Ft int
70.Fn vasprintf "char **ret" "const char *format" "va_list ap"
71.Sh DESCRIPTION
72The
73.Fn printf
74family of functions produces output according to a
75.Fa format
76as described below.
77.Fn Printf
78and
79.Fn vprintf
80write output to
81.Pa stdout ,
82the standard output stream;
83.Fn fprintf
84and
85.Fn vfprintf
86write output to the given output
87.Fa stream ;
88.Fn sprintf ,
89.Fn snprintf ,
90.Fn vsprintf ,
91and
92.Fn vsnprintf
93write to the character string
94.Fa str ;
95and
96.Fn asprintf
97and
98.Fn vasprintf
99dynamically allocate a new string with
100.Xr malloc 3 .
101.Pp
102These functions write the output under the control of a
103.Fa format
104string that specifies how subsequent arguments
105(or arguments accessed via the variable-length argument facilities of
106.Xr stdarg 3 )
107are converted for output.
108.Pp
109These functions return the number of characters printed
110(not including the trailing
111.Ql \e0
112used to end output to strings),
113except for
114.Fn snprintf
115and
116.Fn vsnprintf ,
117which return the number of characters that would have been printed if the
118.Fa size
119were unlimited
120(again, not including the final
121.Ql \e0 ) .
122.Pp
123.Fn Asprintf
124and
125.Fn vasprintf
126set
127.Fa *ret
128to be a pointer to a buffer sufficiently large to hold the formatted string.
129This pointer should be passed to
130.Xr free 3
131to release the allocated storage when it is no longer needed.
132If sufficient space cannot be allocated,
133.Fn asprintf
134and
135.Fn vasprintf
136will return -1 and set
137.Fa ret
138to be a
139.Dv NULL
140pointer.
141.Pp
142.Fn Snprintf
143and
144.Fn vsnprintf
145will write at most
146.Fa size Ns \-1
147of the characters printed into the output string
148(the
149.Fa size Ns 'th
150character then gets the terminating
151.Ql \e0 ) ;
152if the return value is greater than or equal to the
153.Fa size
154argument, the string was too short
155and some of the printed characters were discarded.
156The output is always null-terminated.
157.Pp
158.Fn Sprintf
159and
160.Fn vsprintf
161effectively assume an infinite
162.Fa size .
163.Pp
164The format string is composed of zero or more directives:
165ordinary
166.\" multibyte
167characters (not
168.Cm % ) ,
169which are copied unchanged to the output stream;
170and conversion specifications, each of which results
171in fetching zero or more subsequent arguments.
172Each conversion specification is introduced by
173the
174.Cm %
175character.
176The arguments must correspond properly (after type promotion)
177with the conversion specifier.
178After the
179.Cm % ,
180the following appear in sequence:
181.Bl -bullet
182.It
183An optional field, consisting of a decimal digit string followed by a
184.Cm $ ,
185specifying the next argument to access.
186If this field is not provided, the argument following the last
187argument accessed will be used.
188Arguments are numbered starting at
189.Cm 1 .
190If unaccessed arguments in the format string are interspersed with ones that
191are accessed the results will be indeterminate.
192.It
193Zero or more of the following flags:
194.Bl -tag -width ".So \&\ \& Sc (space)" -compact -offset indent
195.It Sq Cm #
196The value should be converted to an
197.Dq alternate form .
198For
199.Cm c , d , i , n , p , s ,
200and
201.Cm u
202conversions, this option has no effect.
203For
204.Cm o
205conversions, the precision of the number is increased to force the first
206character of the output string to a zero (except if a zero value is printed
207with an explicit precision of zero).
208For
209.Cm x
210and
211.Cm X
212conversions, a non-zero result has the string
213.Ql 0x
214(or
215.Ql 0X
216for
217.Cm X
218conversions) prepended to it.
219For
220.Cm a , A , e , E , f , F , g ,
221and
222.Cm G
223conversions, the result will always contain a decimal point, even if no
224digits follow it (normally, a decimal point appears in the results of
225those conversions only if a digit follows).
226For
227.Cm g
228and
229.Cm G
230conversions, trailing zeros are not removed from the result as they
231would otherwise be.
232.It So Cm 0 Sc (zero)
233Zero padding.
234For all conversions except
235.Cm n ,
236the converted value is padded on the left with zeros rather than blanks.
237If a precision is given with a numeric conversion
238.Cm ( d , i , o , u , i , x ,
239and
240.Cm X ) ,
241the
242.Cm 0
243flag is ignored.
244.It Sq Cm \&\-
245A negative field width flag;
246the converted value is to be left adjusted on the field boundary.
247Except for
248.Cm n
249conversions, the converted value is padded on the right with blanks,
250rather than on the left with blanks or zeros.
251A
252.Cm \-
253overrides a
254.Cm 0
255if both are given.
256.It So \&\ \& Sc (space)
257A blank should be left before a positive number
258produced by a signed conversion
259.Cm ( a , A , d , e , E , f , F , g , G ,
260or
261.Cm i ) .
262.It Sq Cm +
263A sign must always be placed before a
264number produced by a signed conversion.
265A
266.Cm +
267overrides a space if both are used.
268.It Sq Cm '
269Decimal conversions
270.Cm ( d , u ,
271or
272.Cm i )
273or the integral portion of a floating point conversion
274.Cm ( f
275or
276.Cm F )
277should be grouped and separated by thousands using
278the non-monetary seperator returned by
279.Xr localeconv 3 .
280.El
281.It
282An optional decimal digit string specifying a minimum field width.
283If the converted value has fewer characters than the field width, it will
284be padded with spaces on the left (or right, if the left-adjustment
285flag has been given) to fill out
286the field width.
287.It
288An optional precision, in the form of a period
289.Cm \&.
290followed by an
291optional digit string.
292If the digit string is omitted, the precision is taken as zero.
293This gives the minimum number of digits to appear for
294.Cm d , i , o , u , x ,
295and
296.Cm X
297conversions, the number of digits to appear after the decimal-point for
298.Cm a , A , e , E , f ,
299and
300.Cm F
301conversions, the maximum number of significant digits for
302.Cm g
303and
304.Cm G
305conversions, or the maximum number of characters to be printed from a
306string for
307.Cm s
308conversions.
309.It
310An optional length modifier, that specifies the size of the argument.
311The following length modifiers are valid for the
312.Cm d , i , n , o , u , x ,
313or
314.Cm X
315conversion:
316.Bl -column ".Cm q Em (deprecated)" ".Vt signed char" ".Vt unsigned long long" ".Vt unsigned long long *"
317.It Sy Modifier Ta Cm d , i Ta Cm o , u , x , X Ta Cm n
318.It Cm hh Ta Vt signed char Ta Vt unsigned char Ta Vt signed char *
319.It Cm h Ta Vt short Ta Vt unsigned short Ta Vt short *
320.It Cm l No (ell) Ta Vt long Ta Vt unsigned long Ta Vt long *
321.It Cm ll No (ell ell) Ta Vt long long Ta Vt unsigned long long Ta Vt long long *
322.It Cm j Ta Vt intmax_t Ta Vt uintmax_t Ta Vt intmax_t *
323.It Cm t Ta Vt ptrdiff_t Ta Sy * Ta Vt ptrdiff_t *
324.It Cm z Ta Sy * Ta Vt size_t Ta Sy *
325.It Cm q Em (deprecated) Ta Vt quad_t Ta Vt u_quad_t Ta Vt quad_t *
326.El
327.Pp
328.Bl -tag -width ".Cm * No -"
329.It Cm * No -
330The
331.Cm t
332modifier, when applied to a
333.Cm o , u , x ,
334or
335.Cm X
336conversion, indicates that the argument is of an unsigned type
337equivalent in size to a
338.Vt ptrdiff_t .
339The
340.Cm z
341modifier, when applied to a
342.Cm d or
343.Cm i
344conversion, indicates that the argument is of a signed type equivalent in
345size to a
346.Vt size_t .
347Similarly, when applied to an
348.Cm n
349conversion, it indicates that the argument is a pointer to a signed type
350equivalent in size to a
351.Vt size_t .
352.El
353.Pp
354The following length modifier is valid for the
355.Cm a , A , e , E , f , F , g ,
356or
357.Cm G
358conversion:
359.Bl -column ".Sy Modifier" ".Cm a , A , e , E , f , F , g , G"
360.It Sy Modifier Ta Cm a , A , e , E , f , F , g , G
361.It Cm L Ta Vt long double
362.El
363.Pp
364The following length modifier is valid for the
365.Cm c
366or
367.Cm s
368conversion:
369.Bl -column ".Sy Modifier" ".Vt wint_t" ".Vt wchar_t *"
370.It Sy Modifier Ta Cm c Ta Cm s
371.It Cm l No (ell) Ta Vt wint_t Ta Vt wchar_t *
372.El
373.It
374A character that specifies the type of conversion to be applied.
375.El
376.Pp
377A field width or precision, or both, may be indicated by
378an asterisk
379.Ql *
380or an asterisk followed by one or more decimal digits and a
381.Ql $
382instead of a
383digit string.
384In this case, an
385.Vt int
386argument supplies the field width or precision.
387A negative field width is treated as a left adjustment flag followed by a
388positive field width; a negative precision is treated as though it were
389missing.
390If a single format directive mixes positional (nn$)
391and non-positional arguments, the results are undefined.
392.Pp
393The conversion specifiers and their meanings are:
394.Bl -tag -width "diouxX"
395.It Cm diouxX
396The
397.Vt int
398(or appropriate variant) argument is converted to signed decimal
399.Cm ( d
400and
401.Cm i ) ,
402unsigned octal
403.Pq Cm o ,
404unsigned decimal
405.Pq Cm u ,
406or unsigned hexadecimal
407.Cm ( x
408and
409.Cm X )
410notation.
411The letters
412.Cm abcdef
413are used for
414.Cm x
415conversions; the letters
416.Cm ABCDEF
417are used for
418.Cm X
419conversions.
420The precision, if any, gives the minimum number of digits that must
421appear; if the converted value requires fewer digits, it is padded on
422the left with zeros.
423.It Cm DOU
424The
425.Vt long int
426argument is converted to signed decimal, unsigned octal, or unsigned
427decimal, as if the format had been
428.Cm ld , lo ,
429or
430.Cm lu
431respectively.
432These conversion characters are deprecated, and will eventually disappear.
433.It Cm eE
434The
435.Vt double
436argument is rounded and converted in the style
437.Oo \- Oc Ns d Ns Cm \&. Ns ddd Ns Cm e Ns \\*[Pm]dd
438where there is one digit before the
439decimal-point character
440and the number of digits after it is equal to the precision;
441if the precision is missing,
442it is taken as 6; if the precision is
443zero, no decimal-point character appears.
444An
445.Cm E
446conversion uses the letter
447.Cm E
448(rather than
449.Cm e )
450to introduce the exponent.
451The exponent always contains at least two digits; if the value is zero,
452the exponent is 00.
453.Pp
454For
455.Cm a , A , e , E , f , F , g ,
456and
457.Cm G
458conversions, positive and negative infinity are represented as
459.Li inf
460and
461.Li -inf
462respectively when using the lowercase conversion character, and
463.Li INF
464and
465.Li -INF
466respectively when using the uppercase conversion character.
467Similarly, NaN is represented as
468.Li nan
469when using the lowercase conversion, and
470.Li NAN
471when using the uppercase conversion.
472.It Cm fF
473The
474.Vt double
475argument is rounded and converted to decimal notation in the style
476.Oo \- Oc Ns ddd Ns Cm \&. Ns ddd ,
477where the number of digits after the decimal-point character
478is equal to the precision specification.
479If the precision is missing, it is taken as 6; if the precision is
480explicitly zero, no decimal-point character appears.
481If a decimal point appears, at least one digit appears before it.
482.It Cm gG
483The
484.Vt double
485argument is converted in style
486.Cm f
487or
488.Cm e
489(or
490.Cm F
491or
492.Cm E
493for
494.Cm G
495conversions).
496The precision specifies the number of significant digits.
497If the precision is missing, 6 digits are given; if the precision is zero,
498it is treated as 1.
499Style
500.Cm e
501is used if the exponent from its conversion is less than -4 or greater than
502or equal to the precision.
503Trailing zeros are removed from the fractional part of the result; a
504decimal point appears only if it is followed by at least one digit.
505.It Cm aA
506The
507.Vt double
508argument is converted to hexadecimal notation in the style
509.Oo \- Oc Ns 0xh Ns Cm \&. Ns hhhp Ns Oo +- Oc Ns d ,
510where the number of digits after the hexadecimal-point character
511is equal to the precision specification.
512If the precision is missing, it is taken as enough to exactly
513represent the floating-point number; if the precision is
514explicitly zero, no hexadecimal-point character appears.
515This is an exact coversion of the mantissa+exponent internal
516floating point representation; the
517.Oo \- Oc Ns 0xh Ns Cm \&. Ns hhh
518portion represents exactly the mantissa; only denormalized
519mantissas have a zero value to the left of the hexadecimal
520point.
521The
522.Cm p
523is a literal character
524.Qq p ;
525the exponent is preceded by a positive or negative sign
526and is represented in decimal, using only enough characters
527to represent the exponent.
528The
529.Cm A
530conversion uses the prefix
531.Cm 0X
532(rather than
533.Cm 0x ) ,
534the letters
535.Cm ABCDEF
536(rather than
537.Cm abcdef )
538to represent the hex digits, and the letter
539.Cm P
540(rather than
541.Cm p )
542to seperate the mantissa and exponent.
543
544.It Cm C
545Treated as
546.Cm c
547with the
548.Cm l
549(ell) modifier.
550.It Cm c
551The
552.Vt int
553argument is converted to an
554.Vt unsigned char ,
555and the resulting character is written.
556.Pp
557If the
558.Cm l
559(ell) modifier is used, the
560.Vt wint_t
561argument shall be converted to a
562.Vt wchar_t ,
563and the (potentially multi-byte) sequence representing the
564single wide character is written, including any shift sequences.
565If a shift sequence is used, the shift state is also restored
566to the original state after the character.
567.It Cm S
568Treated as
569.Cm s
570with the
571.Cm l
572(ell) modifier.
573.It Cm s
574The
575.Vt char *
576argument is expected to be a pointer to an array of character type (pointer
577to a string).
578Characters from the array are written up to (but not including)
579a terminating
580.Dv NUL
581character;
582if a precision is specified, no more than the number specified are
583written.
584If a precision is given, no null character
585need be present; if the precision is not specified, or is greater than
586the size of the array, the array must contain a terminating
587.Dv NUL
588character.
589.Pp
590If the
591.Cm l
592(ell) modifier is used, the
593.Vt wchar_t *
594argument is expected to be a pointer to an array of wide characters
595(pointer to a wide string).
596For each wide character in the string, the (potentially multi-byte)
597sequence representing the
598wide character is written, including any shift sequences.
599If any shift sequence is used, the shift state is also restored
600to the original state after the string.
601Wide characters from the array are written up to (but not including)
602a terminating wide
603.Dv NUL
604character;
605if a precision is specified, no more than the number of bytes specified are
606written (including shift sequences).  Partial characters are never written.
607If a precision is given, no null character
608need be present; if the precision is not specified, or is greater than
609the number of bytes required to render the multibyte representation of
610the string, the array must contain a terminating wide
611.Dv NUL
612character.
613.It Cm p
614The
615.Vt void *
616pointer argument is printed in hexadecimal (as if by
617.Ql %#x
618or
619.Ql %#lx ) .
620.It Cm n
621The number of characters written so far is stored into the
622integer indicated by the
623.Vt int *
624(or variant) pointer argument.
625No argument is converted.
626.It Cm %
627A
628.Ql %
629is written.
630No argument is converted.
631The complete conversion specification
632is
633.Ql %% .
634.El
635.Pp
636The decimal point
637character is defined in the program's locale (category
638.Dv LC_NUMERIC ) .
639.Pp
640In no case does a non-existent or small field width cause truncation of
641a numeric field; if the result of a conversion is wider than the field
642width, the
643field is expanded to contain the conversion result.
644.Sh EXAMPLES
645To print a date and time in the form
646.Dq Li "Sunday, July 3, 10:02" ,
647where
648.Fa weekday
649and
650.Fa month
651are pointers to strings:
652.Bd -literal -offset indent
653#include <stdio.h>
654fprintf(stdout, "%s, %s %d, %.2d:%.2d\en",
655	weekday, month, day, hour, min);
656.Ed
657.Pp
658To print \*(Pi
659to five decimal places:
660.Bd -literal -offset indent
661#include <math.h>
662#include <stdio.h>
663fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0));
664.Ed
665.Pp
666To allocate a 128 byte string and print into it:
667.Bd -literal -offset indent
668#include <stdio.h>
669#include <stdlib.h>
670#include <stdarg.h>
671char *newfmt(const char *fmt, ...)
672{
673		char *p;
674		va_list ap;
675		if ((p = malloc(128)) == NULL)
676			return (NULL);
677		va_start(ap, fmt);
678		(void) vsnprintf(p, 128, fmt, ap);
679		va_end(ap);
680		return (p);
681}
682.Ed
683.Sh SEE ALSO
684.Xr printf 1 ,
685.Xr scanf 3 ,
686.Xr setlocale 3
687.Sh STANDARDS
688The
689.Fn fprintf ,
690.Fn printf ,
691.Fn sprintf ,
692.Fn vprintf ,
693.Fn vfprintf ,
694and
695.Fn vsprintf
696functions
697conform to
698.St -ansiC
699and
700.St -isoC-99 .
701The
702.Fn snprintf
703and
704.Fn vsnprintf
705functions conform to
706.St -isoC-99 .
707.Sh HISTORY
708The functions
709.Fn asprintf
710and
711.Fn vasprintf
712first appeared in the
713.Tn GNU C
714library.
715These were implemented by
716.An Peter Wemm Aq peter@FreeBSD.org
717in
718.Fx 2.2 ,
719but were later replaced with a different implementation
720from
721.An Todd C. Miller Aq Todd.Miller@courtesan.com
722for
723.Ox 2.3 .
724.Sh BUGS
725The conversion formats
726.Cm \&%D , \&%O ,
727and
728.Cm %U
729are not standard and
730are provided only for backward compatibility.
731The effect of padding the
732.Cm %p
733format with zeros (either by the
734.Cm 0
735flag or by specifying a precision), and the benign effect (i.e., none)
736of the
737.Cm #
738flag on
739.Cm %n
740and
741.Cm %p
742conversions, as well as other
743nonsensical combinations such as
744.Cm %Ld ,
745are not standard; such combinations
746should be avoided.
747.Pp
748Because
749.Fn sprintf
750and
751.Fn vsprintf
752assume an infinitely long string,
753callers must be careful not to overflow the actual space;
754this is often hard to assure.
755For safety, programmers should use the
756.Fn snprintf
757interface instead.
758Unfortunately, this interface was only defined in
759.St -isoC-99 .
760.Pp
761.Cm %n
762can be used to write arbitrary data to the stack.
763Programmers are therefore strongly advised to never pass untrusted strings
764as the
765.Fa format
766argument.
767.Pp
768Never pass a string with user-supplied data as a format without using
769.Ql %s .
770An attacker can put format specifiers in the string to mangle your stack,
771leading to a possible security hole.
772This holds true even if the string was built using a function like
773.Fn snprintf ,
774as the resulting string may still contain user-supplied conversion specifiers
775for later interpolation by
776.Fn printf .
777.Pp
778Always use the proper secure idiom:
779.Pp
780.Dl snprintf(buffer, sizeof(buffer), \*q%s\*q, string);
781.Pp
782The
783.Nm
784family of functions currently lack the ability to use the
785.Qq '
786flag in conjunction with the
787.Qq f
788conversion specifier.  The
789.Qq a
790and
791.Qq A
792conversion specifiers have not yet been implemented.
793The
794.Qq l
795(ell) modifier for the
796.Qq c
797and
798.Qq s
799conversion specifiers, for wide characters and strings, have not yet
800been implemented.
801The
802.Qq L
803modifier for floating point formats simply round the
804.Vt long double
805argument to
806.Vt double ,
807providing no additional precision.
808