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