xref: /dragonfly/lib/libc/stdio/printf.3 (revision b40e316c)
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: src/lib/libc/stdio/printf.3,v 1.17.2.11 2003/03/02 07:29:33 tjr Exp $
38.\" $DragonFly: src/lib/libc/stdio/printf.3,v 1.3 2005/01/04 00:00:52 cpressey Exp $
39.\"
40.Dd March 2, 2003
41.Dt PRINTF 3
42.Os
43.Sh NAME
44.Nm printf , fprintf , sprintf , snprintf , asprintf ,
45.Nm vprintf , vfprintf, vsprintf , vsnprintf , vasprintf
46.Nd formatted output conversion
47.Sh LIBRARY
48.Lb libc
49.Sh SYNOPSIS
50.In stdio.h
51.Ft int
52.Fn printf "const char *format" ...
53.Ft int
54.Fn fprintf "FILE *stream" "const char *format" ...
55.Ft int
56.Fn sprintf "char *str" "const char *format" ...
57.Ft int
58.Fn snprintf "char *str" "size_t size" "const char *format" ...
59.Ft int
60.Fn asprintf "char **ret" "const char *format" ...
61.In stdarg.h
62.Ft int
63.Fn vprintf "const char *format" "va_list ap"
64.Ft int
65.Fn vfprintf "FILE *stream" "const char *format" "va_list ap"
66.Ft int
67.Fn vsprintf "char *str" "const char *format" "va_list ap"
68.Ft int
69.Fn vsnprintf "char *str" "size_t size" "const char *format" "va_list ap"
70.Ft int
71.Fn vasprintf "char **ret" "const char *format" "va_list ap"
72.Sh DESCRIPTION
73The
74.Fn printf
75family of functions produces output according to a
76.Fa format
77as described below.
78.Fn Printf
79and
80.Fn vprintf
81write output to
82.Pa stdout ,
83the standard output stream;
84.Fn fprintf
85and
86.Fn vfprintf
87write output to the given output
88.Fa stream ;
89.Fn sprintf ,
90.Fn snprintf ,
91.Fn vsprintf ,
92and
93.Fn vsnprintf
94write to the character string
95.Fa str ;
96and
97.Fn asprintf
98and
99.Fn vasprintf
100dynamically allocate a new string with
101.Xr malloc 3 .
102.Pp
103These functions write the output under the control of a
104.Fa format
105string that specifies how subsequent arguments
106(or arguments accessed via the variable-length argument facilities of
107.Xr stdarg 3 )
108are converted for output.
109.Pp
110Upon success, these functions return the number of characters printed
111(not including the trailing
112.Ql \e0
113used to end output to strings),
114or, in the case of
115.Fn snprintf
116and
117.Fn vsnprintf ,
118the number of characters that would have been printed if the
119.Fa size
120were unlimited
121(again, not including the final
122.Ql \e0 ) .
123All of these function return a negative value if an output error occurs.
124.Pp
125.Fn Asprintf
126and
127.Fn vasprintf
128set
129.Fa *ret
130to be a pointer to a buffer sufficiently large to hold the formatted string.
131This pointer should be passed to
132.Xr free 3
133to release the allocated storage when it is no longer needed.
134If sufficient space cannot be allocated,
135.Fn asprintf
136and
137.Fn vasprintf
138will return -1 and set
139.Fa ret
140to be a
141.Dv NULL
142pointer.
143.Pp
144.Fn Snprintf
145and
146.Fn vsnprintf
147will write at most
148.Fa size Ns \-1
149of the characters printed into the output string
150(the
151.Fa size Ns 'th
152character then gets the terminating
153.Ql \e0 ) ;
154if the return value is greater than or equal to the
155.Fa size
156argument, the string was too short
157and some of the printed characters were discarded.
158.Pp
159.Fn Sprintf
160and
161.Fn vsprintf
162effectively assume an infinite
163.Fa size .
164.Pp
165The format string is composed of zero or more directives:
166ordinary
167.\" multibyte
168characters (not
169.Cm % ) ,
170which are copied unchanged to the output stream;
171and conversion specifications, each of which results
172in fetching zero or more subsequent arguments.
173Each conversion specification is introduced by
174the
175.Cm %
176character.
177The arguments must correspond properly (after type promotion)
178with the conversion specifier.
179After the
180.Cm % ,
181the following appear in sequence:
182.Bl -bullet
183.It
184An optional field, consisting of a decimal digit string followed by a
185.Cm $ ,
186specifying the next argument to access.
187If this field is not provided, the argument following the last
188argument accessed will be used.
189Arguments are numbered starting at
190.Cm 1 .
191If unaccessed arguments in the format string are interspersed with ones that
192are accessed the results will be indeterminate.
193.It
194Zero or more of the following flags:
195.Bl -hyphen
196.It
197A
198.Cm #
199character
200specifying that the value should be converted to an
201.Dq alternate form .
202For
203.Cm c , d , i , n , p , s ,
204and
205.Cm u
206conversions, this option has no effect.
207For
208.Cm o
209conversions, the precision of the number is increased to force the first
210character of the output string to a zero (except if a zero value is printed
211with an explicit precision of zero).
212For
213.Cm x
214and
215.Cm X
216conversions, a non-zero result has the string
217.Ql 0x
218(or
219.Ql 0X
220for
221.Cm X
222conversions) prepended to it.
223For
224.Cm e , E , f , g ,
225and
226.Cm G
227conversions, the result will always contain a decimal point, even if no
228digits follow it (normally, a decimal point appears in the results of
229those conversions only if a digit follows).
230For
231.Cm g
232and
233.Cm G
234conversions, trailing zeros are not removed from the result as they
235would otherwise be.
236.It
237A
238.Cm 0
239(zero)
240character specifying zero padding.
241For all conversions except
242.Cm n ,
243the converted value is padded on the left with zeros rather than blanks.
244If a precision is given with a numeric conversion
245.Cm ( d , i , o , u , i , x ,
246and
247.Cm X ) ,
248the
249.Cm 0
250flag is ignored.
251.It
252A negative field width flag
253.Cm \-
254indicates the converted value is to be left adjusted on the field boundary.
255Except for
256.Cm n
257conversions, the converted value is padded on the right with blanks,
258rather than on the left with blanks or zeros.
259A
260.Cm \-
261overrides a
262.Cm 0
263if both are given.
264.It
265A space, specifying that a blank should be left before a positive number
266produced by a signed conversion
267.Cm ( d , e , E , f , g , G ,
268or
269.Cm i ) .
270.It
271A
272.Cm +
273character specifying that a sign always be placed before a
274number produced by a signed conversion.
275A
276.Cm +
277overrides a space if both are used.
278.El
279.It
280An optional decimal digit string specifying a minimum field width.
281If the converted value has fewer characters than the field width, it will
282be padded with spaces on the left (or right, if the left-adjustment
283flag has been given) to fill out
284the field width.
285.It
286An optional precision, in the form of a period
287.Cm \&.
288followed by an
289optional digit string.
290If the digit string is omitted, the precision is taken as zero.
291This gives the minimum number of digits to appear for
292.Cm d , i , o , u , x ,
293and
294.Cm X
295conversions, the number of digits to appear after the decimal-point for
296.Cm e , E ,
297and
298.Cm f
299conversions, the maximum number of significant digits for
300.Cm g
301and
302.Cm G
303conversions, or the maximum number of characters to be printed from a
304string for
305.Cm s
306conversions.
307.It
308The optional character
309.Cm h ,
310specifying that a following
311.Cm d , i , o , u , x ,
312or
313.Cm X
314conversion corresponds to a
315.Vt short int
316or
317.Vt unsigned short int
318argument, or that a following
319.Cm n
320conversion corresponds to a pointer to a
321.Vt short int
322argument.
323.It
324The optional character
325.Cm l
326(ell) specifying that a following
327.Cm d , i , o , u , x ,
328or
329.Cm X
330conversion applies to a pointer to a
331.Vt long int
332or
333.Vt unsigned long int
334argument, or that a following
335.Cm n
336conversion corresponds to a pointer to a
337.Vt long int
338argument.
339.It
340The optional characters
341.Cm ll
342(ell ell) specifying that a following
343.Cm d , i , o , u , x ,
344or
345.Cm X
346conversion applies to a pointer to a
347.Vt long long int
348or
349.Vt unsigned long long int
350argument, or that a following
351.Cm n
352conversion corresponds to a pointer to a
353.Vt long long int
354argument.
355.It
356The optional character
357.Cm q ,
358specifying that a following
359.Cm d , i , o , u , x ,
360or
361.Cm X
362conversion corresponds to a
363.Vt quad int
364or
365.Vt unsigned quad int
366argument, or that a following
367.Cm n
368conversion corresponds to a pointer to a
369.Vt quad int
370argument.
371.It
372The character
373.Cm L
374specifying that a following
375.Cm e , E , f , g ,
376or
377.Cm G
378conversion corresponds to a
379.Vt long double
380argument.
381.It
382A character that specifies the type of conversion to be applied.
383.El
384.Pp
385A field width or precision, or both, may be indicated by
386an asterisk
387.Ql *
388or an asterisk followed by one or more decimal digits and a
389.Ql $
390instead of a
391digit string.
392In this case, an
393.Vt int
394argument supplies the field width or precision.
395A negative field width is treated as a left adjustment flag followed by a
396positive field width; a negative precision is treated as though it were
397missing.
398If a single format directive mixes positional (nn$)
399and non-positional arguments, the results are undefined.
400.Pp
401The conversion specifiers and their meanings are:
402.Bl -tag -width "diouxX"
403.It Cm diouxX
404The
405.Vt int
406(or appropriate variant) argument is converted to signed decimal
407.Cm ( d
408and
409.Cm i ) ,
410unsigned octal
411.Pq Cm o ,
412unsigned decimal
413.Pq Cm u ,
414or unsigned hexadecimal
415.Cm ( x
416and
417.Cm X )
418notation.
419The letters
420.Cm abcdef
421are used for
422.Cm x
423conversions; the letters
424.Cm ABCDEF
425are used for
426.Cm X
427conversions.
428The precision, if any, gives the minimum number of digits that must
429appear; if the converted value requires fewer digits, it is padded on
430the left with zeros.
431.It Cm DOU
432The
433.Vt long int
434argument is converted to signed decimal, unsigned octal, or unsigned
435decimal, as if the format had been
436.Cm ld , lo ,
437or
438.Cm lu
439respectively.
440These conversion characters are deprecated, and will eventually disappear.
441.It Cm eE
442The
443.Vt double
444argument is rounded and converted in the style
445.Oo \- Oc Ns d Ns Cm \&. Ns ddd Ns Cm e Ns \\*[Pm]dd
446where there is one digit before the
447decimal-point character
448and the number of digits after it is equal to the precision;
449if the precision is missing,
450it is taken as 6; if the precision is
451zero, no decimal-point character appears.
452An
453.Cm E
454conversion uses the letter
455.Cm E
456(rather than
457.Cm e )
458to introduce the exponent.
459The exponent always contains at least two digits; if the value is zero,
460the exponent is 00.
461.It Cm f
462The
463.Vt double
464argument is rounded and converted to decimal notation in the style
465.Oo \- Oc Ns ddd Ns Cm \&. Ns ddd ,
466where the number of digits after the decimal-point character
467is equal to the precision specification.
468If the precision is missing, it is taken as 6; if the precision is
469explicitly zero, no decimal-point character appears.
470If a decimal point appears, at least one digit appears before it.
471.It Cm gG
472The
473.Vt double
474argument is converted in style
475.Cm f
476or
477.Cm e
478(or
479.Cm E
480for
481.Cm G
482conversions).
483The precision specifies the number of significant digits.
484If the precision is missing, 6 digits are given; if the precision is zero,
485it is treated as 1.
486Style
487.Cm e
488is used if the exponent from its conversion is less than -4 or greater than
489or equal to the precision.
490Trailing zeros are removed from the fractional part of the result; a
491decimal point appears only if it is followed by at least one digit.
492.It Cm c
493The
494.Vt int
495argument is converted to an
496.Vt unsigned char ,
497and the resulting character is written.
498.It Cm s
499The
500.Vt char *
501argument is expected to be a pointer to an array of character type (pointer
502to a string).
503Characters from the array are written up to (but not including)
504a terminating
505.Dv NUL
506character;
507if a precision is specified, no more than the number specified are
508written.
509If a precision is given, no null character
510need be present; if the precision is not specified, or is greater than
511the size of the array, the array must contain a terminating
512.Dv NUL
513character.
514.It Cm p
515The
516.Vt void *
517pointer argument is printed in hexadecimal (as if by
518.Ql %#x
519or
520.Ql %#lx ) .
521.It Cm n
522The number of characters written so far is stored into the
523integer indicated by the
524.Vt int *
525(or variant) pointer argument.
526No argument is converted.
527.It Cm %
528A
529.Ql %
530is written.
531No argument is converted.
532The complete conversion specification
533is
534.Ql %% .
535.El
536.Pp
537In no case does a non-existent or small field width cause truncation of
538a field; if the result of a conversion is wider than the field width, the
539field is expanded to contain the conversion result.
540.Sh EXAMPLES
541To print a date and time in the form
542.Dq Li "Sunday, July 3, 10:02" ,
543where
544.Fa weekday
545and
546.Fa month
547are pointers to strings:
548.Bd -literal -offset indent
549#include <stdio.h>
550fprintf(stdout, "%s, %s %d, %.2d:%.2d\en",
551	weekday, month, day, hour, min);
552.Ed
553.Pp
554To print \*(Pi
555to five decimal places:
556.Bd -literal -offset indent
557#include <math.h>
558#include <stdio.h>
559fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0));
560.Ed
561.Pp
562To allocate a 128 byte string and print into it:
563.Bd -literal -offset indent
564#include <stdio.h>
565#include <stdlib.h>
566#include <stdarg.h>
567char *newfmt(const char *fmt, ...)
568{
569		char *p;
570		va_list ap;
571		if ((p = malloc(128)) == NULL)
572			return (NULL);
573		va_start(ap, fmt);
574		(void) vsnprintf(p, 128, fmt, ap);
575		va_end(ap);
576		return (p);
577}
578.Ed
579.Sh ERRORS
580In addition to the errors documented for the
581.Xr write 2
582system call, the
583.Fn printf
584family of functions may fail if:
585.Bl -tag -width Er
586.It Bq Er ENOMEM
587Insufficient storage space is available.
588.El
589.Sh SEE ALSO
590.Xr printf 1 ,
591.Xr scanf 3
592.Sh STANDARDS
593The
594.Fn fprintf ,
595.Fn printf ,
596.Fn sprintf ,
597.Fn vprintf ,
598.Fn vfprintf ,
599and
600.Fn vsprintf
601functions
602conform to
603.St -isoC .
604.Sh HISTORY
605The functions
606.Fn asprintf
607and
608.Fn vasprintf
609first appeared in the
610.Tn GNU C
611library.
612These were implemented by
613.An Peter Wemm Aq peter@FreeBSD.org
614in
615.Fx 2.2 ,
616but were later replaced with a different implementation
617from
618.An Todd C. Miller Aq Todd.Miller@courtesan.com
619for
620.Ox 2.3 .
621.Sh BUGS
622The conversion formats
623.Cm \&%D , \&%O ,
624and
625.Cm %U
626are not standard and
627are provided only for backward compatibility.
628The effect of padding the
629.Cm %p
630format with zeros (either by the
631.Cm 0
632flag or by specifying a precision), and the benign effect (i.e., none)
633of the
634.Cm #
635flag on
636.Cm %n
637and
638.Cm %p
639conversions, as well as other
640nonsensical combinations such as
641.Cm %Ld ,
642are not standard; such combinations
643should be avoided.
644.Pp
645Because
646.Fn sprintf
647and
648.Fn vsprintf
649assume an infinitely long string,
650callers must be careful not to overflow the actual space;
651this is often hard to assure.
652For safety, programmers should use the
653.Fn snprintf
654interface instead.
655Unfortunately, this interface is not portable.
656