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