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