xref: /openbsd-src/usr.bin/printf/printf.1 (revision 505ee9ea3b177e2387d907a91ca7da069f3f14d8)
1.\"	$OpenBSD: printf.1,v 1.34 2020/01/16 16:46:47 schwarze 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: January 16 2020 $
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 argument ...
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 arguments after the first are treated as strings
59if the corresponding format is
60.Cm b ,
61.Cm c
62or
63.Cm s ;
64otherwise it is evaluated as a C constant, with the following extensions:
65.Bl -bullet -offset indent
66.It
67A leading plus or minus sign is allowed.
68.It
69If the leading character is a single or double quote,
70the value is the ASCII code of the next character.
71.El
72.Pp
73The format string is reused as often as necessary to satisfy the arguments.
74Any extra format specifications are evaluated with zero or the null
75string.
76.Pp
77Character escape sequences are in backslash notation as defined in
78.St -ansiC .
79The characters and their meanings are as follows:
80.Pp
81.Bl -tag -width Ds -offset indent -compact
82.It Cm \ea
83Write a <bell> character.
84.It Cm \eb
85Write a <backspace> character.
86.It Cm \ee
87Write an <escape> character.
88.It Cm \ef
89Write a <form-feed> character.
90.It Cm \en
91Write a <new-line> character.
92.It Cm \er
93Write a <carriage return> character.
94.It Cm \et
95Write a <tab> character.
96.It Cm \ev
97Write a <vertical tab> character.
98.It Cm \e\(aq
99Write a <single quote> character.
100.It Cm \e\e
101Write a backslash character.
102.It Cm \e Ns Ar num
103Write an 8-bit character whose ASCII value is
104the 1-, 2-, or 3-digit octal number
105.Ar num .
106.El
107.Pp
108Each format specification is introduced by the percent
109.Pq Sq \&%
110character.
111The remainder of the format specifiers include,
112in the following order:
113.Bl -tag -width Ds
114.It "Zero or more of the following flags:"
115.Bl -tag -width Ds
116.It Cm #
117Specifies that the value should be printed in an
118.Dq alternate form .
119For the
120.Cm o
121format the precision of the number is increased to force the first
122character of the output string to a zero.
123For the
124.Cm x
125.Pq Cm X
126format, a non-zero result has the string
127.Li 0x
128.Pq Li 0X
129prepended to it.
130For
131.Cm a ,
132.Cm A ,
133.Cm e ,
134.Cm E ,
135.Cm f ,
136.Cm F ,
137.Cm g ,
138and
139.Cm G
140formats, the result will always contain a decimal point, even if no
141digits follow the point (normally, a decimal point only appears in the
142results of those formats if a digit follows the decimal point).
143For
144.Cm g
145and
146.Cm G
147formats, trailing zeros are not removed from the result as they
148would otherwise be.
149For all other formats, behaviour is undefined.
150.It Cm \&\-
151Specifies the
152.Em left adjustment
153of the output in the indicated field.
154.It Cm \&+
155Specifies that there should always be
156a sign placed before the number when using signed formats.
157.It Sq \&\ \&
158A space specifies that a blank should be left before a positive number
159for a signed format.
160A
161.Ql +
162overrides a space if both are used.
163.It Cm \&0
164A zero character specifies that zero-padding should be used
165rather than blank-padding.
166This flag is ignored if used with a precision
167specifier and any of the
168.Cm d , i , o , u ,
169or
170.Cm x
171.Pq Cm X
172formats.
173A
174.Ql \&-
175overrides a
176.Ql \&0
177if both are used.
178.El
179.It "Field Width:"
180An optional digit string specifying a
181.Em field width ;
182if the output string has fewer characters than the field width it will
183be blank-padded on the left (or right, if the left-adjustment indicator
184has been given) to make up the field width (note that a leading zero
185is a flag, but an embedded zero is part of a field width).
186.It Precision:
187An optional period
188.Pq Sq \&. ,
189followed by an optional digit string giving a
190.Em precision
191which specifies the number of digits to appear after the decimal point,
192for
193.Cm e
194and
195.Cm f
196formats, or the maximum number of bytes to be printed
197from a string; if the digit string is missing, the precision is treated
198as zero.
199.It Format:
200A character which indicates the type of format to use (one of
201.Cm diouxXfFeEgGaAbcs ) .
202.El
203.Pp
204A field width or precision may be
205.Ql \&*
206instead of a digit string.
207In this case an
208.Ar argument
209supplies the field width or precision.
210.Pp
211The format characters and their meanings are:
212.Bl -tag -width Ds
213.It Cm diouXx
214The
215.Ar argument
216is printed as a signed decimal
217.Pq Cm d No or Cm i ,
218unsigned octal, unsigned decimal,
219or unsigned hexadecimal
220.Pq Cm x No or Cm X ,
221respectively.
222.It Cm fF
223The
224.Ar argument
225is printed in the style
226.Sm off
227.Pf [\-]ddd Cm \&. No ddd
228.Sm on
229where the number of d's
230after the decimal point is equal to the precision specification for
231the argument.
232If the precision is missing, 6 digits are given; if the precision
233is explicitly 0, no digits and no decimal point are printed.
234.Pp
235If the argument is infinity, it will be converted to [-]inf
236.Pq Cm f
237or [-]INF
238.Pq Cm F ,
239respectively.
240If the argument is not-a-number (NaN), it will be converted to
241[-]nan
242.Pq Cm f
243or [-]NAN
244.Pq Cm F ,
245respectively.
246.It Cm eE
247The
248.Ar argument
249is printed in the style
250.Sm off
251.Pf [\-]d Cm \&. No ddd Cm e No \(+-dd
252.Sm on
253where there
254is one digit before the decimal point and the number after is equal to
255the precision specification for the argument; when the precision is
256missing, 6 digits are produced.
257An upper-case
258.Sq E
259is used for an
260.Cm E
261format.
262.Pp
263If the argument is infinity, it will be converted to [-]inf
264.Pq Cm e
265or [-]INF
266.Pq Cm E ,
267respectively.
268If the argument is not-a-number (NaN), it will be converted to
269[-]nan
270.Pq Cm e
271or [-]NAN
272.Pq Cm E ,
273respectively.
274.It Cm gG
275The
276.Ar argument
277is printed in style
278.Cm f
279or in style
280.Cm e
281.Pq Cm E
282whichever gives full precision in minimum space.
283.Pp
284If the argument is infinity, it will be converted to [-]inf
285.Pq Cm g
286or [-]INF
287.Pq Cm G ,
288respectively.
289If the argument is not-a-number (NaN), it will be converted to
290[-]nan
291.Pq Cm g
292or [-]NAN
293.Pq Cm G ,
294respectively.
295.It Cm aA
296The
297.Ar argument
298is printed in style
299.Sm off
300.Pf [\-]0xh Cm \&. No hhh Cm p No [\(+-]d
301.Sm on
302where there is one digit before the hexadecimal point and the number
303after is equal to the precision specification for the argument.
304When the precision is missing, enough digits are produced to convey
305the argument's exact double-precision floating-point representation.
306.Pp
307If the argument is infinity, it will be converted to [-]inf
308.Pq Cm a
309or [-]INF
310.Pq Cm A ,
311respectively.
312If the argument is not-a-number (NaN), it will be converted to
313[-]nan
314.Pq Cm a
315or [-]NAN
316.Pq Cm A ,
317respectively.
318.It Cm b
319Characters from the string
320.Ar argument
321are printed with backslash-escape sequences expanded.
322In the
323.Ar argument ,
324ASCII characters can be octally encoded either as
325.Cm \e0 Ns Ar num
326or as
327.Cm \e Ns Ar num
328like in the
329.Ar format
330string.
331If the
332.Ar argument
333contains the special escape sequence
334.Cm \ec ,
335this escape sequence is discarded together with
336all remaining characters in this argument, all further arguments,
337and all remaining characters in the
338.Ar format
339string.
340.It Cm c
341The first character of
342.Ar argument
343is printed.
344.It Cm s
345Characters from the string
346.Ar argument
347are printed until the end is reached or until the number of bytes
348indicated by the precision specification is reached; however if the
349precision is 0 or missing, all characters in the string are printed.
350.It Cm \&%
351Print a
352.Ql \&% ;
353no argument is used.
354.El
355.Pp
356In no case does a non-existent or small field width cause truncation of
357a field; padding takes place only if the specified field width exceeds
358the actual width.
359.Sh EXIT STATUS
360.Ex -std printf
361.Sh EXAMPLES
362Convert a hexadecimal value to decimal and print it out:
363.Pp
364.Dl $ printf \&"%d\en\&" 0x20
365.Pp
366Print the decimal representation of the character 'a' (see
367.Xr ascii 7 ) :
368.Pp
369.Dl $ printf \&"%d\en\&" \e'a
370.Sh SEE ALSO
371.Xr echo 1 ,
372.Xr printf 3
373.Sh STANDARDS
374The
375.Nm
376utility is compliant with the
377.St -p1003.1-2008
378specification, but in order to produce predictable output
379it deliberately ignores the
380.Xr locale 1
381and always operates as if
382.Ev LC_ALL Ns =C
383were set.
384.Pp
385The escape sequences
386.Cm \ee
387and
388.Cm \e' ,
389as well as omitting the leading digit
390.Cm 0
391from
392.Cm \e0 Ns Ar num
393octal escape sequences in
394.Cm %b
395arguments, are extensions to that specification.
396.Sh HISTORY
397The
398.Nm
399command appeared in
400.Bx 4.3 Reno .
401.Sh CAVEATS
402It is important never to pass a string with user-supplied data as a
403format without using
404.Ql %s .
405An attacker can put format specifiers in the string to mangle your stack,
406leading to a possible security hole.
407.Pp
408Always be sure to use the proper secure idiom:
409.Bd -literal -offset indent
410printf "%s" "$STRING"
411.Ed
412.Sh BUGS
413Since arguments are translated from ASCII to floating-point,
414and then back again, floating-point precision may be lost.
415