add POSIX/IEEE contribution notice
[unix-history] / usr / src / usr.bin / printf / printf.1
CommitLineData
b5dc1377 1.\" Copyright (c) 1989, 1990 The Regents of the University of California.
a4ba6264
KB
2.\" All rights reserved.
3.\"
ae122740
KB
4.\" This code is derived from software contributed to Berkeley by
5.\" the Institute of Electrical and Electronics Engineers, Inc.
6.\"
b5dc1377 7.\" %sccs.include.redist.man%
a4ba6264 8.\"
ae122740 9.\" @(#)printf.1 5.10 (Berkeley) %G%
a4ba6264 10.\"
ee9b0a0a 11.Vx
b5dc1377
CL
12.Dd
13.Dt PRINTF 1
a4ba6264 14.AT 1
b5dc1377
CL
15.Sh NAME
16.Nm printf
17.Nd formatted output
18.Sh SYNOPSIS
19.Pp
20.Nm printf format
21.Op arguments ...
22.Sh DESCRIPTION
23.Nm Printf
a4ba6264
KB
24formats and prints its arguments, after the first, under control
25of the
b5dc1377 26.Ar format .
a4ba6264 27The
b5dc1377 28.Ar format
a4ba6264
KB
29is a character string which contains three types of objects: plain characters,
30which are simply copied to standard output, character escape sequences which
31are converted and copied to the standard output, and format specifications,
32each of which causes printing of the next successive
b5dc1377
CL
33.Ar argument .
34.Pp
a4ba6264 35The
b5dc1377 36.Ar arguments
a4ba6264
KB
37after the first are treated as strings if the corresponding format is
38either
b5dc1377 39.Cm c
a4ba6264 40or
b5dc1377 41.Cm s ;
a4ba6264 42otherwise it is evaluated as a C constant, with the following extensions:
b5dc1377
CL
43.Pp
44.Df I
a4ba6264 45A leading plus or minus sign is allowed.
b5dc1377 46.br
a4ba6264
KB
47If the leading character is a single or double quote, or not a digit,
48plus, or minus sign, the value is the ASCII code of the next character.
b5dc1377
CL
49.De
50.Pp
a4ba6264 51The format string is reused as often as necessary to satisfy the
b5dc1377 52.Ar arguments .
a4ba6264
KB
53Any extra format specifications are evaluated with zero or the null
54string.
b5dc1377 55.Pp
a4ba6264
KB
56Character escape sequences are in backslash notation as defined in the
57draft proposed ANSI C Standard X3J11. The characters and their meanings
58are as follows:
b5dc1377
CL
59.Tw Ds
60.Tp Cm \ea
f78448a3 61Write a <bell> character.
b5dc1377 62.Tp Cm \eb
a4ba6264 63Write a <backspace> character.
b5dc1377 64.Tp Cm \ef
a4ba6264 65Write a <form-feed> character.
b5dc1377 66.Tp Cm \en
a4ba6264 67Write a <new-line> character.
b5dc1377 68.Tp Cm \er
a4ba6264 69Write a <carriage return> character.
b5dc1377 70.Tp Cm \et
a4ba6264 71Write a <tab> character.
b5dc1377 72.Tp Cm \ev
a4ba6264 73Write a <vertical tab> character.
b5dc1377 74.Tp Cm \e\'
f78448a3 75Write a <single quote> character.
b5dc1377 76.Tp Cm \e\e
a4ba6264 77Write a backslash character.
b5dc1377
CL
78.Tp Cx Cm \e
79.Ar num
80.Cx
5325ced3 81Write an 8-bit character whose ASCII value is the 1-, 2-, or 3-digit
a4ba6264 82octal number
b5dc1377
CL
83.Ar num .
84.Tp
85.Pp
a4ba6264
KB
86Each format specification is introduced by the percent character
87(``%'').
88The remainder of the format specification includes, in the
89following order:
b5dc1377 90.Pp
a4ba6264 91Zero or more of the following flags:
b5dc1377
CL
92.Pp
93.Ds I
94.Tw Ds
95.Tp Cm #
96A `#' character
a4ba6264 97specifying that the value should be printed in an ``alternate form''.
b5dc1377
CL
98For
99.Cm c ,
100.Cm d ,
a4ba6264 101and
b5dc1377 102.Cm s ,
a4ba6264 103formats, this option has no effect. For the
b5dc1377 104.Cm o
a4ba6264
KB
105formats the precision of the number is increased to force the first
106character of the output string to a zero. For the
b5dc1377
CL
107.Cm x
108.Pq Cm X
a4ba6264 109format, a non-zero result has the string
b5dc1377
CL
110.Li 0x
111.Pq Li 0X
112prepended to it. For
113.Cm e ,
114.Cm E ,
115.Cm f ,
116.Cm g ,
a4ba6264 117and
b5dc1377 118.Cm G ,
a4ba6264
KB
119formats, the result will always contain a decimal point, even if no
120digits follow the point (normally, a decimal point only appears in the
121results of those formats if a digit follows the decimal point). For
b5dc1377 122.Cm g
a4ba6264 123and
b5dc1377 124.Cm G
a4ba6264
KB
125formats, trailing zeros are not removed from the result as they
126would otherwise be;
b5dc1377
CL
127.Tp Cm \&\-
128A minus sign `\-' which specifies
129.Em left adjustment
a4ba6264 130of the output in the indicated field;
b5dc1377
CL
131.Tp Cm \&+
132A `+' character specifying that there should always be
a4ba6264 133a sign placed before the number when using signed formats.
b5dc1377
CL
134.Tp Sq \&\ \&
135A space specifying that a blank should be left before a positive number
a4ba6264 136for a signed format. A `+' overrides a space if both are used;
b5dc1377
CL
137.Tp Cm \&0
138A zero `0' character indicating that zero-padding should be used
a4ba6264 139rather than blank-padding. A `\-' overrides a `0' if both are used;
b5dc1377
CL
140.Tp
141.De
142.Pp
143.Tw Ds
144.Tp Field Width:
145An optional digit string specifying a
146.Em field width ;
a4ba6264
KB
147if the output string has fewer characters than the field width it will
148be blank-padded on the left (or right, if the left-adjustment indicator
149has been given) to make up the field width (note that a leading zero
150is a flag, but an embedded zero is part of a field width);
b5dc1377
CL
151.Tp Precision:
152An optional period,
153.Sq Cm \&.\& ,
154followed by an optional digit string giving a
155.Em precision
a4ba6264 156which specifies the number of digits to appear after the decimal point,
b5dc1377
CL
157for
158.Cm e
159and
160.Cm f
161formats, or the maximum number of characters to be printed
a4ba6264
KB
162from a string; if the digit string is missing, the precision is treated
163as zero;
b5dc1377
CL
164.Tp Format:
165A character which indicates the type of format to use (one of
166.Cm diouxXfwEgGcs ) .
167.Tp
168.Pp
169A field width or precision may be
170.Sq Cm \&*
171instead of a digit string.
a4ba6264 172In this case an
b5dc1377 173.Ar argument
a4ba6264 174supplies the field width or precision.
b5dc1377 175.Pp
a4ba6264 176The format characters and their meanings are:
b5dc1377
CL
177.Tw Fl
178.Tp Cm diouXx
a4ba6264 179The
b5dc1377 180.Ar argument
a0486b62
KB
181is printed as a signed decimal (d or i), unsigned decimal, unsigned octal,
182or unsigned hexadecimal (X or x), respectively.
b5dc1377 183.Tp Cm f
a4ba6264 184The
b5dc1377
CL
185.Ar argument
186is printed in the style `[\-]ddd.ddd' where the number of d's
a4ba6264
KB
187after the decimal point is equal to the precision specification for
188the argument.
5325ced3
CL
189If the precision is missing, 6 digits are given; if the precision
190is explicitly 0, no digits and no decimal point are printed.
b5dc1377 191.Tp Cm eE
a4ba6264 192The
b5dc1377
CL
193.Ar argument
194is printed in the style
195.Cx `[-]d.ddd
196.Cm e
197.Cx \(+-dd\'
198.Cx
199where there
a4ba6264
KB
200is one digit before the decimal point and the number after is equal to
201the precision specification for the argument; when the precision is
5325ced3 202missing, 6 digits are produced.
a4ba6264 203An upper-case E is used for an `E' format.
b5dc1377 204.Tp Cm gG
a4ba6264 205The
b5dc1377 206.Ar argument
a4ba6264 207is printed in style
b5dc1377 208.Cm f
a4ba6264 209or in style
b5dc1377
CL
210.Cm e
211.Pq Cm E
a4ba6264 212whichever gives full precision in minimum space.
b5dc1377 213.Tp Cm c
a4ba6264 214The first character of
b5dc1377 215.Ar argument
a4ba6264 216is printed.
b5dc1377 217.Tp Cm s
a4ba6264 218Characters from the string
b5dc1377 219.Ar argument
a4ba6264
KB
220are printed until the end is reached or until the number of characters
221indicated by the precision specification is reached; however if the
222precision is 0 or missing, all characters in the string are printed.
b5dc1377 223.Tp Cm \&%
a4ba6264 224Print a `%'; no argument is used.
b5dc1377
CL
225.Tp
226.Pp
a4ba6264
KB
227In no case does a non-existent or small field width cause truncation of
228a field; padding takes place only if the specified field width exceeds
229the actual width.
b5dc1377
CL
230.Sh RETURN VALUE
231.Nm Printf
5d351dd2 232exits 0 on success, 1 on failure.
b5dc1377
CL
233.Sh SEE ALSO
234.Xr printf 3
235.Sh HISTORY
5325ced3
CL
236.Nm Printf
237as a command, appears in 4.3+Reno BSD. It is modeled
238after the standard library function,
239.Xr printf 3 .
b5dc1377 240.Sh BUGS
0e206801
KB
241Since the number is translated from ASCII to floating-point, and
242then back again, floating-point precision may be lost.
b5dc1377 243.Pp
0e206801 244ANSI hexadecimal character constants were deliberately not provided.