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