xref: /netbsd-src/lib/libc/stdio/printf.3 (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1.\"	$NetBSD: printf.3,v 1.13 1998/09/14 21:10:18 tv 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 *format" ...
61.Ft int
62.Fn fprintf "FILE *stream" "const char *format" ...
63.Ft int
64.Fn sprintf "char *str" "const char *format" ...
65.Ft int
66.Fn snprintf "char *str" "size_t size" "const char *format" ...
67.Ft int
68.Fn asprintf "char **ret" "const char *format" ...
69.Fd #include <stdarg.h>
70.Ft int
71.Fn vprintf "const char *format" "va_list ap"
72.Ft int
73.Fn vfprintf "FILE *stream" "const char *format" "va_list ap"
74.Ft int
75.Fn vsprintf "char *str" "const char *format" "va_list ap"
76.Ft int
77.Fn vsnprintf "char *str" "size_t size" "const char *format" "va_list ap"
78.Ft int
79.Fn vasprintf "char **ret" "const char *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.
152.Pp
153.Fn sprintf
154and
155.Fn vsprintf
156effectively assume an infinite
157.Fa size .
158.Pp
159The format string is composed of zero or more directives:
160ordinary
161.\" multibyte
162characters (not
163.Cm % ) ,
164which are copied unchanged to the output stream;
165and conversion specifications, each of which results
166in fetching zero or more subsequent arguments.
167Each conversion specification is introduced by
168the character
169.Cm % .
170The arguments must correspond properly (after type promotion)
171with the conversion specifier.
172After the
173.Cm % ,
174the following appear in sequence:
175.Bl -bullet
176.It
177Zero or more of the following flags:
178.Bl -hyphen
179.It
180A
181.Cm #
182character
183specifying that the value should be converted to an ``alternative form''.
184For
185.Cm c ,
186.Cm d ,
187.Cm i ,
188.Cm n ,
189.Cm p ,
190.Cm s ,
191and
192.Cm u ,
193conversions, this option has no effect.
194For
195.Cm o
196conversions, the precision of the number is increased to force the first
197character of the output string to a zero (except if a zero value is printed
198with an explicit precision of zero).
199For
200.Cm x
201and
202.Cm X
203conversions, a non-zero result has the string
204.Ql 0x
205(or
206.Ql 0X
207for
208.Cm X
209conversions) prepended to it.
210For
211.Cm e ,
212.Cm E ,
213.Cm f ,
214.Cm g ,
215and
216.Cm G ,
217conversions, the result will always contain a decimal point, even if no
218digits follow it (normally, a decimal point appears in the results of
219those conversions only if a digit follows).
220For
221.Cm g
222and
223.Cm G
224conversions, trailing zeros are not removed from the result as they
225would otherwise be.
226.It
227A zero
228.Sq Cm \&0
229character specifying zero padding.
230For all conversions except
231.Cm n ,
232the converted value is padded on the left with zeros rather than blanks.
233If a precision is given with a numeric conversion
234.Pf ( Cm d ,
235.Cm i ,
236.Cm o ,
237.Cm u ,
238.Cm i ,
239.Cm x ,
240and
241.Cm X ) ,
242the
243.Sq Cm \&0
244flag is ignored.
245.It
246A negative field width flag
247.Sq Cm \-
248indicates the converted value is to be left adjusted on the field boundary.
249Except for
250.Cm n
251conversions, the converted value is padded on the right with blanks,
252rather than on the left with blanks or zeros.
253A
254.Sq Cm \-
255overrides a
256.Sq Cm \&0
257if both are given.
258.It
259A space, specifying that a blank should be left before a positive number
260produced by a signed conversion
261.Pf ( Cm d ,
262.Cm e ,
263.Cm E ,
264.Cm f ,
265.Cm g ,
266.Cm G ,
267or
268.Cm i ) .
269.It
270A
271.Sq Cm +
272character specifying that a sign always be placed before a
273number produced by a signed conversion.
274A
275.Sq Cm +
276overrides a space if both are used.
277.El
278.It
279An optional decimal digit string specifying a minimum field width.
280If the converted value has fewer characters than the field width, it will
281be padded with spaces on the left (or right, if the left-adjustment
282flag has been given) to fill out
283the field width.
284.It
285An optional precision, in the form of a period
286.Sq Cm \&.
287followed by an
288optional digit string.  If the digit string is omitted, the precision
289is taken as zero.  This gives the minimum number of digits to appear for
290.Cm d ,
291.Cm i ,
292.Cm o ,
293.Cm u ,
294.Cm x ,
295and
296.Cm X
297conversions, the number of digits to appear after the decimal-point for
298.Cm e ,
299.Cm E ,
300and
301.Cm f
302conversions, the maximum number of significant digits for
303.Cm g
304and
305.Cm G
306conversions, or the maximum number of characters to be printed from a
307string for
308.Cm s
309conversions.
310.It
311The optional character
312.Cm h ,
313specifying that a following
314.Cm d ,
315.Cm i ,
316.Cm o ,
317.Cm u ,
318.Cm x ,
319or
320.Cm X
321conversion corresponds to a
322.Em short int
323or
324.Em unsigned short int
325argument, or that a following
326.Cm n
327conversion corresponds to a pointer to a
328.Em short int
329argument.
330.It
331The optional character
332.Cm l
333(ell) specifying that a following
334.Cm d ,
335.Cm i ,
336.Cm o ,
337.Cm u ,
338.Cm x ,
339or
340.Cm X
341conversion applies to a pointer to a
342.Em long int
343or
344.Em unsigned long int
345argument, or that a following
346.Cm n
347conversion corresponds to a pointer to a
348.Em long int
349argument.
350.It
351The optional character
352.Cm q ,
353or alternatively two consecutive
354.Cm l
355(ell) characters,
356specifying that a following
357.Cm d ,
358.Cm i ,
359.Cm o ,
360.Cm u ,
361.Cm x ,
362or
363.Cm X
364conversion corresponds to a
365.Em quad_t
366or
367.Em u_quad_t
368argument, or that a following
369.Cm n
370conversion corresponds to a pointer to a
371.Em quad_t
372argument.
373.It
374The character
375.Cm L
376specifying that a following
377.Cm e ,
378.Cm E ,
379.Cm f ,
380.Cm g ,
381or
382.Cm G
383conversion corresponds to a
384.Em long double
385argument (but note that long double values are not currently supported
386by the
387.Tn VAX
388and
389.Tn Tahoe
390compilers).
391.It
392A character that specifies the type of conversion to be applied.
393.El
394.Pp
395A field width or precision, or both, may be indicated by
396an asterisk
397.Ql *
398instead of a
399digit string.
400In this case, an
401.Em int
402argument supplies the field width or precision.
403A negative field width is treated as a left adjustment flag followed by a
404positive field width; a negative precision is treated as though it were
405missing.
406.Pp
407The conversion specifiers and their meanings are:
408.Bl -tag -width "diouxX"
409.It Cm diouxX
410The
411.Em int
412(or appropriate variant) argument is converted to signed decimal
413.Pf ( Cm d
414and
415.Cm i ) ,
416unsigned octal
417.Pq Cm o ,
418unsigned decimal
419.Pq Cm u ,
420or unsigned hexadecimal
421.Pf ( Cm x
422and
423.Cm X )
424notation.  The letters
425.Cm abcdef
426are used for
427.Cm x
428conversions; the letters
429.Cm ABCDEF
430are used for
431.Cm X
432conversions.
433The precision, if any, gives the minimum number of digits that must
434appear; if the converted value requires fewer digits, it is padded on
435the left with zeros.
436.It Cm DOU
437The
438.Em long int
439argument is converted to signed decimal, unsigned octal, or unsigned
440decimal, as if the format had been
441.Cm ld ,
442.Cm lo ,
443or
444.Cm lu
445respectively.
446These conversion characters are deprecated, and will eventually disappear.
447.It Cm eE
448The
449.Em double
450argument is rounded and converted in the style
451.Sm off
452.Pf [\-]d Cm \&. No ddd Cm e No \\*(Pmdd
453.Sm on
454where there is one digit before the
455decimal-point character
456and the number of digits after it is equal to the precision;
457if the precision is missing,
458it is taken as 6; if the precision is
459zero, no decimal-point character appears.
460An
461.Cm E
462conversion uses the letter
463.Cm E
464(rather than
465.Cm e )
466to introduce the exponent.
467The exponent always contains at least two digits; if the value is zero,
468the exponent is 00.
469.It Cm f
470The
471.Em double
472argument is rounded and converted to decimal notation in the style
473.Sm off
474.Pf [-]ddd Cm \&. No ddd ,
475.Sm on
476where the number of digits after the decimal-point character
477is equal to the precision specification.
478If the precision is missing, it is taken as 6; if the precision is
479explicitly zero, no decimal-point character appears.
480If a decimal point appears, at least one digit appears before it.
481.It Cm g
482The
483.Em double
484argument is converted in style
485.Cm f
486or
487.Cm e
488(or
489.Cm E
490for
491.Cm G
492conversions).
493The precision specifies the number of significant digits.
494If the precision is missing, 6 digits are given; if the precision is zero,
495it is treated as 1.
496Style
497.Cm e
498is used if the exponent from its conversion is less than -4 or greater than
499or equal to the precision.
500Trailing zeros are removed from the fractional part of the result; a
501decimal point appears only if it is followed by at least one digit.
502.It Cm c
503The
504.Em int
505argument is converted to an
506.Em unsigned char ,
507and the resulting character is written.
508.It Cm s
509The
510.Dq Em char *
511argument is expected to be a pointer to an array of character type (pointer
512to a string).
513Characters from the array are written up to (but not including)
514a terminating
515.Dv NUL
516character;
517if a precision is specified, no more than the number specified are
518written.
519If a precision is given, no null character
520need be present; if the precision is not specified, or is greater than
521the size of the array, the array must contain a terminating
522.Dv NUL
523character.
524.It Cm p
525The
526.Dq Em void *
527pointer argument is printed in hexadecimal (as if by
528.Ql %#x
529or
530.Ql %#lx ) .
531.It Cm n
532The number of characters written so far is stored into the
533integer indicated by the
534.Dq Em int *
535(or variant) pointer argument.
536No argument is converted.
537.It Cm %
538A
539.Ql %
540is written. No argument is converted. The complete conversion specification
541is
542.Ql %% .
543.El
544.Pp
545In no case does a non-existent or small field width cause truncation of
546a field; if the result of a conversion is wider than the field width, the
547field is expanded to contain the conversion result.
548.Pp
549.Sh EXAMPLES
550.br
551To print a date and time in the form `Sunday, July 3, 10:02',
552where
553.Em weekday
554and
555.Em month
556are pointers to strings:
557.Bd -literal -offset indent
558#include <stdio.h>
559fprintf(stdout, "%s, %s %d, %.2d:%.2d\en",
560	weekday, month, day, hour, min);
561.Ed
562.Pp
563To print \*(Pi
564to five decimal places:
565.Bd -literal -offset indent
566#include <math.h>
567#include <stdio.h>
568fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0));
569.Ed
570.Pp
571To allocate a 128 byte string and print into it:
572.Bd -literal -offset indent
573#include <stdio.h>
574#include <stdlib.h>
575#include <stdarg.h>
576char *newfmt(const char *fmt, ...)
577{
578		char *p;
579		va_list ap;
580		if ((p = malloc(128)) == NULL)
581			return (NULL);
582		va_start(ap, fmt);
583		(void) vsnprintf(p, 128, fmt, ap);
584		va_end(ap);
585		return (p);
586}
587.Ed
588.Sh SEE ALSO
589.Xr printf 1 ,
590.Xr scanf 3
591.Sh STANDARDS
592The
593.Fn fprintf ,
594.Fn printf ,
595.Fn sprintf ,
596.Fn vprintf ,
597.Fn vfprintf ,
598and
599.Fn vsprintf
600functions
601conform to
602.St -ansiC .
603.Sh HISTORY
604The functions
605.Fn snprintf
606and
607.Fn vsnprintf
608first appeared in
609.Bx 4.4 .
610The functions
611.Fn asprintf
612and
613.Fn vasprintf
614are modeled on the ones that first appeared in the GNU C library.
615.Sh BUGS
616The conversion formats
617.Cm \&%D ,
618.Cm \&%O ,
619and
620.Cm %U
621are not standard and are provided only for backward compatibility.
622The effect of padding the
623.Cm %p
624format with zeros (either by the
625.Sq Cm 0
626flag or by specifying a precision), and the benign effect (i.e. none)
627of the
628.Sq Cm #
629flag on
630.Cm %n
631and
632.Cm %p
633conversions, as well as other nonsensical combinations such as
634.Cm %Ld ,
635are not standard; such combinations should be avoided.
636.Sh SECURITY CONSIDERATIONS
637Because
638.Fn sprintf
639and
640.Fn vsprintf
641assume an infinitely long string, callers must be careful not to
642overflow the actual space; this is often impossible to assure.
643For safety, programmers should use the
644.Fn snprintf
645and
646.Fn asprintf
647family of interfaces instead.
648Unfortunately, the
649.Fn snprintf
650interfaces are not available on older
651systems and the
652.Fn asprintf
653interfaces are not yet portable.
654