1 .\" $NetBSD: printf.1,v 1.22 2008/09/01 09:20:41 dholland 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. 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.
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
33 .\" from: @(#)printf.1 8.1 (Berkeley) 6/6/93
47 formats and prints its arguments, after the first, under control
52 is a character string which contains three types of objects: plain characters,
53 which are simply copied to standard output, character escape sequences which
54 are converted and copied to the standard output, and format specifications,
55 each of which causes printing of the next successive
60 after the first are treated as strings if the corresponding format is
67 otherwise it is evaluated as a C constant, with the following extensions:
69 .Bl -bullet -offset indent -compact
71 A leading plus or minus sign is allowed.
73 If the leading character is a single or double quote, the value is the
75 code of the next character.
78 The format string is reused as often as necessary to satisfy the
80 Any extra format specifications are evaluated with zero or the null
83 Character escape sequences are in backslash notation as defined in
85 The characters and their meanings are as follows:
86 .Bl -tag -width Ds -offset indent
128 Write a backslash character.
130 Write an 8\-bit character whose
132 value is the 1\-, 2\-, or 3\-digit octal number
135 Write an 8\-bit character whose
137 value is the 1\- or 2\-digit hexadecimal number
141 Each format specification is introduced by the percent character
143 The remainder of the format specification includes,
144 in the following order:
146 .It Zero or more of the following flags :
151 character specifying that the value should be printed in an
152 .Dq alternative form .
159 formats, this option has no effect.
162 format the precision of the number is increased to force the first
163 character of the output string to a zero.
167 format, a non-zero result has the string
178 formats, the result will always contain a decimal point, even if no
179 digits follow the point (normally, a decimal point only appears in the
180 results of those formats if a digit follows the decimal point).
185 formats, trailing zeros are not removed from the result as they
187 .\" I turned this off - decided it isn't a valid use of '#'
190 .\" format, backslash-escape sequences are expanded first;
196 of the output in the indicated field;
200 character specifying that there should always be
201 a sign placed before the number when using signed formats.
203 A space specifying that a blank should be left before a positive number
207 overrides a space if both are used;
209 A zero `0' character indicating that zero-padding should be used
210 rather than blank-padding.
218 An optional digit string specifying a
220 if the output string has fewer characters than the field width it will
221 be blank-padded on the left (or right, if the left-adjustment indicator
222 has been given) to make up the field width (note that a leading zero
223 is a flag, but an embedded zero is part of a field width);
227 followed by an optional digit string giving a
229 which specifies the number of digits to appear after the decimal point,
234 formats, or the maximum number of characters to be printed
242 formats); if the digit string is missing, the precision is treated
245 A character which indicates the type of format to use (one of
246 .Cm diouxXfwEgGbBcs ) .
249 A field width or precision may be
251 instead of a digit string.
254 supplies the field width or precision.
256 The format characters and their meanings are:
261 is printed as a signed decimal (d or i), unsigned octal, unsigned decimal,
262 or unsigned hexadecimal (X or x), respectively.
266 is printed in the style
268 .Pf [\-]ddd Cm \&. No ddd
270 where the number of d's
271 after the decimal point is equal to the precision specification for
273 If the precision is missing, 6 digits are given; if the precision
274 is explicitly 0, no digits and no decimal point are printed.
278 is printed in the style
280 .Pf [\-]d Cm \&. No ddd Cm e No \\*(Pmdd
283 is one digit before the decimal point and the number after is equal to
284 the precision specification for the argument; when the precision is
285 missing, 6 digits are produced.
286 An upper-case E is used for an
297 whichever gives full precision in minimum space.
299 Characters from the string
301 are printed with backslash-escape sequences expanded.
303 The following additional backslash-escape sequences are supported:
308 to ignore any remaining characters in the string operand containing it,
309 any remaining string operands, and any additional characters in
312 Write an 8\-bit character whose
314 value is the 1\-, 2\-, or 3\-digit
318 Write the control character
320 Generates characters `\e000' through `\e037`, and `\e177' (from `\e^?').
324 with the 8th bit set.
325 Generates characters `\e241' through `\e376`.
327 Write the control character
329 with the 8th bit set.
330 Generates characters `\e200' through `\e237`, and `\e377' (from `\eM^?').
333 Characters from the string
335 are printed with unprintable characters backslash-escaped using the
337 .Pf ` Cm \e Ar c No ',
338 .Pf ` Cm \e^ Ar c No ',
339 .Pf ` Cm \eM\- Ar c No '
341 .Pf ` Cm \eM^ Ar c No ',
343 formats described above.
345 The first character of
349 Characters from the string
351 are printed until the end is reached or until the number of characters
352 indicated by the precision specification is reached; if the
353 precision is omitted, all characters in the string are printed.
355 Print a `%'; no argument is used.
358 In no case does a non-existent or small field width cause truncation of
359 a field; padding takes place only if the specified field width exceeds
363 exits 0 on success, 1 on failure.
375 Support for the floating point formats and `*' as a field width and precision
376 are optional in POSIX.
378 The behaviour of the %B format and the \e', \e", \exxx, \ee and
379 \e[M][\-|^]c escape sequences are undefined in POSIX.
381 Since the floating point numbers are translated from
383 to floating-point and
384 then back again, floating-point precision may be lost.
386 Hexadecimal character constants are restricted to, and should be specified
387 as, two character constants.
388 This is contrary to the ISO C standard but
389 does guarantee detection of the end of the constant.
391 All formats which treat the
393 as a number first convert the
395 from its external representation as a character string
396 to an internal numeric representation, and then apply the
397 format to the internal numeric representation, producing
398 another external character string representation.
401 format to do likewise, but in fact it does not.
403 To convert a string representation of a decimal, octal, or hexadecimal
404 number into the corresponding character, two nested
406 invocations may be used, in which the inner invocation
407 converts the input to an octal string, and the outer
408 invocation uses the octal string as part of a format.
409 For example, the following command outputs the character whose code
410 is 0x0A, which is a newline in ASCII:
412 .Dl printf \&"$(printf \&"\e\e%o" \&"0x0A")"