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