|
» |
|
|
|
NAMEprintf — format and print arguments DESCRIPTIONprintf
writes formatted arguments to the standard output.
The
arg
arguments are formatted under control of the
format
operand. format
is a character string patterned after the formatting conventions of
printf()
(see
printf(3S)),
and contains the following types of objects:
- characters
Characters that are not
escape sequences
or
conversion specifications
(as described below) are copied to standard output. - escape sequences
These are interpreted as non-graphic characters:
- \a
alert - \b
backspace - \f
form-feed - \n
new-line - \r
carriage return - \t
tab - \v
vertical tab - \'
single quote character - \\
backslash - \n
the 8-bit character whose ASCII
code is the 1-, 2-, 3-, or 4-digit octal number
n,
whose first character must be a zero.
- conversion specification
Specifies the output format of each argument (see below). Arguments following
format
are interpreted as strings if the corresponding format is either
c
or
s;
otherwise they are treated as constants.
Conversion SpecificationsEach conversion specification is introduced by the percent character
%.
After the
%
character, the following can appear in the sequence indicated:
- flags
Zero or more
flags,
in any order, which modify the meaning of the conversion specification.
The flag characters and their meanings are:
- -
The result of the conversion is left-justified within the field. - +
The result of a signed conversion always begins with a sign,
+
or
-. - <space>
If the first character of a signed conversion is not a sign,
a space character is prefixed to the result.
This means that if the space flag and
+
flag both appear, the space flag is ignored. - #
The value is to be converted to an "alternate form". For
c,
d,
i,
u,
and
s
conversions, this flag has no effect.
For
o
conversion, it increases the precision
to force the first digit of the result to be a zero.
For
x
or
X
conversion, a non-zero result has
0x
or
0X
prefixed to it.
For
e,
E,
f,
g,
and
G
conversions, the result always contains a radix character,
even if no digits follow the radix character.
For
g
and
G
conversions, trailing zeros are not removed from the result,
contrary to usual behavior.
- field width
An optional string of decimal digits to specify a minimum
field width.
For an output field,
if the converted value has fewer bytes than the field width,
it is padded on the left (or right, if the left-adjustment flag,
-
has been given) to the field width. - precision
The
precision
specifies the minimum number of digits to appear for the
d,
o,
i,
u,
x,
or
X
conversions (the field is padded with leading zeros),
the number of digits to appear after the radix character for the
e
and
f
conversions, the maximum number of significant digits for the
g
conversion, or the maximum number of bytes
to be printed from a string in s conversion.
The precision takes the form of a period
.
followed by a decimal digit string.
A null digit string is treated as a zero. - conversion characters
A
conversion character
indicates the type of conversion to be applied:
- d,i,
- o,u,
- x,X
The integer argument is printed a signed decimal
(d
or
i),
unsigned octal
(o),
unsigned decimal
(u),
or unsigned hexadecimal notation
(x
and
X).
The
x
conversion uses the numbers and letters
0123456789abcdef,
and the
X
conversion uses the numbers and letters
0123456789ABCDEF.
The
precision
component of the argument
specifies the minimum number of digits to appear.
If the value being converted can be represented
in fewer digits than the specified minimum,
it is expanded with leading zeroes.
The default precision is 1.
The result of converting a zero value with a precision of 0
is no characters. - f
The floating-point number argument
is printed in decimal notation in the style
[-]dddrddd
, where the number of digits after the radix character,
r,
is equal to the
precision
specification.
If the
precision
is omitted from the argument, six digits are output; if the
precision
is explicitly 0, no radix appears. - e,E
The floating-point-number argument is printed in the style
[-]drddde±dd
, where there is one digit before the radix character,
and the number of digits after it is equal to the precision.
When the precision is missing, six digits are produced;
if the precision is 0, no radix character appears.
The
E
conversion character produces a number with
E
introducing the exponent instead of
e.
The exponent always contains at least two digits.
However, if the value to be printed
requires an exponent greater than two digits,
additional exponent digits are printed as necessary. - g,G
The floating-point-number argument is printed in style
f
or
e
(or int style
E
in the case of a
G
conversion character),
with the precision specifying the number of significant digits.
The style used depends on the value converted; style
e
is used only if the exponent resulting from the conversion is less than
-h
or greater than or equal to the precision.
Trailing zeros are remove from the result.
A radix character appears only if it is followed by a digit. - c
The first byte of the argument is printed. - s
The argument is taken to be a string,
and characters from the string are printed
until the end of the string
or the number of bytes indicated by the
precision
specification of the argument is reached.
If the precision is omitted from the argument,
it is interpreted as infinite and all characters up to
the end of the string are printed. - %
Print a
%
character; no argument is converted. - b
Similar to the
s
conversion specifier,
except that the string can contain backslash-escape sequences
which are then converted to the characters they represent.
\c
will cause
printf
to ignore any remaining characters in the
string operand containing it, any remaining string operands
and any additional characters in the format operand.
In no case does a nonexistent or insufficient field width
cause truncation of a field;
if the result of a conversion is wider than the field width,
the field is simply expanded to contain the conversion result.
EXTERNAL INFLUENCESEnvironment VariablesLC_CTYPE
determines the interpretation of
arg
as single and/or multi-byte characters. LC_MESSAGES
determines the language in which messages are displayed. If
LC_CTYPE
or
LC_MESSAGES
is not specified in the environment or is set to
the empty string, the value of
LANG
is used as a default for each
unspecified or empty variable.
If
LANG
is not specified or is set to the empty string,
a default of "C" (see
lang(5))
is used instead of
LANG. If any internationalization variable contains an invalid setting,
printf
behaves as if all internationalization variables are set to "C".
See
environ(5). International Code Set SupportSingle and multi-byte character code sets are supported. RETURN VALUEprintf
exits with one of the following values:
- 0
Successful completion. - >0
Errors occurred; the exit value is increased by one for each error that
occurred up to a maximum of 255.
DIAGNOSTICSIf an argument cannot be converted into a form
suitable for the corresponding conversion specification,
or for any other reason cannot be correctly printed,
a diagnostic message is printed to standard error,
the argument is output as a string form
as it was given on the command line,
and the exit value is incremented. EXAMPLESThe following command prints the number 123 in octal, hexadecimal and
floating point formats in their alternate form
printf "%#o, %#x, %#X, %#f, %#g, %#e\n" 123 123 123 123 123 123 resulting in the following output
0173, 0x7b, 0X7B, 123.000000, 123.000, 1.230000e+02 Print the outputs with their corresponding field widths and precision:
printf "%.6d, %10.6d, %.6f, %.6e, %.6s\n" 123 123 1.23 123.4 MoreThanSix resulting in the following output
000123, 000123, 1.230000, 1.234000e+02, MoreTh STANDARDS CONFORMANCEprintf: XPG4, POSIX.2
|