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