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