HP C/iX Library Reference Manual (30026-90004)
274 Chapter5
HP C/iX Library Function Descriptions
printf
always contains exactly three digits. If the # flag is specified, the result
always contains a decimal point, even if no digits follow the decimal point.
E Same as e above, except that E is used to introduce the exponent instead of
e (style E).
g The float or double
item
is converted to either style f or style e,
depending on the size of the exponent. If the exponent resulting from the
conversion is less than -4 or greater than the
precision
, style e is used.
Otherwise, style f is used. The
precision
specifies the number of
significant digits. Trailing zeros are removed from the result, and a
decimal point appears only if it is followed by a digit. If the # flag is
specified, the result always has a decimal point, even if no digits follow the
decimal point, and trailing zeros are not removed.
G Same as the g conversion above, except that style E is used instead of style
e.
p The argument is a pointer to void. The value of the pointer is converted to
a sequence of printable characters.
n The argument is a pointer to an integer into which is written the number
of characters written to the output stream so far by this call to fprintf().
No argument is converted.
% A % is written. No argument is converted. The complete conversion
specification is &%&%.
The
item
s in the item list may be variable names or expressions. Note that, with the
exception of the s conversion, pointers are not required in the item list. If the s conversion
is used, a pointer to a character string must be specified.
Examples
Some examples of printf() conversion specifications and a brief description are shown
below:
%d Output a signed decimal integer. The field width is just large enough to
hold the value.
%-*d Output a signed decimal integer. The left-justify flag (-) and the blank
flag are specified. The asterisk causes a
field width
value to be extracted
from the item list. Thus, the item specifying the desired field width must
occur before the item containing the value to be converted by the d
conversion character.
%+7.2f Output a floating-point value. The + flag causes the value to have an initial
sign (+ or -). The value is right-justified in a 7-column field, and has
exactly two digits after the decimal point. This conversion specification is
ideal for a debit/credit column on a finance worksheet. (If the + sign is not
necessary, use the blank flag instead.)
The following program reads a number from stdin and prints the number, followed by its
square and its cube:
#include <stdio.h>