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