HP-UX Reference (11i v2 03/08) - 3 Library Functions N-Z (vol 7)

p
printf(3S) printf(3S)
5. A conversion character that indicates the type of conversion to be applied.
A field width or precision can be indicated by an asterisk instead of a digit string. In this case, an integer
arg supplies the field width or precision. The arg that is actually converted is not fetched until the
conversion letter is seen, so the args specifying field width, or precision, or both must appear in that
order before the arg, if any, to be converted. A negative field width is taken as a
- flag followed by a
positive field width. A negative precision is taken as if the precision were omitted. Format strings con-
taining
%n$ conversion specifications can also indicate a field width or precision by the sequence
*n$.
The n indicates the position of an integer arg. With the
*n$ sequence, the args specifying field width or
precision can appear before or after the arg to be converted.
The flag characters and their meanings are:
The integer portion of the result of a decimal conversion (%i, %d, %u, %f, %g, or %G) will
be formatted with thousands grouping characters. Thousands grouping and the separa-
tor character are determined by the "grouping" and "thousands_sep" fields of the
LC_NUMERIC category respectively. (See localedef (4)). For other conversions the
behavior is undefined. The nonmonetary grouping character is used.
- The resulting conversion is left-justified within the field.
+ The resulting signed conversion always begins with a sign (
+ or -).
blank If the first character of a signed conversion is not a sign, a blank is prefixed to the result.
This implies that if the blank and
+ flags both appear, the blank flag is ignored.
# This flag specifies that the value is converted to an ‘‘alternate form’. For
c, d, i, s, n,
and
u conversions, the 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 nonzero result is
prefixed by
0x or 0X.Forap conversion, a nonzero result is prefixed by
0x.Fora, A, e,
E, f, g, and G conversions, the result always contains a radix character, even if no digits
follow the radix. (Normally, a radix character appears in the resulting conversions only
if followed by a digit). For g and G conversions, trailing zeros are not removed from the
result (which they normally are).
0 Leading zeros (following any indication of sign or base) are used to pad to the field width
for all conversion characters. No space padding is performed. If both the
0 and -
appear, the 0 flag is ignored. For d, i, o,
u, p, x, and X, conversions, if a precision is
specified, the
0 flag is ignored.
The conversion characters and their meanings are:
d,i,o,u,x,X The integer arg is converted to signed decimal (
d and i are identical), unsigned
octal (
o), decimal (u), or hexadecimal notation (x and X), respectively; the letters
abcdef are used for x conversion and the letters ABCDEF for X conversion. The
precision specifies the minimum number of digits to appear. If the value being con-
verted can be represented in fewer digits, it is expanded with leading zeros. (For
compatibility with older versions, padding with leading zeros can alternatively be
specified by inserting a zero in front of the field width. This does not imply an octal
value for the field width). The default precision is 1. The result of converting a
zero value with a precision of zero is a null string.
f The double arg is converted to decimal notation in the style [-]dddrddd, where r is
the radix character. The number of digits after the radix character is equal to the
precision specification. If the precision is missing, six digits are output. If the pre-
cision is explicitly zero, no radix character appears.
e,E The double arg is converted in the style [-]drddde±ddd, where r is the radix char-
acter. 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 zero, no radix character appears. The E format code produces a
number with E instead of e introducing the exponent. The exponent always con-
tains at least two digits.
g,G The double arg is printed in style f or e (or in style E in the case of a G format
code), 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 -4 or greater than or equal to the precision. Trailing
zeros are removed from the fractional part of the result; a radix character appears
Section 3720 Hewlett-Packard Company 2 HP-UX 11i Version 2: August 2003