1PRINTF(P) POSIX Programmer's Manual PRINTF(P)
2
3
4
6 printf - write formatted output
7
9 printf format[argument...]
10
12 The printf utility shall write formatted operands to the standard out‐
13 put. The argument operands shall be formatted under control of the for‐
14 mat operand.
15
17 None.
18
20 The following operands shall be supported:
21
22 format A string describing the format to use to write the remaining op‐
23 erands. See the EXTENDED DESCRIPTION section.
24
25 argument
26 The strings to be written to standard output, under the control
27 of format. See the EXTENDED DESCRIPTION section.
28
29
31 Not used.
32
34 None.
35
37 The following environment variables shall affect the execution of
38 printf:
39
40 LANG Provide a default value for the internationalization variables
41 that are unset or null. (See the Base Definitions volume of
42 IEEE Std 1003.1-2001, Section 8.2, Internationalization Vari‐
43 ables for the precedence of internationalization variables used
44 to determine the values of locale categories.)
45
46 LC_ALL If set to a non-empty string value, override the values of all
47 the other internationalization variables.
48
49 LC_CTYPE
50 Determine the locale for the interpretation of sequences of
51 bytes of text data as characters (for example, single-byte as
52 opposed to multi-byte characters in arguments).
53
54 LC_MESSAGES
55 Determine the locale that should be used to affect the format
56 and contents of diagnostic messages written to standard error.
57
58 LC_NUMERIC
59
60 Determine the locale for numeric formatting. It shall affect the
61 format of numbers written using the e , E , f , g , and G con‐
62 version specifier characters (if supported).
63
64 NLSPATH
65 Determine the location of message catalogs for the processing of
66 LC_MESSAGES .
67
68
70 Default.
71
73 See the EXTENDED DESCRIPTION section.
74
76 The standard error shall be used only for diagnostic messages.
77
79 None.
80
82 The format operand shall be used as the format string described in the
83 Base Definitions volume of IEEE Std 1003.1-2001, Chapter 5, File Format
84 Notation with the following exceptions:
85
86 1. A <space> in the format string, in any context other than a flag of
87 a conversion specification, shall be treated as an ordinary charac‐
88 ter that is copied to the output.
89
90 2. A ' ' character in the format string shall be treated as a ' '
91 character, not as a <space>.
92
93 3. In addition to the escape sequences shown in the Base Definitions
94 volume of IEEE Std 1003.1-2001, Chapter 5, File Format Notation (
95 '\\' , '\a' , '\b' , '\f' , '\n' , '\r' , '\t' , '\v' ), "\ddd" ,
96 where ddd is a one, two, or three-digit octal number, shall be
97 written as a byte with the numeric value specified by the octal
98 number.
99
100 4. The implementation shall not precede or follow output from the d or
101 u conversion specifiers with <blank>s not specified by the format
102 operand.
103
104 5. The implementation shall not precede output from the o conversion
105 specifier with zeros not specified by the format operand.
106
107 6. The e , E , f , g , and G conversion specifiers need not be sup‐
108 ported.
109
110 7. An additional conversion specifier character, b , shall be sup‐
111 ported as follows. The argument shall be taken to be a string that
112 may contain backslash-escape sequences. The following backslash-
113 escape sequences shall be supported:
114
115 * The escape sequences listed in the Base Definitions volume of
116 IEEE Std 1003.1-2001, Chapter 5, File Format Notation ( '\\' ,
117 '\a' , '\b' , '\f' , '\n' , '\r' , '\t' , '\v' ), which shall be
118 converted to the characters they represent
119
120 * "\0ddd" , where ddd is a zero, one, two, or three-digit octal
121 number that shall be converted to a byte with the numeric value
122 specified by the octal number
123
124 * '\c' , which shall not be written and shall cause printf to
125 ignore any remaining characters in the string operand containing
126 it, any remaining string operands, and any additional characters
127 in the format operand
128
129 The interpretation of a backslash followed by any other sequence of
130 characters is unspecified.
131
132 Bytes from the converted string shall be written until the end of the
133 string or the number of bytes indicated by the precision specification
134 is reached. If the precision is omitted, it shall be taken to be infi‐
135 nite, so all bytes up to the end of the converted string shall be writ‐
136 ten.
137
138 8. For each conversion specification that consumes an argument, the
139 next argument operand shall be evaluated and converted to the
140 appropriate type for the conversion as specified below.
141
142 9. The format operand shall be reused as often as necessary to satisfy
143 the argument operands. Any extra c or s conversion specifiers shall
144 be evaluated as if a null string argument were supplied; other
145 extra conversion specifications shall be evaluated as if a zero
146 argument were supplied. If the format operand contains no conver‐
147 sion specifications and argument operands are present, the results
148 are unspecified.
149
150 10. If a character sequence in the format operand begins with a '%'
151 character, but does not form a valid conversion specification, the
152 behavior is unspecified.
153
154 The argument operands shall be treated as strings if the corresponding
155 conversion specifier is b , c , or s ; otherwise, it shall be evaluated
156 as a C constant, as described by the ISO C standard, with the following
157 extensions:
158
159 * A leading plus or minus sign shall be allowed.
160
161 * If the leading character is a single-quote or double-quote, the
162 value shall be the numeric value in the underlying codeset of the
163 character following the single-quote or double-quote.
164
165 If an argument operand cannot be completely converted into an internal
166 value appropriate to the corresponding conversion specification, a
167 diagnostic message shall be written to standard error and the utility
168 shall not exit with a zero exit status, but shall continue processing
169 any remaining operands and shall write the value accumulated at the
170 time the error was detected to standard output.
171
172 It is not considered an error if an argument operand is not completely
173 used for a c or s conversion or if a string operand's first or second
174 character is used to get the numeric value of a character.
175
177 The following exit values shall be returned:
178
179 0 Successful completion.
180
181 >0 An error occurred.
182
183
185 Default.
186
187 The following sections are informative.
188
190 The floating-point formatting conversion specifications of printf() are
191 not required because all arithmetic in the shell is integer arithmetic.
192 The awk utility performs floating-point calculations and provides its
193 own printf function. The bc utility can perform arbitrary-precision
194 floating-point arithmetic, but does not provide extensive formatting
195 capabilities. (This printf utility cannot really be used to format bc
196 output; it does not support arbitrary precision.) Implementations are
197 encouraged to support the floating-point conversions as an extension.
198
199 Note that this printf utility, like the printf() function defined in
200 the System Interfaces volume of IEEE Std 1003.1-2001 on which it is
201 based, makes no special provision for dealing with multi-byte charac‐
202 ters when using the %c conversion specification or when a precision is
203 specified in a %b or %s conversion specification. Applications should
204 be extremely cautious using either of these features when there are
205 multi-byte characters in the character set.
206
207 No provision is made in this volume of IEEE Std 1003.1-2001 which
208 allows field widths and precisions to be specified as '*' since the '*'
209 can be replaced directly in the format operand using shell variable
210 substitution. Implementations can also provide this feature as an
211 extension if they so choose.
212
213 Hexadecimal character constants as defined in the ISO C standard are
214 not recognized in the format operand because there is no consistent way
215 to detect the end of the constant. Octal character constants are lim‐
216 ited to, at most, three octal digits, but hexadecimal character con‐
217 stants are only terminated by a non-hex-digit character. In the ISO C
218 standard, the "##" concatenation operator can be used to terminate a
219 constant and follow it with a hexadecimal character to be written. In
220 the shell, concatenation occurs before the printf utility has a chance
221 to parse the end of the hexadecimal constant.
222
223 The %b conversion specification is not part of the ISO C standard; it
224 has been added here as a portable way to process backslash escapes
225 expanded in string operands as provided by the echo utility. See also
226 the APPLICATION USAGE section of echo for ways to use printf as a
227 replacement for all of the traditional versions of the echo utility.
228
229 If an argument cannot be parsed correctly for the corresponding conver‐
230 sion specification, the printf utility is required to report an error.
231 Thus, overflow and extraneous characters at the end of an argument
232 being used for a numeric conversion shall be reported as errors.
233
235 To alert the user and then print and read a series of prompts:
236
237
238 printf "\aPlease fill in the following: \nName: "
239 read name
240 printf "Phone number: "
241 read phone
242
243 To read out a list of right and wrong answers from a file, calculate
244 the percentage correctly, and print them out. The numbers are right-
245 justified and separated by a single <tab>. The percentage is written to
246 one decimal place of accuracy:
247
248
249 while read right wrong ; do
250 percent=$(echo "scale=1;($right*100)/($right+$wrong)" | bc)
251 printf "%2d right\t%2d wrong\t(%s%%)\n" \
252 $right $wrong $percent
253 done < database_file
254 The command:
255
256
257 printf "%5d%4d\n" 1 21 321 4321 54321
258
259 produces:
260
261
262 1 21
263 3214321
264 54321 0
265
266 Note that the format operand is used three times to print all of the
267 given strings and that a '0' was supplied by printf to satisfy the last
268 %4d conversion specification.
269
270 The printf utility is required to notify the user when conversion
271 errors are detected while producing numeric output; thus, the following
272 results would be expected on an implementation with 32-bit twos-comple‐
273 ment integers when %d is specified as the format operand:
274
275 Standard
276 Argument Output Diagnostic Output
277 5a 5 printf: "5a" not completely converted
278 9999999999 2147483647 printf: "9999999999" arithmetic overflow
279 -9999999999 -2147483648 printf: "-9999999999" arithmetic overflow
280 ABC 0 printf: "ABC" expected numeric value
281
282 The diagnostic message format is not specified, but these examples con‐
283 vey the type of information that should be reported. Note that the
284 value shown on standard output is what would be expected as the return
285 value from the strtol() function as defined in the System Interfaces
286 volume of IEEE Std 1003.1-2001. A similar correspondence exists between
287 %u and strtoul() and %e , %f , and %g (if the implementation supports
288 floating-point conversions) and strtod().
289
290 In a locale using the ISO/IEC 646:1991 standard as the underlying code‐
291 set, the command:
292
293
294 printf "%d\n" 3 +3 -3 \'3 \"+3 "'-3"
295
296 produces:
297
298 3 Numeric value of constant 3
299
300 3 Numeric value of constant 3
301
302 -3 Numeric value of constant -3
303
304 51 Numeric value of the character '3' in the ISO/IEC 646:1991 stan‐
305 dard codeset
306
307 43 Numeric value of the character '+' in the ISO/IEC 646:1991 stan‐
308 dard codeset
309
310 45 Numeric value of the character '-' in the ISO/IEC 646:1991 stan‐
311 dard codeset
312
313
314 Note that in a locale with multi-byte characters, the value of a char‐
315 acter is intended to be the value of the equivalent of the wchar_t rep‐
316 resentation of the character as described in the System Interfaces vol‐
317 ume of IEEE Std 1003.1-2001.
318
320 The printf utility was added to provide functionality that has histori‐
321 cally been provided by echo. However, due to irreconcilable differences
322 in the various versions of echo extant, the version has few special
323 features, leaving those to this new printf utility, which is based on
324 one in the Ninth Edition system.
325
326 The EXTENDED DESCRIPTION section almost exactly matches the printf()
327 function in the ISO C standard, although it is described in terms of
328 the file format notation in the Base Definitions volume of
329 IEEE Std 1003.1-2001, Chapter 5, File Format Notation.
330
332 None.
333
335 awk , bc , echo , the System Interfaces volume of IEEE Std 1003.1-2001,
336 printf()
337
339 Portions of this text are reprinted and reproduced in electronic form
340 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
341 -- Portable Operating System Interface (POSIX), The Open Group Base
342 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
343 Electrical and Electronics Engineers, Inc and The Open Group. In the
344 event of any discrepancy between this version and the original IEEE and
345 The Open Group Standard, the original IEEE and The Open Group Standard
346 is the referee document. The original Standard can be obtained online
347 at http://www.opengroup.org/unix/online.html .
348
349
350
351IEEE/The Open Group 2003 PRINTF(P)