xref: /freebsd/lib/libc/stdio/printf.3 (revision 1a27f4f2c3efb03d2f035757d856beff7ddf480f)
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.It Cm C
544Treated as
545.Cm c
546with the
547.Cm l
548(ell) modifier.
549.It Cm c
550The
551.Vt int
552argument is converted to an
553.Vt unsigned char ,
554and the resulting character is written.
555.Pp
556If the
557.Cm l
558(ell) modifier is used, the
559.Vt wint_t
560argument shall be converted to a
561.Vt wchar_t ,
562and the (potentially multi-byte) sequence representing the
563single wide character is written, including any shift sequences.
564If a shift sequence is used, the shift state is also restored
565to the original state after the character.
566.It Cm S
567Treated as
568.Cm s
569with the
570.Cm l
571(ell) modifier.
572.It Cm s
573The
574.Vt char *
575argument is expected to be a pointer to an array of character type (pointer
576to a string).
577Characters from the array are written up to (but not including)
578a terminating
579.Dv NUL
580character;
581if a precision is specified, no more than the number specified are
582written.
583If a precision is given, no null character
584need be present; if the precision is not specified, or is greater than
585the size of the array, the array must contain a terminating
586.Dv NUL
587character.
588.Pp
589If the
590.Cm l
591(ell) modifier is used, the
592.Vt wchar_t *
593argument is expected to be a pointer to an array of wide characters
594(pointer to a wide string).
595For each wide character in the string, the (potentially multi-byte)
596sequence representing the
597wide character is written, including any shift sequences.
598If any shift sequence is used, the shift state is also restored
599to the original state after the string.
600Wide characters from the array are written up to (but not including)
601a terminating wide
602.Dv NUL
603character;
604if a precision is specified, no more than the number of bytes specified are
605written (including shift sequences).  Partial characters are never written.
606If a precision is given, no null character
607need be present; if the precision is not specified, or is greater than
608the number of bytes required to render the multibyte representation of
609the string, the array must contain a terminating wide
610.Dv NUL
611character.
612.It Cm p
613The
614.Vt void *
615pointer argument is printed in hexadecimal (as if by
616.Ql %#x
617or
618.Ql %#lx ) .
619.It Cm n
620The number of characters written so far is stored into the
621integer indicated by the
622.Vt int *
623(or variant) pointer argument.
624No argument is converted.
625.It Cm %
626A
627.Ql %
628is written.
629No argument is converted.
630The complete conversion specification
631is
632.Ql %% .
633.El
634.Pp
635The decimal point
636character is defined in the program's locale (category
637.Dv LC_NUMERIC ) .
638.Pp
639In no case does a non-existent or small field width cause truncation of
640a numeric field; if the result of a conversion is wider than the field
641width, the
642field is expanded to contain the conversion result.
643.Sh EXAMPLES
644To print a date and time in the form
645.Dq Li "Sunday, July 3, 10:02" ,
646where
647.Fa weekday
648and
649.Fa month
650are pointers to strings:
651.Bd -literal -offset indent
652#include <stdio.h>
653fprintf(stdout, "%s, %s %d, %.2d:%.2d\en",
654	weekday, month, day, hour, min);
655.Ed
656.Pp
657To print \*(Pi
658to five decimal places:
659.Bd -literal -offset indent
660#include <math.h>
661#include <stdio.h>
662fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0));
663.Ed
664.Pp
665To allocate a 128 byte string and print into it:
666.Bd -literal -offset indent
667#include <stdio.h>
668#include <stdlib.h>
669#include <stdarg.h>
670char *newfmt(const char *fmt, ...)
671{
672		char *p;
673		va_list ap;
674		if ((p = malloc(128)) == NULL)
675			return (NULL);
676		va_start(ap, fmt);
677		(void) vsnprintf(p, 128, fmt, ap);
678		va_end(ap);
679		return (p);
680}
681.Ed
682.Sh SEE ALSO
683.Xr printf 1 ,
684.Xr scanf 3 ,
685.Xr setlocale 3
686.Sh STANDARDS
687The
688.Fn fprintf ,
689.Fn printf ,
690.Fn sprintf ,
691.Fn vprintf ,
692.Fn vfprintf ,
693and
694.Fn vsprintf
695functions
696conform to
697.St -ansiC
698and
699.St -isoC-99 .
700The
701.Fn snprintf
702and
703.Fn vsnprintf
704functions conform to
705.St -isoC-99 .
706.Sh HISTORY
707The functions
708.Fn asprintf
709and
710.Fn vasprintf
711first appeared in the
712.Tn GNU C
713library.
714These were implemented by
715.An Peter Wemm Aq peter@FreeBSD.org
716in
717.Fx 2.2 ,
718but were later replaced with a different implementation
719from
720.An Todd C. Miller Aq Todd.Miller@courtesan.com
721for
722.Ox 2.3 .
723.Sh BUGS
724The conversion formats
725.Cm \&%D , \&%O ,
726and
727.Cm %U
728are not standard and
729are provided only for backward compatibility.
730The effect of padding the
731.Cm %p
732format with zeros (either by the
733.Cm 0
734flag or by specifying a precision), and the benign effect (i.e., none)
735of the
736.Cm #
737flag on
738.Cm %n
739and
740.Cm %p
741conversions, as well as other
742nonsensical combinations such as
743.Cm %Ld ,
744are not standard; such combinations
745should be avoided.
746.Pp
747Because
748.Fn sprintf
749and
750.Fn vsprintf
751assume an infinitely long string,
752callers must be careful not to overflow the actual space;
753this is often hard to assure.
754For safety, programmers should use the
755.Fn snprintf
756interface instead.
757Unfortunately, this interface was only defined in
758.St -isoC-99 .
759.Pp
760.Cm %n
761can be used to write arbitrary data to the stack.
762Programmers are therefore strongly advised to never pass untrusted strings
763as the
764.Fa format
765argument.
766.Pp
767Never pass a string with user-supplied data as a format without using
768.Ql %s .
769An attacker can put format specifiers in the string to mangle your stack,
770leading to a possible security hole.
771This holds true even if the string was built using a function like
772.Fn snprintf ,
773as the resulting string may still contain user-supplied conversion specifiers
774for later interpolation by
775.Fn printf .
776.Pp
777Always use the proper secure idiom:
778.Pp
779.Dl snprintf(buffer, sizeof(buffer), \*q%s\*q, string);
780.Pp
781The
782.Nm
783family of functions currently lack the ability to use the
784.Qq '
785flag in conjunction with the
786.Qq f
787conversion specifier.  The
788.Qq a
789and
790.Qq A
791conversion specifiers have not yet been implemented.
792The
793.Qq l
794(ell) modifier for the
795.Qq c
796and
797.Qq s
798conversion specifiers, for wide characters and strings, have not yet
799been implemented.
800The
801.Qq L
802modifier for floating point formats simply round the
803.Vt long double
804argument to
805.Vt double ,
806providing no additional precision.
807