xref: /openbsd-src/usr.bin/printf/printf.1 (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1.\"	$OpenBSD: printf.1,v 1.13 2001/02/17 17:15:27 pjanzen Exp $
2.\"
3.\" Copyright (c) 1989, 1990 The Regents of the University of California.
4.\" 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	5.11 (Berkeley) 7/24/91
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 printf
47.Ar format
48.Op Ar arguments ...
49.Sh DESCRIPTION
50.Nm printf
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
65.Cm b ,
66.Cm c
67or
68.Cm s ;
69otherwise it is evaluated as a C constant, with the following extensions:
70.Bl -bullet -offset indent
71.It
72A leading plus or minus sign is allowed.
73.It
74If the leading character is a single or double quote, the value is the
75.Tn ASCII
76code of the next character.
77.El
78.Pp
79The format string is reused as often as necessary to satisfy the
80.Ar arguments .
81Any extra format specifications are evaluated with zero or the null
82string.
83.Pp
84Character escape sequences are in backslash notation as defined in
85.St -ansiC .
86The characters and their meanings are as follows:
87.Pp
88.Bl -tag -width Ds -offset indent -compact
89.It Cm \ee
90Write an <escape> character.
91.It Cm \ea
92Write a <bell> character.
93.It Cm \eb
94Write a <backspace> character.
95.It Cm \ef
96Write a <form-feed> character.
97.It Cm \en
98Write a <new-line> character.
99.It Cm \er
100Write a <carriage return> character.
101.It Cm \et
102Write a <tab> character.
103.It Cm \ev
104Write a <vertical tab> character.
105.It Cm \e\'
106Write a <single quote> character.
107.It Cm \e\e
108Write a backslash character.
109.It Cm \e Ns Ar num
110Write an 8-bit character whose
111.Tn ASCII
112value is the 1-, 2-, or 3-digit
113octal number
114.Ar num .
115.El
116.Pp
117Each format specification is introduced by the percent
118.Pq Sq \&%
119character.
120The remainder of the format specifiers include,
121in the following order:
122.Bl -tag -width Ds
123.It "Zero or more of the following flags:"
124.Bl -tag -width Ds
125.It Cm #
126Specifies that the value should be printed in an
127.Dq alternate form .
128For the
129.Cm c ,
130.Cm d ,
131and
132.Cm s
133formats, this option has no effect.
134For the
135.Cm o
136format the precision of the number is increased to force the first
137character of the output string to a zero.
138For the
139.Cm x
140.Pq Cm X
141format, a non-zero result has the string
142.Li 0x
143.Pq Li 0X
144prepended to it.
145For
146.Cm e ,
147.Cm E ,
148.Cm f ,
149.Cm g ,
150and
151.Cm G
152formats, the result will always contain a decimal point, even if no
153digits follow the point (normally, a decimal point only appears in the
154results of those formats if a digit follows the decimal point).
155For
156.Cm g
157and
158.Cm G
159formats, trailing zeros are not removed from the result as they
160would otherwise be.
161.It Cm \&\-
162Specifies the
163.Em left adjustment
164of the output in the indicated field.
165.It Cm \&+
166Specifies that there should always be
167a sign placed before the number when using signed formats.
168.It Sq \&\ \&
169A space specifies that a blank should be left before a positive number
170for a signed format.
171A
172.Ql +
173overrides a space if both are used.
174.It Cm \&0
175A zero character specifies that zero-padding should be used
176rather than blank-padding.
177This flag is ignored if used with a precision
178specifier and any of the
179.Cm d , i , o , u ,
180or
181.Cm x
182.Pq Cm X
183formats.
184A
185.Ql \&-
186overrides a
187.Ql \&0
188if both are used.
189.El
190.It "Field Width:"
191An optional digit string specifying a
192.Em field width ;
193if the output string has fewer characters than the field width it will
194be blank-padded on the left (or right, if the left-adjustment indicator
195has been given) to make up the field width (note that a leading zero
196is a flag, but an embedded zero is part of a field width).
197.It Precision:
198An optional period
199.Pq Sq \&. ,
200followed by an optional digit string giving a
201.Em precision
202which specifies the number of digits to appear after the decimal point,
203for
204.Cm e
205and
206.Cm f
207formats, or the maximum number of characters to be printed
208from a string; if the digit string is missing, the precision is treated
209as zero.
210.It Format:
211A character which indicates the type of format to use (one of
212.Cm diouxXfEgGbcs ) .
213.El
214.Pp
215A field width or precision may be
216.Ql \&*
217instead of a digit string.
218In this case an
219.Ar argument
220supplies the field width or precision.
221.Pp
222The format characters and their meanings are:
223.Bl -tag -width Fl
224.It Cm diouXx
225The
226.Ar argument
227is printed as a signed decimal
228.Pq Cm d No or Cm i ,
229unsigned octal, unsigned decimal,
230or unsigned hexadecimal
231.Pq Cm x No or Cm X ,
232respectively.
233.It Cm f
234The
235.Ar argument
236is printed in the style
237.Sm off
238.Pf [\-]ddd Cm \&. No ddd
239.Sm on
240where the number of d's
241after the decimal point is equal to the precision specification for
242the argument.
243If the precision is missing, 6 digits are given; if the precision
244is explicitly 0, no digits and no decimal point are printed.
245.It Cm eE
246The
247.Ar argument
248is printed in the style
249.Sm off
250.Pf [\-]d Cm \&. No ddd Cm e No \\*(Pmdd
251.Sm on
252where there
253is one digit before the decimal point and the number after is equal to
254the precision specification for the argument; when the precision is
255missing, 6 digits are produced.
256An upper-case
257.Sq E
258is used for an
259.Cm E
260format.
261.It Cm gG
262The
263.Ar argument
264is printed in style
265.Cm f
266or in style
267.Cm e
268.Pq Cm E
269whichever gives full precision in minimum space.
270.It Cm b
271Characters from the string
272.Ar argument
273are printed with backslash-escape sequences expanded.
274.It Cm c
275The first character of
276.Ar argument
277is printed.
278.It Cm s
279Characters from the string
280.Ar argument
281are printed until the end is reached or until the number of characters
282indicated by the precision specification is reached; however if the
283precision is 0 or missing, all characters in the string are printed.
284.It Cm \&%
285Print a
286.Ql \&% ;
287no argument is used.
288.El
289.Pp
290In no case does a non-existent or small field width cause truncation of
291a field; padding takes place only if the specified field width exceeds
292the actual width.
293.Pp
294The
295.Nm
296utility exits 0 on success or 1 on failure.
297.Sh EXAMPLES
298Convert a hexidecimal value to decimal and print it out:
299.Pp
300.D1 Ic printf \&"%d\en\&" 0x20
301.Pp
302Print the decimal representation of the character 'a' (see
303.Xr ascii 7 ) :
304.Pp
305.D1 Ic printf \&"%d\en\&" \e'a
306.Sh SEE ALSO
307.Xr echo 1 ,
308.Xr printf 3
309.Sh STANDARDS
310The
311.Nm printf
312utility conforms to
313.St -p1003.2-92 .
314.Sh HISTORY
315The
316.Nm
317command appeared in
318.Bx 4.3 Reno .
319.Sh CAVEATS
320It is important never to pass a string with user-supplied data as a
321format without using
322.Ql %s .
323An attacker can put format specifiers in the string to mangle your stack,
324leading to a possible security hole.
325.Pp
326Always be sure to use the proper secure idiom:
327.Bd -literal -offset indent
328printf "%s" "$STRING"
329.Ed
330.Sh BUGS
331Since arguments are translated from
332.Tn ASCII
333to floating-point, and
334then back again, floating-point precision may be lost.
335