xref: /netbsd/usr.bin/printf/printf.1 (revision c4a72b64)
1.\"	$NetBSD: printf.1,v 1.17 2002/11/24 22:35:45 christos Exp $
2.\"
3.\" Copyright (c) 1989, 1990, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" the Institute of Electrical and Electronics Engineers, Inc.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\" 3. All advertising materials mentioning features or use of this software
18.\"    must display the following acknowledgement:
19.\"	This product includes software developed by the University of
20.\"	California, Berkeley and its contributors.
21.\" 4. Neither the name of the University nor the names of its contributors
22.\"    may be used to endorse or promote products derived from this software
23.\"    without specific prior written permission.
24.\"
25.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35.\" SUCH DAMAGE.
36.\"
37.\"	from: @(#)printf.1	8.1 (Berkeley) 6/6/93
38.\"
39.Dd November 5, 1993
40.Dt PRINTF 1
41.Os
42.Sh NAME
43.Nm printf
44.Nd formatted output
45.Sh SYNOPSIS
46.Nm
47.Ar format
48.Op Ar arguments  ...
49.Sh DESCRIPTION
50.Nm
51formats and prints its arguments, after the first, under control
52of the
53.Ar format  .
54The
55.Ar format
56is a character string which contains three types of objects: plain characters,
57which are simply copied to standard output, character escape sequences which
58are converted and copied to the standard output, and format specifications,
59each of which causes printing of the next successive
60.Ar argument  .
61.Pp
62The
63.Ar arguments
64after the first are treated as strings if the corresponding format is
65either
66.Cm b ,
67.Cm B ,
68.Cm c
69or
70.Cm s ;
71otherwise it is evaluated as a C constant, with the following extensions:
72.Pp
73.Bl -bullet -offset indent -compact
74.It
75A leading plus or minus sign is allowed.
76.It
77If the leading character is a single or double quote, the value is the
78.Tn ASCII
79code of the next character.
80.El
81.Pp
82The format string is reused as often as necessary to satisfy the
83.Ar arguments  .
84Any extra format specifications are evaluated with zero or the null
85string.
86.Pp
87Character escape sequences are in backslash notation as defined in
88.St -ansiC .
89The characters and their meanings are as follows:
90.Bl -tag -width Ds -offset indent
91.It Cm \ee
92Write an \*[Lt]escape\*[Gt] character.
93.It Cm \ea
94Write a \*[Lt]bell\*[Gt] character.
95.It Cm \eb
96Write a \*[Lt]backspace\*[Gt] character.
97.It Cm \ef
98Write a \*[Lt]form-feed\*[Gt] character.
99.It Cm \en
100Write a \*[Lt]new-line\*[Gt] character.
101.It Cm \er
102Write a \*[Lt]carriage return\*[Gt] character.
103.It Cm \et
104Write a \*[Lt]tab\*[Gt] character.
105.It Cm \ev
106Write a \*[Lt]vertical tab\*[Gt] character.
107.It Cm \e\'
108Write a \*[Lt]single quote\*[Gt] character.
109.It Cm \e"
110Write a \*[Lt]double quote\*[Gt] character.
111.It Cm \e\e
112Write a backslash character.
113.It Cm \e Ns Ar num
114Write an 8\-bit character whose
115.Tn ASCII
116value is the 1\-, 2\-, or 3\-digit octal number
117.Ar num .
118.It Cm \ex Ns Ar xx
119Write an 8\-bit character whose
120.Tn ASCII
121value is the 1\- or 2\-digit hexadecimal number
122.Ar xx .
123.El
124.Pp
125Each format specification is introduced by the percent character
126(``%'').
127The remainder of the format specification includes,
128in the following order:
129.Bl -tag -width Ds
130.It "Zero or more of the following flags:"
131.Bl -tag -width Ds
132.It Cm #
133A `#' character
134specifying that the value should be printed in an ``alternative form''.
135For
136.Cm b ,
137.Cm c ,
138.Cm d ,
139and
140.Cm s
141formats, this option has no effect.
142For the
143.Cm o
144format the precision of the number is increased to force the first
145character of the output string to a zero.
146For the
147.Cm x
148.Pq Cm X
149format, a non-zero result has the string
150.Li 0x
151.Pq Li 0X
152prepended to it.
153For
154.Cm e  ,
155.Cm E ,
156.Cm f  ,
157.Cm g ,
158and
159.Cm G
160formats, the result will always contain a decimal point, even if no
161digits follow the point (normally, a decimal point only appears in the
162results of those formats if a digit follows the decimal point).
163For
164.Cm g
165and
166.Cm G
167formats, trailing zeros are not removed from the result as they
168would otherwise be.
169.\" I turned this off - decided it isn't a valid use of '#'
170.\" For the
171.\" .Cm B
172.\" format, backslash-escape sequences are expanded first;
173.It Cm \&\-
174A minus sign `\-' which specifies
175.Em left adjustment
176of the output in the indicated field;
177.It Cm \&+
178A `+' character specifying that there should always be
179a sign placed before the number when using signed formats.
180.It Sq \&\ \&
181A space specifying that a blank should be left before a positive number
182for a signed format.
183A `+' overrides a space if both are used;
184.It Cm \&0
185A zero `0' character indicating that zero-padding should be used
186rather than blank-padding.
187A `\-' overrides a `0' if both are used;
188.El
189.It "Field Width:"
190An optional digit string specifying a
191.Em field width ;
192if the output string has fewer characters than the field width it will
193be blank-padded on the left (or right, if the left-adjustment indicator
194has been given) to make up the field width (note that a leading zero
195is a flag, but an embedded zero is part of a field width);
196.It Precision :
197An optional period,
198.Sq Cm \&.\& ,
199followed by an optional digit string giving a
200.Em precision
201which specifies the number of digits to appear after the decimal point,
202for
203.Cm e
204and
205.Cm f
206formats, or the maximum number of characters to be printed
207from a string
208.Sm off
209.Pf ( Cm b No ,
210.Sm on
211.Cm B
212and
213.Cm s
214formats); if the digit string is missing, the precision is treated
215as zero;
216.It Format :
217A character which indicates the type of format to use (one of
218.Cm diouxXfwEgGbBcs ) .
219.El
220.Pp
221A field width or precision may be
222.Sq Cm \&*
223instead of a digit string.
224In this case an
225.Ar argument
226supplies the field width or precision.
227.Pp
228The format characters and their meanings are:
229.Bl -tag -width Fl
230.It Cm diouXx
231The
232.Ar argument
233is printed as a signed decimal (d or i), unsigned octal, unsigned decimal,
234or unsigned hexadecimal (X or x), respectively.
235.It Cm f
236The
237.Ar argument
238is printed in the style
239.Sm off
240.Pf [\-]ddd Cm \&. No ddd
241.Sm on
242where the number of d's
243after the decimal point is equal to the precision specification for
244the argument.
245If the precision is missing, 6 digits are given; if the precision
246is explicitly 0, no digits and no decimal point are printed.
247.It Cm eE
248The
249.Ar argument
250is printed in the style
251.Sm off
252.Pf [\-]d Cm \&. No ddd Cm e No \\*(Pmdd
253.Sm on
254where there
255is one digit before the decimal point and the number after is equal to
256the precision specification for the argument; when the precision is
257missing, 6 digits are produced.
258An upper-case E is used for an `E' format.
259.It Cm gG
260The
261.Ar argument
262is printed in style
263.Cm f
264or in style
265.Cm e
266.Pq Cm E
267whichever gives full precision in minimum space.
268.It Cm b
269Characters from the string
270.Ar argument
271are printed with backslash-escape sequences expanded.
272.br
273The following additional backslash-escape sequences are supported:
274.Bl -tag -width Ds
275.It Cm \ec
276Causes
277.Nm
278to ignore any remaining characters in the string operand containing it,
279any remaining string operands, and any additional characters in
280the format operand.
281.It Cm \e0 Ns Ar num
282Write an 8\-bit character whose
283.Tn ASCII
284value is the 1\-, 2\-, or 3\-digit
285octal number
286.Ar num .
287.It Cm \e^ Ns Ar c
288Write the control character
289.Ar c .
290Generates characters `\e000' through `\e037`, and `\e177' (from `\e^?').
291.It Cm \eM\- Ns Ar c
292Write the character
293.Ar c
294with the 8th bit set.
295Generates characters `\e241' through `\e376`.
296.It Cm \eM^ Ns Ar c
297Write the control character
298.Ar c
299with the 8th bit set.
300Generates characters `\e000' through `\e037`, and `\e177' (from `\eM^?').
301.El
302.It Cm B
303Characters from the string
304.Ar argument
305are printed with unprintable characters backslash-escaped using the
306.Sm off
307.Pf ` Cm \e Ar c No ',
308.Pf ` Cm \e^ Ar c No ',
309.Pf ` Cm \eM\- Ar c No '
310or
311.Pf ` Cm \eM^ Ar c No ',
312.Sm on
313formats described above.
314.It Cm c
315The first character of
316.Ar argument
317is printed.
318.It Cm s
319Characters from the string
320.Ar argument
321are printed until the end is reached or until the number of characters
322indicated by the precision specification is reached; if the
323precision is omitted, all characters in the string are printed.
324.It Cm \&%
325Print a `%'; no argument is used.
326.El
327.Pp
328In no case does a non-existent or small field width cause truncation of
329a field; padding takes place only if the specified field width exceeds
330the actual width.
331.Sh EXIT STATUS
332.Nm
333exits 0 on success, 1 on failure.
334.Sh SEE ALSO
335.Xr echo 1 ,
336.Xr printf 3 ,
337.Xr printf 9
338.Xr vis 3
339.Sh STANDARDS
340The
341.Nm
342utility conforms to
343.St -p1003.1-2001 .
344.Pp
345Support for the floating point formats and `*' as a field width and precision
346are optional in POSIX.
347.Pp
348The behaviour of the %B format and the \e', \e", \exxx, \ee and
349\e[M][\-|^]c escape sequences are undefined in POSIX.
350.Sh BUGS
351Since the floating point numbers are translated from
352.Tn ASCII
353to floating-point and
354then back again, floating-point precision may be lost.
355.Pp
356Hexadecimal character constants are restricted to, and should be specified
357as, two character constants.  This is contrary to the ISO C standard but
358does guarantee detection of the end of the constant.
359