add ANSI contribution notice
[unix-history] / usr / src / lib / libc / stdio / printf.3
CommitLineData
ae59e04c 1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
411867e7 2.\" All rights reserved.
c3ce85c3 3.\"
411867e7 4.\" This code is derived from software contributed to Berkeley by
043368e6
KB
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
411867e7
KB
8.\" %sccs.include.redist.man%
9.\"
043368e6 10.\" @(#)printf.3 6.13 (Berkeley) %G%
411867e7 11.\"
ae59e04c
CL
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" ...
108b3140 37.Fd #include <stdarg.h>
ae59e04c
CL
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
411867e7 49The
ae59e04c 50.Fn printf
411867e7 51family of functions produces output according to a
ae59e04c 52.Fa format
411867e7 53as described below.
ae59e04c 54.Fn Printf
df27e01d 55and
ae59e04c
CL
56.Fn vprintf
57write output to
58.Em stdout,
59the standard output stream;
60.Fn fprintf
df27e01d 61and
ae59e04c
CL
62.Fn vfprintf
63write output to the given output
64.Fa stream ;
65.Fn sprintf ,
66.Fn snprintf ,
67.Fn vsprintf ,
df27e01d 68and
ae59e04c 69.Fn vsnprintf
411867e7 70write to the character string
ae59e04c 71.Fa str .
411867e7
KB
72.\" .IR str ;
73.\" and
74.\" .I smprintf
75.\" and
76.\" .I vsmprintf
77.\" dynamically allocate a new string with
78.\" .IR malloc .
ae59e04c
CL
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
108b3140 83.Xr stdarg 3 )
ae59e04c 84are converted for output.
411867e7
KB
85.\" Except for
86.\" .I smprintf
87.\" and
88.\" .IR vsmprintf ,
89.\" all of these functions return
ae59e04c 90These functions return
411867e7 91the number of characters printed
ae59e04c
CL
92(not including the trailing
93.Ql \e0
94used to end output to strings).
411867e7
KB
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
ae59e04c
CL
109.\" .BR
110.Fn Snprintf
411867e7 111and
ae59e04c 112.Fn vsnprintf
411867e7 113will write at most
ae59e04c 114.Fa size Ns \-1
411867e7
KB
115of the characters printed into the output string
116(the
ae59e04c
CL
117.Fa size Ns 'th
118character then gets the terminating
119.Ql \e0 ) ;
411867e7 120if the return value is greater than or equal to the
ae59e04c 121.Fa size
411867e7
KB
122argument, the string was too short
123and some of the printed characters were discarded.
ae59e04c 124.Fn Sprintf
df27e01d 125and
ae59e04c 126.Fn vsprintf
411867e7 127effectively assume an infinte
ae59e04c
CL
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)
411867e7 142with the conversion specifier.
ae59e04c
CL
143After the
144.Cm % ,
145the following appear in sequence:
146.Bl -bullet
147.It
a2f1db91 148Zero or more of the following flags:
ae59e04c
CL
149.Bl -hyphen -offset indent
150.It
151a
152.Cm #
153character
ef8094b4
KM
154specifying that the value should be converted to an ``alternate form''.
155For
ae59e04c
CL
156.Cm c ,
157.Cm d ,
158.Cm i ,
159.Cm n ,
160.Cm p ,
161.Cm s ,
ef8094b4 162and
ae59e04c 163.Cm u ,
df27e01d
KB
164conversions, this option has no effect.
165For
ae59e04c 166.Cm o
ef8094b4 167conversions, the precision of the number is increased to force the first
df27e01d
KB
168character of the output string to a zero (except if a zero value is printed
169with an explicit precision of zero).
170For
ae59e04c 171.Cm x
df27e01d 172and
ae59e04c 173.Cm X
df27e01d 174conversions, a non-zero result has the string
ae59e04c 175.Ql 0x
df27e01d 176(or
ae59e04c 177.Ql 0X
df27e01d 178for
ae59e04c 179.Cm X
df27e01d
KB
180conversions) prepended to it.
181For
ae59e04c
CL
182.Cm e ,
183.Cm E ,
184.Cm f ,
185.Cm g ,
ef8094b4 186and
ae59e04c 187.Cm G ,
ef8094b4 188conversions, the result will always contain a decimal point, even if no
df27e01d
KB
189digits follow it (normally, a decimal point appears in the results of
190those conversions only if a digit follows).
191For
ae59e04c 192.Cm g
ef8094b4 193and
ae59e04c 194.Cm G
ef8094b4 195conversions, trailing zeros are not removed from the result as they
df27e01d 196would otherwise be.
ae59e04c
CL
197.It
198A zero
199.Sq Cm \&0
200character specifying zero padding.
df27e01d 201For all conversions except
ae59e04c 202.Cm n ,
df27e01d 203the converted value is padded on the left with zeros rather than blanks.
411867e7 204If a precision is given with a numeric conversion
ae59e04c
CL
205.Pf ( Mc d ,
206.Cm i ,
207.Cm o ,
208.Cm u ,
209.Cm i ,
210.Cm x ,
df27e01d 211and
ae59e04c
CL
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.
df27e01d 220Except for
ae59e04c 221.Cm n
df27e01d
KB
222conversions, the converted value is padded on the right with blanks,
223rather than on the left with blanks or zeros.
ae59e04c
CL
224A
225.Sq Cm \-
226overrides a
227.Sq Cm \&0
228if both are given.
229.It
df27e01d 230A space, specifying that a blank should be left before a positive number
411867e7 231produced by a signed conversion
ae59e04c
CL
232.Pf ( Cm d ,
233.Cm e ,
234.Cm E ,
235.Cm f ,
236.Cm g ,
237.Cm G ,
df27e01d 238or
ae59e04c
CL
239.Cm i ) .
240.It
241a
242.Sq Cm +
243character specifying that a sign always be placed before a
df27e01d 244number produced by a signed conversion.
ae59e04c
CL
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.
df27e01d 251If the converted value has fewer characters than the field width, it will
ae59e04c
CL
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
df27e01d
KB
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
ae59e04c
CL
261.Cm d ,
262.Cm i ,
263.Cm o ,
264.Cm u ,
265.Cm x ,
df27e01d 266and
ae59e04c
CL
267.Cm X
268conversions, the number of digits to appear after the decimal-point for
269.Cm e ,
270.Cm E ,
df27e01d 271and
ae59e04c 272.Cm f
df27e01d 273conversions, the maximum number of significant digits for
ae59e04c 274.Cm g
df27e01d 275and
ae59e04c 276.Cm G
df27e01d
KB
277conversions, or the maximum number of characters to be printed from a
278string for
ae59e04c 279.Cm s
df27e01d 280conversions.
ae59e04c
CL
281.It
282The optional character
283.Cm h ,
c3ce85c3 284specifying that a following
ae59e04c
CL
285.Cm d ,
286.Cm i ,
287.Cm o ,
288.Cm u ,
289.Cm x ,
c3ce85c3 290or
ae59e04c 291.Cm X
df27e01d 292conversion corresponds to a
ae59e04c 293.Em short int
df27e01d 294or
ae59e04c 295.Em unsigned short int
df27e01d 296argument, or that a following
ae59e04c 297.Cm n
df27e01d 298conversion corresponds to a pointer to a
ae59e04c 299.Em short int
df27e01d 300argument.
ae59e04c
CL
301.It
302The optional character
303.Cm l
df27e01d 304(ell) specifying that a following
ae59e04c
CL
305.Cm d ,
306.Cm i ,
307.Cm o ,
308.Cm u ,
309.Cm x ,
a2f1db91 310or
ae59e04c
CL
311.Cm X
312conversion applies to a pointer to a
313.Em long int
df27e01d 314or
ae59e04c 315.Em unsigned long int
df27e01d 316argument, or that a following
ae59e04c 317.Cm n
df27e01d 318conversion corresponds to a pointer to a
ae59e04c 319.Em long int
df27e01d 320argument.
ae59e04c 321.It
df27e01d 322The character
ae59e04c 323.Cm L
df27e01d 324specifying that a following
ae59e04c
CL
325.Cm e ,
326.Cm E ,
327.Cm f ,
328.Cm g ,
df27e01d 329or
ae59e04c 330.Cm G
df27e01d 331conversion corresponds to a
ae59e04c 332.Em long double
df27e01d 333argument (but note that long double values are not currently supported
ae59e04c
CL
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
df27e01d 347digit string.
ae59e04c
CL
348In this case, an
349.Em int
350argument supplies the field width or precision.
df27e01d
KB
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.
ae59e04c
CL
354.Pp
355The conversion specifiers and their meanings are:
356.Bl -tag -width "diouxX"
357.It Cm diouxX
df27e01d 358The
ae59e04c 359.Em int
df27e01d 360(or appropriate variant) argument is converted to signed decimal
ae59e04c
CL
361.Pf ( Cm d
362and
363.Cm i ) ,
df27e01d 364unsigned octal
ae59e04c 365.Pq Cm o ,
df27e01d 366unsigned decimal
ae59e04c 367.Pq Cm u ,
df27e01d 368or unsigned hexadecimal
ae59e04c
CL
369.Pf ( Cm x
370and
371.Cm X )
372notation. The letters
373.Cm abcdef
df27e01d 374are used for
ae59e04c 375.Cm x
df27e01d 376conversions; the letters
ae59e04c 377.Cm ABCDEF
df27e01d 378are used for
ae59e04c 379.m X
df27e01d
KB
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.
ae59e04c 384.It Cm DOU
df27e01d 385The
ae59e04c 386.Em long int
df27e01d
KB
387argument is converted to signed decimal, unsigned octal, or unsigned
388decimal, as if the format had been
ae59e04c
CL
389.Cm ld ,
390.Cm lo ,
df27e01d 391or
ae59e04c 392.Cm lu
df27e01d
KB
393respectively.
394These conversion characters are deprecated, and will eventually disappear.
ae59e04c 395.It Cm eE
df27e01d 396The
ae59e04c 397.Em double
df27e01d 398argument is rounded and converted in the style
ae59e04c
CL
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.
df27e01d 408An
ae59e04c 409.Cm E
df27e01d 410conversion uses the letter
ae59e04c 411.Cm E
df27e01d 412(rather than
ae59e04c 413.Cm e )
df27e01d
KB
414to introduce the exponent.
415The exponent always contains at least two digits; if the value is zero,
416the exponent is 00.
ae59e04c 417.It Cm f
df27e01d 418The
ae59e04c 419.Em double
df27e01d 420argument is rounded and converted to decimal notation in the style
ae59e04c
CL
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.
df27e01d 428If a decimal point appears, at least one digit appears before it.
ae59e04c 429.It Cm g
df27e01d 430The
ae59e04c
CL
431.Em double
432argument is converted in style
433.Cm f
df27e01d 434or
ae59e04c 435.Cm e
df27e01d 436(or
ae59e04c 437.Cm E
df27e01d 438for
ae59e04c 439.Cm G
df27e01d
KB
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
ae59e04c 445.Cm e
df27e01d
KB
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.
ae59e04c 450.It Cm c
df27e01d 451The
ae59e04c 452.Em int
df27e01d 453argument is converted to an
ae59e04c
CL
454.Em unsigned char ,
455and the resulting character is written.
456.It Cm s
df27e01d 457The
ae59e04c
CL
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
df27e01d 473The
ae59e04c
CL
474.Dq Em void *
475pointer argument is printed in hexadecimal (as if by
476.Ql %#x
477or
478.Ql %#lx ) .
479.It Cm n
df27e01d
KB
480The number of characters written so far is stored into the
481integer indicated by the
ae59e04c 482.Dq Em int *
df27e01d
KB
483(or variant) pointer argument.
484No argument is converted.
ae59e04c
CL
485.It Cm %
486A
487.Ql %
488is written. No argument is converted. The complete conversion specification
489is
490.Ql %% .
491.El
492.Pp
df27e01d
KB
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
ae59e04c
CL
495field is expanded to contain the conversion result.
496.Pp
497.Sh EXAMPLES
c3ce85c3
KM
498.br
499To print a date and time in the form `Sunday, July 3, 10:02',
500where
ae59e04c 501.Em weekday
c3ce85c3 502and
ae59e04c
CL
503.Em month
504are pointers to strings:
505.Bd -literal -offset indent
506#include <stdio.h>
108b3140 507fprintf(stdout, "%s, %s %d, %.2d:%.2d\en",
ae59e04c
CL
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>
108b3140 516fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0));
ae59e04c
CL
517.Ed
518.Pp
df27e01d 519To allocate a 128 byte string and print into it:
ae59e04c 520.Bd -literal -offset indent
df27e01d 521#include <stdio.h>
108b3140
DS
522#include <stdlib.h>
523#include <stdarg.h>
524char *newfmt(const char *fmt, ...)
df27e01d 525{
108b3140 526 char *p;
ae59e04c
CL
527 va_list ap;
528 if ((p = malloc(128)) == NULL)
529 return (NULL);
108b3140
DS
530 va_start(ap, fmt);
531 (void) vsnprintf(p, 128, fmt, ap);
ae59e04c
CL
532 va_end(ap);
533 return (p);
df27e01d 534}
ae59e04c
CL
535.Ed
536.Sh SEE ALSO
537.Xr printf 1 ,
538.Xr scanf 3
539.Sh STANDARDS
108b3140 540The
ae59e04c
CL
541.Fn fprintf ,
542.Fn printf ,
543.Fn sprintf ,
ae59e04c
CL
544.Fn vprintf ,
545.Fn vfprintf ,
ae59e04c 546and
108b3140 547.Fn vsprintf
ae59e04c
CL
548functions
549conform to
550.St -ansiC .
108b3140
DS
551.Sh HISTORY
552The functions
553.Fn snprintf
554and
555.Fn vsnprintf
556are new to this release.
ae59e04c
CL
557.Sh BUGS
558The conversion formats
559.Cm %D ,
560.Cm %O ,
561and
562.Cm %U
563are not standard and
df27e01d 564are provided only for backward compatibility.
ae59e04c
CL
565The effect of padding the
566.Cm %p
567format with zeros (either by the
568.Sq Cm 0
df27e01d 569flag or by specifying a precision), and the benign effect (i.e., none)
ae59e04c
CL
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
df27e01d 580should be avoided.
ae59e04c 581.Pp
411867e7 582Because
ae59e04c 583.Fn sprintf
411867e7 584and
ae59e04c 585.Fn vsprintf
411867e7
KB
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
ae59e04c 590.Fn snprintf
411867e7
KB
591interface instead.
592Unfortunately, this interface is not portable.