The printf precision specifiers set the maximum number of characters (or minimum number of integer digits) to print. A printf precision specification always begins with a period (".") to separate it from any preceding width specifier.
Then, like the width specifier, precision is specified in one of two ways:
•
|
directly, through a decimal digit string;
|
•
|
indirectly, through an asterisk (*).
|
If you use an * for the precision specifier, the next argument in the call (treated as an int) specifies the precision.
If you use asterisks for the width or the precision, or for both, the width argument must immediately follow the specifiers, followed by the precision argument, then the argument for the data to be converted.
[.prec]
|
How Output Precision Is Affected
|
(none)
|
Precision set to default:
= 1 for d,i,,u,x,X types;
= 6 for e,E,f types;
= All significant digits for g,G types;
= Print to first null character for s types;
= No effect on types.
|
.0
|
For d,i,o,u,x types, precision set to default.
for e,E,f types, no decimal point is printed.
|
.n
|
n characters or n decimal places are printed.
If the output value has more than n characters, the output might be truncated or rounded. (Whether this happens depends on the type character.)
|
.
|
The argument list supplies the precision specifier, which must precede the actual argument being formatted.
|
No numeric characters will be output for a field (i.e., the field will be blank) if the following conditions are all met:
•
|
you specify an explicit precision of 0;
|
•
|
the format specifier for the field is one of the integer formats (d, i, o, u, or x);
|
•
|
the value to be printed is 0
|
How [.prec] Affects Conversion
Char Type
|
Effect of [.prec] (.n) on Conversion
|
d
|
Specifies that at least n digits are printed.
|
o
|
output value is left-padded x with zeros.
|
u
|
If input argument has more than n digits,
|
x
|
the output value is not truncated.
|
e
|
Specifies that n characters are
|
E
|
printed after the decimal point, and
|
f
|
the last digit printed is rounded.
|
g
|
Specifies that at most n significant
|
c
|
Has no effect on the output.
|
s
|
Specifies that no more than n characters are printed.
|
|