xref: /openbsd-src/usr.bin/printf/printf.1 (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1.\"	$OpenBSD: printf.1,v 1.22 2009/02/08 17:15:10 jmc 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. Neither the name of the University nor the names of its contributors
18.\"    may be used to endorse or promote products derived from this software
19.\"    without specific prior written permission.
20.\"
21.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31.\" SUCH DAMAGE.
32.\"
33.\"	from: @(#)printf.1	5.11 (Berkeley) 7/24/91
34.\"
35.Dd $Mdocdate: February 8 2009 $
36.Dt PRINTF 1
37.Os
38.Sh NAME
39.Nm printf
40.Nd formatted output
41.Sh SYNOPSIS
42.Nm printf
43.Ar format
44.Op Ar arguments ...
45.Sh DESCRIPTION
46.Nm printf
47formats and prints its arguments, after the first, under control
48of the
49.Ar format .
50The
51.Ar format
52is a character string which contains three types of objects: plain characters,
53which are simply copied to standard output, character escape sequences which
54are converted and copied to the standard output, and format specifications,
55each of which causes printing of the next successive
56.Ar argument .
57.Pp
58The
59.Ar arguments
60after the first are treated as strings if the corresponding format is
61.Cm b ,
62.Cm c
63or
64.Cm s ;
65otherwise it is evaluated as a C constant, with the following extensions:
66.Bl -bullet -offset indent
67.It
68A leading plus or minus sign is allowed.
69.It
70If the leading character is a single or double quote, the value is the
71.Tn ASCII
72code of the next character.
73.El
74.Pp
75The format string is reused as often as necessary to satisfy the
76.Ar arguments .
77Any extra format specifications are evaluated with zero or the null
78string.
79.Pp
80Character escape sequences are in backslash notation as defined in
81.St -ansiC .
82The characters and their meanings are as follows:
83.Pp
84.Bl -tag -width Ds -offset indent -compact
85.It Cm \ee
86Write an <escape> character.
87.It Cm \ea
88Write a <bell> character.
89.It Cm \eb
90Write a <backspace> character.
91.It Cm \ef
92Write a <form-feed> character.
93.It Cm \en
94Write a <new-line> character.
95.It Cm \er
96Write a <carriage return> character.
97.It Cm \et
98Write a <tab> character.
99.It Cm \ev
100Write a <vertical tab> character.
101.It Cm \e\'
102Write a <single quote> character.
103.It Cm \e\e
104Write a backslash character.
105.It Cm \e Ns Ar num
106Write an 8-bit character whose
107.Tn ASCII
108value is the 1-, 2-, or 3-digit
109octal number
110.Ar num .
111.El
112.Pp
113Each format specification is introduced by the percent
114.Pq Sq \&%
115character.
116The remainder of the format specifiers include,
117in the following order:
118.Bl -tag -width Ds
119.It "Zero or more of the following flags:"
120.Bl -tag -width Ds
121.It Cm #
122Specifies that the value should be printed in an
123.Dq alternate form .
124For the
125.Cm c ,
126.Cm d ,
127and
128.Cm s
129formats, this option has no effect.
130For the
131.Cm o
132format the precision of the number is increased to force the first
133character of the output string to a zero.
134For the
135.Cm x
136.Pq Cm X
137format, a non-zero result has the string
138.Li 0x
139.Pq Li 0X
140prepended to it.
141For
142.Cm a ,
143.Cm A ,
144.Cm e ,
145.Cm E ,
146.Cm f ,
147.Cm F ,
148.Cm g ,
149and
150.Cm G
151formats, the result will always contain a decimal point, even if no
152digits follow the point (normally, a decimal point only appears in the
153results of those formats if a digit follows the decimal point).
154For
155.Cm g
156and
157.Cm G
158formats, trailing zeros are not removed from the result as they
159would otherwise be.
160.It Cm \&\-
161Specifies the
162.Em left adjustment
163of the output in the indicated field.
164.It Cm \&+
165Specifies that there should always be
166a sign placed before the number when using signed formats.
167.It Sq \&\ \&
168A space specifies that a blank should be left before a positive number
169for a signed format.
170A
171.Ql +
172overrides a space if both are used.
173.It Cm \&0
174A zero character specifies that zero-padding should be used
175rather than blank-padding.
176This flag is ignored if used with a precision
177specifier and any of the
178.Cm d , i , o , u ,
179or
180.Cm x
181.Pq Cm X
182formats.
183A
184.Ql \&-
185overrides a
186.Ql \&0
187if 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.Pq Sq \&. ,
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; if the digit string is missing, the precision is treated
208as zero.
209.It Format:
210A character which indicates the type of format to use (one of
211.Cm diouxXfFeEgGaAbcs ) .
212.El
213.Pp
214A field width or precision may be
215.Ql \&*
216instead of a digit string.
217In this case an
218.Ar argument
219supplies the field width or precision.
220.Pp
221The format characters and their meanings are:
222.Bl -tag -width Fl
223.It Cm diouXx
224The
225.Ar argument
226is printed as a signed decimal
227.Pq Cm d No or Cm i ,
228unsigned octal, unsigned decimal,
229or unsigned hexadecimal
230.Pq Cm x No or Cm X ,
231respectively.
232.It Cm fF
233The
234.Ar argument
235is printed in the style
236.Sm off
237.Pf [\-]ddd Cm \&. No ddd
238.Sm on
239where the number of d's
240after the decimal point is equal to the precision specification for
241the argument.
242If the precision is missing, 6 digits are given; if the precision
243is explicitly 0, no digits and no decimal point are printed.
244.Pp
245If the argument is infinity, it will be converted to [-]inf
246.Pq Cm f
247or [-]INF
248.Pq Cm F ,
249respectively.
250If the argument is not-a-number (NaN), it will be converted to
251[-]nan
252.Pq Cm f
253or [-]NAN
254.Pq Cm F ,
255respectively.
256.It Cm eE
257The
258.Ar argument
259is printed in the style
260.Sm off
261.Pf [\-]d Cm \&. No ddd Cm e No \\*(Pmdd
262.Sm on
263where there
264is one digit before the decimal point and the number after is equal to
265the precision specification for the argument; when the precision is
266missing, 6 digits are produced.
267An upper-case
268.Sq E
269is used for an
270.Cm E
271format.
272.Pp
273If the argument is infinity, it will be converted to [-]inf
274.Pq Cm e
275or [-]INF
276.Pq Cm E ,
277respectively.
278If the argument is not-a-number (NaN), it will be converted to
279[-]nan
280.Pq Cm e
281or [-]NAN
282.Pq Cm E ,
283respectively.
284.It Cm gG
285The
286.Ar argument
287is printed in style
288.Cm f
289or in style
290.Cm e
291.Pq Cm E
292whichever gives full precision in minimum space.
293.Pp
294If the argument is infinity, it will be converted to [-]inf
295.Pq Cm g
296or [-]INF
297.Pq Cm G ,
298respectively.
299If the argument is not-a-number (NaN), it will be converted to
300[-]nan
301.Pq Cm g
302or [-]NAN
303.Pq Cm G ,
304respectively.
305.It Cm aA
306The
307.Ar argument
308is printed in style
309.Sm off
310.Pf [\-]0xh Cm \&. No hhh Cm p No [\\*(Pm]d
311.Sm on
312where there is one digit before the hexadecimal point and the number
313after is equal to the precision specification for the argument.
314When the precision is missing, enough digits are produced to convey
315the argument's exact double-precision floating-point representation.
316.Pp
317If the argument is infinity, it will be converted to [-]inf
318.Pq Cm a
319or [-]INF
320.Pq Cm A ,
321respectively.
322If the argument is not-a-number (NaN), it will be converted to
323[-]nan
324.Pq Cm a
325or [-]NAN
326.Pq Cm A ,
327respectively.
328.It Cm b
329Characters from the string
330.Ar argument
331are printed with backslash-escape sequences expanded.
332.It Cm c
333The first character of
334.Ar argument
335is printed.
336.It Cm s
337Characters from the string
338.Ar argument
339are printed until the end is reached or until the number of characters
340indicated by the precision specification is reached; however if the
341precision is 0 or missing, all characters in the string are printed.
342.It Cm \&%
343Print a
344.Ql \&% ;
345no argument is used.
346.El
347.Pp
348In no case does a non-existent or small field width cause truncation of
349a field; padding takes place only if the specified field width exceeds
350the actual width.
351.Pp
352The
353.Nm
354utility exits 0 on success or 1 on failure.
355.Sh EXAMPLES
356Convert a hexadecimal value to decimal and print it out:
357.Pp
358.D1 Ic $ printf \&"%d\en\&" 0x20
359.Pp
360Print the decimal representation of the character 'a' (see
361.Xr ascii 7 ) :
362.Pp
363.D1 Ic $ printf \&"%d\en\&" \e'a
364.Sh SEE ALSO
365.Xr echo 1 ,
366.Xr printf 3
367.Sh STANDARDS
368The
369.Nm
370utility is compliant with the
371.St -p1003.1-2008
372specification.
373.Sh HISTORY
374The
375.Nm
376command appeared in
377.Bx 4.3 Reno .
378.Sh CAVEATS
379It is important never to pass a string with user-supplied data as a
380format without using
381.Ql %s .
382An attacker can put format specifiers in the string to mangle your stack,
383leading to a possible security hole.
384.Pp
385Always be sure to use the proper secure idiom:
386.Bd -literal -offset indent
387printf "%s" "$STRING"
388.Ed
389.Sh BUGS
390Since arguments are translated from
391.Tn ASCII
392to floating-point, and
393then back again, floating-point precision may be lost.
394