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