1 .\" $NetBSD: printf.1,v 1.10 1998/08/22 14:54:48 garbled Exp $
3 .\" Copyright (c) 1989, 1990, 1993
4 .\" The Regents of the University of California. All rights reserved.
6 .\" This code is derived from software contributed to Berkeley by
7 .\" the Institute of Electrical and Electronics Engineers, Inc.
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
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.
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
37 .\" from: @(#)printf.1 8.1 (Berkeley) 6/6/93
52 utility formats and prints its arguments, after the first, under control
57 is a character string which contains three types of objects: plain characters,
58 which are simply copied to standard output, character escape sequences which
59 are converted and copied to the standard output, and format specifications,
60 each of which causes printing of the next successive
65 after the first are treated as strings if the corresponding format is
71 otherwise it is evaluated as a C constant, with the following extensions:
73 .Bl -bullet -offset indent -compact
75 A leading plus or minus sign is allowed.
77 If the leading character is a single or double quote, the value is the
79 code of the next character.
82 The format string is reused as often as necessary to satisfy the
84 Any extra format specifications are evaluated with zero or the null
87 Character escape sequences are in backslash notation as defined in
89 The characters and their meanings
91 .Bl -tag -width Ds -offset indent
93 Write an <escape> character.
95 Write a <bell> character.
97 Write a <backspace> character.
99 Write a <form-feed> character.
101 Write a <new-line> character.
103 Write a <carriage return> character.
105 Write a <tab> character.
107 Write a <vertical tab> character.
109 Write a <single quote> character.
111 Write a backslash character.
113 Write an 8-bit character whose
115 value is the 1-, 2-, or 3-digit
120 Each format specification is introduced by the percent character
122 The remainder of the format specification includes,
123 in the following order:
125 .It "Zero or more of the following flags:"
129 specifying that the value should be printed in an ``alternative form''.
135 formats, this option has no effect. For the
137 formats the precision of the number is increased to force the first
138 character of the output string to a zero. For the
141 format, a non-zero result has the string
151 formats, the result will always contain a decimal point, even if no
152 digits follow the point (normally, a decimal point only appears in the
153 results of those formats if a digit follows the decimal point). For
157 formats, trailing zeros are not removed from the result as they
160 A minus sign `\-' which specifies
162 of the output in the indicated field;
164 A `+' character specifying that there should always be
165 a sign placed before the number when using signed formats.
167 A space specifying that a blank should be left before a positive number
168 for a signed format. A `+' overrides a space if both are used;
170 A zero `0' character indicating that zero-padding should be used
171 rather than blank-padding. A `\-' overrides a `0' if both are used;
174 An optional digit string specifying a
176 if the output string has fewer characters than the field width it will
177 be blank-padded on the left (or right, if the left-adjustment indicator
178 has been given) to make up the field width (note that a leading zero
179 is a flag, but an embedded zero is part of a field width);
183 followed by an optional digit string giving a
185 which specifies the number of digits to appear after the decimal point,
190 formats, or the maximum number of characters to be printed
191 from a string; if the digit string is missing, the precision is treated
194 A character which indicates the type of format to use (one of
195 .Cm diouxXfwEgGbcs ) .
198 A field width or precision may be
200 instead of a digit string.
203 supplies the field width or precision.
205 The format characters and their meanings are:
210 is printed as a signed decimal (d or i), unsigned octal, unsigned decimal,
211 or unsigned hexadecimal (X or x), respectively.
215 is printed in the style
217 .Pf [\-]ddd Cm \&. No ddd
219 where the number of d's
220 after the decimal point is equal to the precision specification for
222 If the precision is missing, 6 digits are given; if the precision
223 is explicitly 0, no digits and no decimal point are printed.
227 is printed in the style
229 .Pf [\-]d Cm \&. No ddd Cm e No \\*(Pmdd
232 is one digit before the decimal point and the number after is equal to
233 the precision specification for the argument; when the precision is
234 missing, 6 digits are produced.
235 An upper-case E is used for an `E' format.
244 whichever gives full precision in minimum space.
246 Characters from the string
248 are printed with backslash-escape sequences expanded.
250 The first character of
254 Characters from the string
256 are printed until the end is reached or until the number of characters
257 indicated by the precision specification is reached; however if the
258 precision is 0 or missing, all characters in the string are printed.
260 Print a `%'; no argument is used.
263 In no case does a non-existent or small field width cause truncation of
264 a field; padding takes place only if the specified field width exceeds
268 exits 0 on success, 1 on failure.
275 utility mostly conforms to
278 Since the floating point numbers are translated from
280 to floating-point and
281 then back again, floating-point precision may be lost.
283 Parsing of - arguments is also somewhat different from
285 where unknown arguments are simply printed instead of being