xref: /netbsd-src/lib/libc/stdio/printf.3 (revision 5aefcfdc06931dd97e76246d2fe0302f7b3fe094)
1.\"	$NetBSD: printf.3,v 1.15 2000/12/29 15:22:51 kleink Exp $
2.\"
3.\" Copyright (c) 1990, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
9.\"
10.\" Redistribution and use in source and binary forms, with or without
11.\" modification, are permitted provided that the following conditions
12.\" are met:
13.\" 1. Redistributions of source code must retain the above copyright
14.\"    notice, this list of conditions and the following disclaimer.
15.\" 2. Redistributions in binary form must reproduce the above copyright
16.\"    notice, this list of conditions and the following disclaimer in the
17.\"    documentation and/or other materials provided with the distribution.
18.\" 3. All advertising materials mentioning features or use of this software
19.\"    must display the following acknowledgement:
20.\"	This product includes software developed by the University of
21.\"	California, Berkeley and its contributors.
22.\" 4. Neither the name of the University nor the names of its contributors
23.\"    may be used to endorse or promote products derived from this software
24.\"    without specific prior written permission.
25.\"
26.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36.\" SUCH DAMAGE.
37.\"
38.\"     @(#)printf.3	8.1 (Berkeley) 6/4/93
39.\"
40.Dd June 4, 1993
41.Dt PRINTF 3
42.Os
43.Sh NAME
44.Nm printf ,
45.Nm fprintf ,
46.Nm sprintf ,
47.Nm snprintf ,
48.Nm asprintf ,
49.Nm vprintf ,
50.Nm vfprintf,
51.Nm vsprintf ,
52.Nm vsnprintf ,
53.Nm vasprintf
54.Nd formatted output conversion
55.Sh LIBRARY
56.Lb libc
57.Sh SYNOPSIS
58.Fd #include <stdio.h>
59.Ft int
60.Fn printf "const char * restrict format" ...
61.Ft int
62.Fn fprintf "FILE * restrict stream" "const char * restrict format" ...
63.Ft int
64.Fn sprintf "char * restrict str" "const char * restrict format" ...
65.Ft int
66.Fn snprintf "char * restrict str" "size_t size" "const char * restrict format" ...
67.Ft int
68.Fn asprintf "char ** restrict ret" "const char * restrict format" ...
69.Fd #include <stdarg.h>
70.Ft int
71.Fn vprintf "const char * restrict format" "va_list ap"
72.Ft int
73.Fn vfprintf "FILE * restrict stream" "const char * restrict format" "va_list ap"
74.Ft int
75.Fn vsprintf "char * restrict str" "const char * restrict format" "va_list ap"
76.Ft int
77.Fn vsnprintf "char * restrict str" "size_t size" "const char * restrict format" "va_list ap"
78.Ft int
79.Fn vasprintf "char ** restrict ret" "const char * restrict format" "va_list ap"
80.Sh DESCRIPTION
81The
82.Fn printf
83family of functions produces output according to a
84.Fa format
85as described below.
86.Fn printf
87and
88.Fn vprintf
89write output to
90.Em stdout,
91the standard output stream;
92.Fn fprintf
93and
94.Fn vfprintf
95write output to the given output
96.Fa stream ;
97.Fn sprintf ,
98.Fn snprintf ,
99.Fn vsprintf ,
100and
101.Fn vsnprintf
102write to the character string
103.Fa str ;
104.Fn asprintf
105and
106.Fn vasprintf
107write to a dynamically allocated string that is stored in
108.Fa ret .
109.Pp
110These functions write the output under the control of a
111.Fa format
112string that specifies how subsequent arguments
113(or arguments accessed via the variable-length argument facilities of
114.Xr stdarg 3 )
115are converted for output.
116.Pp
117These functions return
118the number of characters printed
119(not including the trailing
120.Ql \e0
121used to end output to strings).
122.Pp
123.Fn asprintf
124and
125.Fn vasprintf
126return a pointer to a buffer sufficiently large to hold the
127string in the
128.Fa ret
129argument.
130This pointer should be passed to
131.Xr free 3
132to release the allocated storage when it is no longer needed.
133If sufficient space cannot be allocated, these functions
134will return -1 and set
135.Fa ret
136to be a NULL pointer.
137.Pp
138.Fn snprintf
139and
140.Fn vsnprintf
141will write at most
142.Fa size Ns \-1
143of the characters printed into the output string
144(the
145.Fa size Ns 'th
146character then gets the terminating
147.Ql \e0 ) ;
148if the return value is greater than or equal to the
149.Fa size
150argument, the string was too short
151and some of the printed characters were discarded.
152If
153.Fa size
154is zero, nothing is written and
155.Fa str
156may be a NULL pointer.
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 character
174.Cm % .
175The arguments must correspond properly (after type promotion)
176with the conversion specifier.
177After the
178.Cm % ,
179the following appear in sequence:
180.Bl -bullet
181.It
182Zero or more of the following flags:
183.Bl -hyphen
184.It
185A
186.Cm #
187character
188specifying that the value should be converted to an ``alternative form''.
189For
190.Cm c ,
191.Cm d ,
192.Cm i ,
193.Cm n ,
194.Cm p ,
195.Cm s ,
196and
197.Cm u ,
198conversions, this option has no effect.
199For
200.Cm o
201conversions, the precision of the number is increased to force the first
202character of the output string to a zero (except if a zero value is printed
203with an explicit precision of zero).
204For
205.Cm x
206and
207.Cm X
208conversions, a non-zero result has the string
209.Ql 0x
210(or
211.Ql 0X
212for
213.Cm X
214conversions) prepended to it.
215For
216.Cm e ,
217.Cm E ,
218.Cm f ,
219.Cm g ,
220and
221.Cm G ,
222conversions, the result will always contain a decimal point, even if no
223digits follow it (normally, a decimal point appears in the results of
224those conversions only if a digit follows).
225For
226.Cm g
227and
228.Cm G
229conversions, trailing zeros are not removed from the result as they
230would otherwise be.
231.It
232A zero
233.Sq Cm \&0
234character specifying zero padding.
235For all conversions except
236.Cm n ,
237the converted value is padded on the left with zeros rather than blanks.
238If a precision is given with a numeric conversion
239.Pf ( Cm d ,
240.Cm i ,
241.Cm o ,
242.Cm u ,
243.Cm i ,
244.Cm x ,
245and
246.Cm X ) ,
247the
248.Sq Cm \&0
249flag is ignored.
250.It
251A negative field width flag
252.Sq Cm \-
253indicates the converted value is to be left adjusted on the field boundary.
254Except for
255.Cm n
256conversions, the converted value is padded on the right with blanks,
257rather than on the left with blanks or zeros.
258A
259.Sq Cm \-
260overrides a
261.Sq Cm \&0
262if both are given.
263.It
264A space, specifying that a blank should be left before a positive number
265produced by a signed conversion
266.Pf ( Cm d ,
267.Cm e ,
268.Cm E ,
269.Cm f ,
270.Cm g ,
271.Cm G ,
272or
273.Cm i ) .
274.It
275A
276.Sq Cm +
277character specifying that a sign always be placed before a
278number produced by a signed conversion.
279A
280.Sq Cm +
281overrides a space if both are used.
282.El
283.It
284An optional decimal digit string specifying a minimum field width.
285If the converted value has fewer characters than the field width, it will
286be padded with spaces on the left (or right, if the left-adjustment
287flag has been given) to fill out
288the field width.
289.It
290An optional precision, in the form of a period
291.Sq Cm \&.
292followed by an
293optional digit string.  If the digit string is omitted, the precision
294is taken as zero.  This gives the minimum number of digits to appear for
295.Cm d ,
296.Cm i ,
297.Cm o ,
298.Cm u ,
299.Cm x ,
300and
301.Cm X
302conversions, the number of digits to appear after the decimal-point for
303.Cm e ,
304.Cm E ,
305and
306.Cm f
307conversions, the maximum number of significant digits for
308.Cm g
309and
310.Cm G
311conversions, or the maximum number of characters to be printed from a
312string for
313.Cm s
314conversions.
315.It
316The optional character
317.Cm h ,
318specifying that a following
319.Cm d ,
320.Cm i ,
321.Cm o ,
322.Cm u ,
323.Cm x ,
324or
325.Cm X
326conversion corresponds to a
327.Em short int
328or
329.Em unsigned short int
330argument, or that a following
331.Cm n
332conversion corresponds to a pointer to a
333.Em short int
334argument.
335.It
336The optional character
337.Cm l
338(ell) specifying that a following
339.Cm d ,
340.Cm i ,
341.Cm o ,
342.Cm u ,
343.Cm x ,
344or
345.Cm X
346conversion applies to a pointer to a
347.Em long int
348or
349.Em unsigned long int
350argument, or that a following
351.Cm n
352conversion corresponds to a pointer to a
353.Em long int
354argument.
355.It
356The optional character
357.Cm q ,
358or alternatively two consecutive
359.Cm l
360(ell) characters,
361specifying that a following
362.Cm d ,
363.Cm i ,
364.Cm o ,
365.Cm u ,
366.Cm x ,
367or
368.Cm X
369conversion corresponds to a
370.Em quad_t
371or
372.Em u_quad_t
373argument, or that a following
374.Cm n
375conversion corresponds to a pointer to a
376.Em quad_t
377argument.
378.It
379The character
380.Cm L
381specifying that a following
382.Cm e ,
383.Cm E ,
384.Cm f ,
385.Cm g ,
386or
387.Cm G
388conversion corresponds to a
389.Em long double
390argument (but note that long double values are not currently supported
391by the
392.Tn VAX
393and
394.Tn Tahoe
395compilers).
396.It
397A character that specifies the type of conversion to be applied.
398.El
399.Pp
400A field width or precision, or both, may be indicated by
401an asterisk
402.Ql *
403instead of a
404digit string.
405In this case, an
406.Em int
407argument supplies the field width or precision.
408A negative field width is treated as a left adjustment flag followed by a
409positive field width; a negative precision is treated as though it were
410missing.
411.Pp
412The conversion specifiers and their meanings are:
413.Bl -tag -width "diouxX"
414.It Cm diouxX
415The
416.Em int
417(or appropriate variant) argument is converted to signed decimal
418.Pf ( Cm d
419and
420.Cm i ) ,
421unsigned octal
422.Pq Cm o ,
423unsigned decimal
424.Pq Cm u ,
425or unsigned hexadecimal
426.Pf ( Cm x
427and
428.Cm X )
429notation.  The letters
430.Cm abcdef
431are used for
432.Cm x
433conversions; the letters
434.Cm ABCDEF
435are used for
436.Cm X
437conversions.
438The precision, if any, gives the minimum number of digits that must
439appear; if the converted value requires fewer digits, it is padded on
440the left with zeros.
441.It Cm DOU
442The
443.Em long int
444argument is converted to signed decimal, unsigned octal, or unsigned
445decimal, as if the format had been
446.Cm ld ,
447.Cm lo ,
448or
449.Cm lu
450respectively.
451These conversion characters are deprecated, and will eventually disappear.
452.It Cm eE
453The
454.Em double
455argument is rounded and converted in the style
456.Sm off
457.Pf [\-]d Cm \&. No ddd Cm e No \\*(Pmdd
458.Sm on
459where there is one digit before the
460decimal-point character
461and the number of digits after it is equal to the precision;
462if the precision is missing,
463it is taken as 6; if the precision is
464zero, no decimal-point character appears.
465An
466.Cm E
467conversion uses the letter
468.Cm E
469(rather than
470.Cm e )
471to introduce the exponent.
472The exponent always contains at least two digits; if the value is zero,
473the exponent is 00.
474.It Cm f
475The
476.Em double
477argument is rounded and converted to decimal notation in the style
478.Sm off
479.Pf [-]ddd Cm \&. No ddd ,
480.Sm on
481where the number of digits after the decimal-point character
482is equal to the precision specification.
483If the precision is missing, it is taken as 6; if the precision is
484explicitly zero, no decimal-point character appears.
485If a decimal point appears, at least one digit appears before it.
486.It Cm g
487The
488.Em double
489argument is converted in style
490.Cm f
491or
492.Cm e
493(or
494.Cm E
495for
496.Cm G
497conversions).
498The precision specifies the number of significant digits.
499If the precision is missing, 6 digits are given; if the precision is zero,
500it is treated as 1.
501Style
502.Cm e
503is used if the exponent from its conversion is less than -4 or greater than
504or equal to the precision.
505Trailing zeros are removed from the fractional part of the result; a
506decimal point appears only if it is followed by at least one digit.
507.It Cm c
508The
509.Em int
510argument is converted to an
511.Em unsigned char ,
512and the resulting character is written.
513.It Cm s
514The
515.Dq Em char *
516argument is expected to be a pointer to an array of character type (pointer
517to a string).
518Characters from the array are written up to (but not including)
519a terminating
520.Dv NUL
521character;
522if a precision is specified, no more than the number specified are
523written.
524If a precision is given, no null character
525need be present; if the precision is not specified, or is greater than
526the size of the array, the array must contain a terminating
527.Dv NUL
528character.
529.It Cm p
530The
531.Dq Em void *
532pointer argument is printed in hexadecimal (as if by
533.Ql %#x
534or
535.Ql %#lx ) .
536.It Cm n
537The number of characters written so far is stored into the
538integer indicated by the
539.Dq Em int *
540(or variant) pointer argument.
541No argument is converted.
542.It Cm %
543A
544.Ql %
545is written. No argument is converted. The complete conversion specification
546is
547.Ql %% .
548.El
549.Pp
550In no case does a non-existent or small field width cause truncation of
551a field; if the result of a conversion is wider than the field width, the
552field is expanded to contain the conversion result.
553.Pp
554.Sh EXAMPLES
555.br
556To print a date and time in the form `Sunday, July 3, 10:02',
557where
558.Em weekday
559and
560.Em month
561are pointers to strings:
562.Bd -literal -offset indent
563#include <stdio.h>
564fprintf(stdout, "%s, %s %d, %.2d:%.2d\en",
565	weekday, month, day, hour, min);
566.Ed
567.Pp
568To print \*(Pi
569to five decimal places:
570.Bd -literal -offset indent
571#include <math.h>
572#include <stdio.h>
573fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0));
574.Ed
575.Pp
576To allocate a 128 byte string and print into it:
577.Bd -literal -offset indent
578#include <stdio.h>
579#include <stdlib.h>
580#include <stdarg.h>
581char *newfmt(const char *fmt, ...)
582{
583		char *p;
584		va_list ap;
585		if ((p = malloc(128)) == NULL)
586			return (NULL);
587		va_start(ap, fmt);
588		(void) vsnprintf(p, 128, fmt, ap);
589		va_end(ap);
590		return (p);
591}
592.Ed
593.Sh SEE ALSO
594.Xr printf 1 ,
595.Xr scanf 3
596.Sh STANDARDS
597The
598.Fn fprintf ,
599.Fn printf ,
600.Fn sprintf ,
601.Fn vprintf ,
602.Fn vfprintf ,
603and
604.Fn vsprintf
605functions
606conform to
607.St -ansiC .
608.Sh HISTORY
609The functions
610.Fn snprintf
611and
612.Fn vsnprintf
613first appeared in
614.Bx 4.4 .
615The functions
616.Fn asprintf
617and
618.Fn vasprintf
619are modeled on the ones that first appeared in the GNU C library.
620.Sh BUGS
621The conversion formats
622.Cm \&%D ,
623.Cm \&%O ,
624and
625.Cm %U
626are not standard and are provided only for backward compatibility.
627The effect of padding the
628.Cm %p
629format with zeros (either by the
630.Sq Cm 0
631flag or by specifying a precision), and the benign effect (i.e. none)
632of the
633.Sq Cm #
634flag on
635.Cm %n
636and
637.Cm %p
638conversions, as well as other nonsensical combinations such as
639.Cm %Ld ,
640are not standard; such combinations should be avoided.
641.Sh SECURITY CONSIDERATIONS
642Because
643.Fn sprintf
644and
645.Fn vsprintf
646assume an infinitely long string, callers must be careful not to
647overflow the actual space; this is often impossible to assure.
648For safety, programmers should use the
649.Fn snprintf
650and
651.Fn asprintf
652family of interfaces instead.
653Unfortunately, the
654.Fn snprintf
655interfaces are not available on older
656systems and the
657.Fn asprintf
658interfaces are not yet portable.
659