xref: /csrg-svn/lib/libc/stdio/printf.3 (revision 61180)
1*61180Sbostic.\" Copyright (c) 1990, 1991, 1993
2*61180Sbostic.\"	The Regents of the University of California.  All rights reserved.
320358Smckusick.\"
446094Sbostic.\" This code is derived from software contributed to Berkeley by
550296Sbostic.\" Chris Torek and the American National Standards Committee X3,
650296Sbostic.\" on Information Processing Systems.
750296Sbostic.\"
846094Sbostic.\" %sccs.include.redist.man%
946094Sbostic.\"
10*61180Sbostic.\"     @(#)printf.3	8.1 (Berkeley) 06/04/93
1146094Sbostic.\"
1248351Scael.Dd
1348351Scael.Dt PRINTF 3
1448351Scael.Os
1548351Scael.Sh NAME
1648351Scael.Nm printf ,
1748351Scael.Nm fprintf ,
1848351Scael.Nm sprintf ,
1948351Scael.Nm snprintf ,
2048351Scael.Nm vprintf ,
2148351Scael.Nm vfprintf,
2248351Scael.Nm vsprintf ,
2348351Scael.Nm vsnprintf
2448351Scael.Nd formatted output conversion
2548351Scael.Sh SYNOPSIS
2648351Scael.Fd #include <stdio.h>
2748351Scael.Ft int
2848351Scael.Fn printf "const char *format" ...
2948351Scael.Ft int
3048351Scael.Fn fprintf "FILE *stream" "const char *format" ...
3148351Scael.Ft int
3248351Scael.Fn sprintf "char *str" "const char *format" ...
3348351Scael.Ft int
3448351Scael.Fn snprintf "char *str" "size_t size" "const char *format" ...
3548351Scael.\" .Ft int
3648351Scael.\" .Fn smprintf "const char *format" ...
3748723Sdonn.Fd #include <stdarg.h>
3848351Scael.Ft int
3948351Scael.Fn vprintf "const char *format" "va_list ap"
4048351Scael.Ft int
4148351Scael.Fn vfprintf "FILE *stream" "const char *format" "va_list ap"
4248351Scael.Ft int
4348351Scael.Fn vsprintf "char *str" "char *format" "va_list ap"
4448351Scael.Ft int
4548351Scael.Fn vsnprintf "char *str" "size_t size" "const char *format" "va_list ap"
4648351Scael.\" .Ft int
4748351Scael.\" .Fn vsmprintf "const char *format" "va_list ap"
4848351Scael.Sh DESCRIPTION
4946094SbosticThe
5048351Scael.Fn printf
5146094Sbosticfamily of functions produces output according to a
5248351Scael.Fa format
5346094Sbosticas described below.
5448351Scael.Fn Printf
5537423Sbosticand
5648351Scael.Fn vprintf
5748351Scaelwrite output to
5848351Scael.Em stdout,
5948351Scaelthe standard output stream;
6048351Scael.Fn fprintf
6137423Sbosticand
6248351Scael.Fn vfprintf
6348351Scaelwrite output to the given output
6448351Scael.Fa stream ;
6548351Scael.Fn sprintf ,
6648351Scael.Fn snprintf ,
6748351Scael.Fn vsprintf ,
6837423Sbosticand
6948351Scael.Fn vsnprintf
7046094Sbosticwrite to the character string
7148351Scael.Fa str .
7246094Sbostic.\" .IR str ;
7346094Sbostic.\" and
7446094Sbostic.\" .I smprintf
7546094Sbostic.\" and
7646094Sbostic.\" .I vsmprintf
7746094Sbostic.\" dynamically allocate a new string with
7846094Sbostic.\" .IR malloc .
7948351ScaelThese functions write the output under the control of a
8048351Scael.Fa format
8148351Scaelstring that specifies how subsequent arguments
8248351Scael(or arguments accessed via the variable-length argument facilities of
8348723Sdonn.Xr stdarg 3 )
8448351Scaelare converted for output.
8546094Sbostic.\" Except for
8646094Sbostic.\" .I smprintf
8746094Sbostic.\" and
8846094Sbostic.\" .IR vsmprintf ,
8946094Sbostic.\" all of these functions return
9048351ScaelThese functions return
9146094Sbosticthe number of characters printed
9248351Scael(not including the trailing
9348351Scael.Ql \e0
9448351Scaelused to end output to strings).
9546094Sbostic.\" .I Smprintf
9646094Sbostic.\" and
9746094Sbostic.\" .I vsmprintf
9846094Sbostic.\" return a pointer to a string of an appropriate length;
9946094Sbostic.\" this pointer should be passed to
10046094Sbostic.\" .I free
10146094Sbostic.\" to release the associated storage
10246094Sbostic.\" when it is no longer needed.
10346094Sbostic.\" If sufficient space is not avaliable,
10446094Sbostic.\" .I smprintf
10546094Sbostic.\" and
10646094Sbostic.\" .I vsmprintf
10746094Sbostic.\" will return
10846094Sbostic.\" .SM
10948351Scael.\" .BR
11048351Scael.Fn Snprintf
11137423Sbosticand
11248351Scael.Fn vsnprintf
11346094Sbosticwill write at most
11448351Scael.Fa size Ns \-1
11546094Sbosticof the characters printed into the output string
11646094Sbostic(the
11748351Scael.Fa size Ns 'th
11848351Scaelcharacter then gets the terminating
11948351Scael.Ql \e0 ) ;
12046094Sbosticif the return value is greater than or equal to the
12148351Scael.Fa size
12246094Sbosticargument, the string was too short
12346094Sbosticand some of the printed characters were discarded.
12448351Scael.Fn Sprintf
12534643Sbosticand
12648351Scael.Fn vsprintf
12755608Sbosticeffectively assume an infinite
12848351Scael.Fa size .
12948351Scael.Pp
13048351ScaelThe format string is composed of zero or more directives:
13148351Scaelordinary
13248351Scael.\" multibyte
13348351Scaelcharacters (not
13448351Scael.Cm % ) ,
13548351Scaelwhich are copied unchanged to the output stream;
13648351Scaeland conversion specifications, each of which results
13748351Scaelin fetching zero or more subsequent arguments.
13848351ScaelEach conversion specification is introduced by
13948351Scaelthe character
14048351Scael.Cm % .
14148351ScaelThe arguments must correspond properly (after type promotion)
14246094Sbosticwith the conversion specifier.
14348351ScaelAfter the
14448351Scael.Cm % ,
14548351Scaelthe following appear in sequence:
14648351Scael.Bl -bullet
14748351Scael.It
14834643SbosticZero or more of the following flags:
14953810Sbostic.Bl -hyphen
15048351Scael.It
15153810SbosticA
15248351Scael.Cm #
15348351Scaelcharacter
15420359Smckusickspecifying that the value should be converted to an ``alternate form''.
15520359SmckusickFor
15648351Scael.Cm c ,
15748351Scael.Cm d ,
15848351Scael.Cm i ,
15948351Scael.Cm n ,
16048351Scael.Cm p ,
16148351Scael.Cm s ,
16220359Smckusickand
16348351Scael.Cm u ,
16437423Sbosticconversions, this option has no effect.
16537423SbosticFor
16648351Scael.Cm o
16720359Smckusickconversions, the precision of the number is increased to force the first
16837423Sbosticcharacter of the output string to a zero (except if a zero value is printed
16937423Sbosticwith an explicit precision of zero).
17037423SbosticFor
17148351Scael.Cm x
17237423Sbosticand
17348351Scael.Cm X
17437423Sbosticconversions, a non-zero result has the string
17548351Scael.Ql 0x
17637423Sbostic(or
17748351Scael.Ql 0X
17837423Sbosticfor
17948351Scael.Cm X
18037423Sbosticconversions) prepended to it.
18137423SbosticFor
18248351Scael.Cm e ,
18348351Scael.Cm E ,
18448351Scael.Cm f ,
18548351Scael.Cm g ,
18620359Smckusickand
18748351Scael.Cm G ,
18820359Smckusickconversions, the result will always contain a decimal point, even if no
18937423Sbosticdigits follow it (normally, a decimal point appears in the results of
19037423Sbosticthose conversions only if a digit follows).
19137423SbosticFor
19248351Scael.Cm g
19320359Smckusickand
19448351Scael.Cm G
19520359Smckusickconversions, trailing zeros are not removed from the result as they
19637423Sbosticwould otherwise be.
19748351Scael.It
19848351ScaelA zero
19948351Scael.Sq Cm \&0
20048351Scaelcharacter specifying zero padding.
20137423SbosticFor all conversions except
20248351Scael.Cm n ,
20337423Sbosticthe converted value is padded on the left with zeros rather than blanks.
20446094SbosticIf a precision is given with a numeric conversion
20548351Scael.Pf ( Mc d ,
20648351Scael.Cm i ,
20748351Scael.Cm o ,
20848351Scael.Cm u ,
20948351Scael.Cm i ,
21048351Scael.Cm x ,
21137423Sbosticand
21248351Scael.Cm X ) ,
21348351Scaelthe
21448351Scael.Sq Cm \&0
21548351Scaelflag is ignored.
21648351Scael.It
21748351ScaelA negative field width flag
21848351Scael.Sq Cm \-
21948351Scaelindicates the converted value is to be left adjusted on the field boundary.
22037423SbosticExcept for
22148351Scael.Cm n
22237423Sbosticconversions, the converted value is padded on the right with blanks,
22337423Sbosticrather than on the left with blanks or zeros.
22448351ScaelA
22548351Scael.Sq Cm \-
22648351Scaeloverrides a
22748351Scael.Sq Cm \&0
22848351Scaelif both are given.
22948351Scael.It
23037423SbosticA space, specifying that a blank should be left before a positive number
23146094Sbosticproduced by a signed conversion
23248351Scael.Pf ( Cm d ,
23348351Scael.Cm e ,
23448351Scael.Cm E ,
23548351Scael.Cm f ,
23648351Scael.Cm g ,
23748351Scael.Cm G ,
23837423Sbosticor
23948351Scael.Cm i ) .
24048351Scael.It
24153810SbosticA
24248351Scael.Sq Cm +
24348351Scaelcharacter specifying that a sign always be placed before a
24437423Sbosticnumber produced by a signed conversion.
24548351ScaelA
24648351Scael.Sq Cm +
24748351Scaeloverrides a space if both are used.
24848351Scael.El
24948351Scael.It
25048351ScaelAn optional decimal digit string specifying a minimum field width.
25137423SbosticIf the converted value has fewer characters than the field width, it will
25248351Scaelbe padded with spaces on the left (or right, if the left-adjustment
25348351Scaelflag has been given) to fill out
25448351Scaelthe field width.
25548351Scael.It
25648351ScaelAn optional precision, in the form of a period
25748351Scael.Sq Cm \&.
25848351Scaelfollowed by an
25937423Sbosticoptional digit string.  If the digit string is omitted, the precision
26037423Sbosticis taken as zero.  This gives the minimum number of digits to appear for
26148351Scael.Cm d ,
26248351Scael.Cm i ,
26348351Scael.Cm o ,
26448351Scael.Cm u ,
26548351Scael.Cm x ,
26637423Sbosticand
26748351Scael.Cm X
26848351Scaelconversions, the number of digits to appear after the decimal-point for
26948351Scael.Cm e ,
27048351Scael.Cm E ,
27137423Sbosticand
27248351Scael.Cm f
27337423Sbosticconversions, the maximum number of significant digits for
27448351Scael.Cm g
27537423Sbosticand
27648351Scael.Cm G
27737423Sbosticconversions, or the maximum number of characters to be printed from a
27837423Sbosticstring for
27948351Scael.Cm s
28037423Sbosticconversions.
28148351Scael.It
28248351ScaelThe optional character
28348351Scael.Cm h ,
28420358Smckusickspecifying that a following
28548351Scael.Cm d ,
28648351Scael.Cm i ,
28748351Scael.Cm o ,
28848351Scael.Cm u ,
28948351Scael.Cm x ,
29020358Smckusickor
29148351Scael.Cm X
29237423Sbosticconversion corresponds to a
29348351Scael.Em short int
29437423Sbosticor
29548351Scael.Em unsigned short int
29637423Sbosticargument, or that a following
29748351Scael.Cm n
29837423Sbosticconversion corresponds to a pointer to a
29948351Scael.Em short int
30037423Sbosticargument.
30148351Scael.It
30248351ScaelThe optional character
30348351Scael.Cm l
30437423Sbostic(ell) specifying that a following
30548351Scael.Cm d ,
30648351Scael.Cm i ,
30748351Scael.Cm o ,
30848351Scael.Cm u ,
30948351Scael.Cm x ,
31034643Sbosticor
31148351Scael.Cm X
31248351Scaelconversion applies to a pointer to a
31348351Scael.Em long int
31437423Sbosticor
31548351Scael.Em unsigned long int
31637423Sbosticargument, or that a following
31748351Scael.Cm n
31837423Sbosticconversion corresponds to a pointer to a
31948351Scael.Em long int
32037423Sbosticargument.
32148351Scael.It
32253810SbosticThe optional character
32353810Sbostic.Cm q ,
32453810Sbosticspecifying that a following
32553810Sbostic.Cm d ,
32653810Sbostic.Cm i ,
32753810Sbostic.Cm o ,
32853810Sbostic.Cm u ,
32953810Sbostic.Cm x ,
33053810Sbosticor
33153810Sbostic.Cm X
33253810Sbosticconversion corresponds to a
33353810Sbostic.Em quad int
33453810Sbosticor
33553810Sbostic.Em unsigned quad int
33653810Sbosticargument, or that a following
33753810Sbostic.Cm n
33853810Sbosticconversion corresponds to a pointer to a
33953810Sbostic.Em quad int
34053810Sbosticargument.
34153810Sbostic.It
34237423SbosticThe character
34348351Scael.Cm L
34437423Sbosticspecifying that a following
34548351Scael.Cm e ,
34648351Scael.Cm E ,
34748351Scael.Cm f ,
34848351Scael.Cm g ,
34937423Sbosticor
35048351Scael.Cm G
35137423Sbosticconversion corresponds to a
35248351Scael.Em long double
35337423Sbosticargument (but note that long double values are not currently supported
35448351Scaelby the
35548351Scael.Tn VAX
35648351Scaeland
35748351Scael.Tn Tahoe
35848351Scaelcompilers).
35948351Scael.It
36048351ScaelA character that specifies the type of conversion to be applied.
36148351Scael.El
36248351Scael.Pp
36348351ScaelA field width or precision, or both, may be indicated by
36448351Scaelan asterisk
36548351Scael.Ql *
36648351Scaelinstead of a
36737423Sbosticdigit string.
36848351ScaelIn this case, an
36948351Scael.Em int
37048351Scaelargument supplies the field width or precision.
37137423SbosticA negative field width is treated as a left adjustment flag followed by a
37237423Sbosticpositive field width; a negative precision is treated as though it were
37337423Sbosticmissing.
37448351Scael.Pp
37548351ScaelThe conversion specifiers and their meanings are:
37648351Scael.Bl -tag -width "diouxX"
37748351Scael.It Cm diouxX
37837423SbosticThe
37948351Scael.Em int
38037423Sbostic(or appropriate variant) argument is converted to signed decimal
38148351Scael.Pf ( Cm d
38248351Scaeland
38348351Scael.Cm i ) ,
38437423Sbosticunsigned octal
38548351Scael.Pq Cm o ,
38637423Sbosticunsigned decimal
38748351Scael.Pq Cm u ,
38837423Sbosticor unsigned hexadecimal
38948351Scael.Pf ( Cm x
39048351Scaeland
39148351Scael.Cm X )
39248351Scaelnotation.  The letters
39348351Scael.Cm abcdef
39437423Sbosticare used for
39548351Scael.Cm x
39637423Sbosticconversions; the letters
39748351Scael.Cm ABCDEF
39837423Sbosticare used for
39948351Scael.m X
40037423Sbosticconversions.
40137423SbosticThe precision, if any, gives the minimum number of digits that must
40237423Sbosticappear; if the converted value requires fewer digits, it is padded on
40337423Sbosticthe left with zeros.
40448351Scael.It Cm DOU
40537423SbosticThe
40648351Scael.Em long int
40737423Sbosticargument is converted to signed decimal, unsigned octal, or unsigned
40837423Sbosticdecimal, as if the format had been
40948351Scael.Cm ld ,
41048351Scael.Cm lo ,
41137423Sbosticor
41248351Scael.Cm lu
41337423Sbosticrespectively.
41437423SbosticThese conversion characters are deprecated, and will eventually disappear.
41548351Scael.It Cm eE
41637423SbosticThe
41748351Scael.Em double
41837423Sbosticargument is rounded and converted in the style
41948351Scael.Sm off
42048351Scael.Pf [\-]d Cm \&. No ddd Cm e No \\*(Pmdd
42148351Scael.Sm on
42248351Scaelwhere there is one digit before the
42348351Scaeldecimal-point character
42448351Scaeland the number of digits after it is equal to the precision;
42548351Scaelif the precision is missing,
42648351Scaelit is taken as 6; if the precision is
42748351Scaelzero, no decimal-point character appears.
42837423SbosticAn
42948351Scael.Cm E
43037423Sbosticconversion uses the letter
43148351Scael.Cm E
43237423Sbostic(rather than
43348351Scael.Cm e )
43437423Sbosticto introduce the exponent.
43537423SbosticThe exponent always contains at least two digits; if the value is zero,
43637423Sbosticthe exponent is 00.
43748351Scael.It Cm f
43837423SbosticThe
43948351Scael.Em double
44037423Sbosticargument is rounded and converted to decimal notation in the style
44148351Scael.Sm off
44248351Scael.Pf [-]ddd Cm \&. No ddd ,
44348351Scael.Sm on
44448351Scaelwhere the number of digits after the decimal-point character
44548351Scaelis equal to the precision specification.
44648351ScaelIf the precision is missing, it is taken as 6; if the precision is
44748351Scaelexplicitly zero, no decimal-point character appears.
44837423SbosticIf a decimal point appears, at least one digit appears before it.
44948351Scael.It Cm g
45037423SbosticThe
45148351Scael.Em double
45248351Scaelargument is converted in style
45348351Scael.Cm f
45437423Sbosticor
45548351Scael.Cm e
45637423Sbostic(or
45748351Scael.Cm E
45837423Sbosticfor
45948351Scael.Cm G
46037423Sbosticconversions).
46137423SbosticThe precision specifies the number of significant digits.
46237423SbosticIf the precision is missing, 6 digits are given; if the precision is zero,
46337423Sbosticit is treated as 1.
46437423SbosticStyle
46548351Scael.Cm e
46637423Sbosticis used if the exponent from its conversion is less than -4 or greater than
46737423Sbosticor equal to the precision.
46837423SbosticTrailing zeros are removed from the fractional part of the result; a
46937423Sbosticdecimal point appears only if it is followed by at least one digit.
47048351Scael.It Cm c
47137423SbosticThe
47248351Scael.Em int
47337423Sbosticargument is converted to an
47448351Scael.Em unsigned char ,
47548351Scaeland the resulting character is written.
47648351Scael.It Cm s
47737423SbosticThe
47848351Scael.Dq Em char *
47948351Scaelargument is expected to be a pointer to an array of character type (pointer
48048351Scaelto a string).
48148351ScaelCharacters from the array are written up to (but not including)
48248351Scaela terminating
48348351Scael.Dv NUL
48448351Scaelcharacter;
48548351Scaelif a precision is specified, no more than the number specified are
48648351Scaelwritten.
48748351ScaelIf a precision is given, no null character
48848351Scaelneed be present; if the precision is not specified, or is greater than
48948351Scaelthe size of the array, the array must contain a terminating
49048351Scael.Dv NUL
49148351Scaelcharacter.
49248351Scael.It Cm p
49337423SbosticThe
49448351Scael.Dq Em void *
49548351Scaelpointer argument is printed in hexadecimal (as if by
49648351Scael.Ql %#x
49748351Scaelor
49848351Scael.Ql %#lx ) .
49948351Scael.It Cm n
50037423SbosticThe number of characters written so far is stored into the
50137423Sbosticinteger indicated by the
50248351Scael.Dq Em int *
50337423Sbostic(or variant) pointer argument.
50437423SbosticNo argument is converted.
50548351Scael.It Cm %
50648351ScaelA
50748351Scael.Ql %
50848351Scaelis written. No argument is converted. The complete conversion specification
50948351Scaelis
51048351Scael.Ql %% .
51148351Scael.El
51248351Scael.Pp
51337423SbosticIn no case does a non-existent or small field width cause truncation of
51437423Sbostica field; if the result of a conversion is wider than the field width, the
51548351Scaelfield is expanded to contain the conversion result.
51648351Scael.Pp
51748351Scael.Sh EXAMPLES
51820358Smckusick.br
51920358SmckusickTo print a date and time in the form `Sunday, July 3, 10:02',
52020358Smckusickwhere
52148351Scael.Em weekday
52220358Smckusickand
52348351Scael.Em month
52448351Scaelare pointers to strings:
52548351Scael.Bd -literal -offset indent
52648351Scael#include <stdio.h>
52748723Sdonnfprintf(stdout, "%s, %s %d, %.2d:%.2d\en",
52848351Scael	weekday, month, day, hour, min);
52948351Scael.Ed
53048351Scael.Pp
53148351ScaelTo print \*(Pi
53248351Scaelto five decimal places:
53348351Scael.Bd -literal -offset indent
53448351Scael#include <math.h>
53548351Scael#include <stdio.h>
53648723Sdonnfprintf(stdout, "pi = %.5f\en", 4 * atan(1.0));
53748351Scael.Ed
53848351Scael.Pp
53937423SbosticTo allocate a 128 byte string and print into it:
54048351Scael.Bd -literal -offset indent
54137423Sbostic#include <stdio.h>
54248723Sdonn#include <stdlib.h>
54348723Sdonn#include <stdarg.h>
54448723Sdonnchar *newfmt(const char *fmt, ...)
54537423Sbostic{
54648723Sdonn		char *p;
54748351Scael		va_list ap;
54848351Scael		if ((p = malloc(128)) == NULL)
54948351Scael			return (NULL);
55048723Sdonn		va_start(ap, fmt);
55148723Sdonn		(void) vsnprintf(p, 128, fmt, ap);
55248351Scael		va_end(ap);
55348351Scael		return (p);
55437423Sbostic}
55548351Scael.Ed
55648351Scael.Sh SEE ALSO
55748351Scael.Xr printf 1 ,
55848351Scael.Xr scanf 3
55948351Scael.Sh STANDARDS
56048723SdonnThe
56148351Scael.Fn fprintf ,
56248351Scael.Fn printf ,
56348351Scael.Fn sprintf ,
56448351Scael.Fn vprintf ,
56548351Scael.Fn vfprintf ,
56648351Scaeland
56748723Sdonn.Fn vsprintf
56848351Scaelfunctions
56948351Scaelconform to
57048351Scael.St -ansiC .
57148723Sdonn.Sh HISTORY
57248723SdonnThe functions
57348723Sdonn.Fn snprintf
57448723Sdonnand
57548723Sdonn.Fn vsnprintf
57648723Sdonnare new to this release.
57748351Scael.Sh BUGS
57848351ScaelThe conversion formats
57950709Scael.Cm \&%D ,
58050709Scael.Cm \&%O ,
58148351Scaeland
58248351Scael.Cm %U
58348351Scaelare not standard and
58437423Sbosticare provided only for backward compatibility.
58548351ScaelThe effect of padding the
58648351Scael.Cm %p
58748351Scaelformat with zeros (either by the
58848351Scael.Sq Cm 0
58937423Sbosticflag or by specifying a precision), and the benign effect (i.e., none)
59048351Scaelof the
59148351Scael.Sq Cm #
59248351Scaelflag on
59348351Scael.Cm %n
59448351Scaeland
59548351Scael.Cm %p
59648351Scaelconversions, as well as other
59748351Scaelnonsensical combinations such as
59848351Scael.Cm %Ld ,
59948351Scaelare not standard; such combinations
60037423Sbosticshould be avoided.
60148351Scael.Pp
60246094SbosticBecause
60348351Scael.Fn sprintf
60446094Sbosticand
60548351Scael.Fn vsprintf
60646094Sbosticassume an infinitely long string,
60746094Sbosticcallers must be careful not to overflow the actual space;
60846094Sbosticthis is often impossible to assure.
60946094SbosticFor safety, programmers should use the
61048351Scael.Fn snprintf
61146094Sbosticinterface instead.
61246094SbosticUnfortunately, this interface is not portable.
613