printf [ HP C/iX Library Reference Manual ] MPE/iX 5.0 Documentation
HP C/iX Library Reference Manual
printf
Writes data in formatted form to the standard output stream stdout.
Syntax
#include <stdio.h>
int printf (const char *format [,item [,item]...] );
Parameters
format A pointer to a character string defining the format (or the
character string itself enclosed in double quotes).
item,... Each item is a variable or expression specifying the data
to print.
Return Values
>=0 If successful, the number of characters written.
<0 An error occurred.
Description
The printf function enables you to output data in formatted form. In the
printf function, format is a pointer to a character string (or the
character string itself enclosed in double quotes) that specifies the
format and content of the data to be printed. Each item is a variable or
expression specifying the data to print.
The printf() format is similar to the scanf function. It is made up of
conversion specifications and literal characters. Literal characters are
all characters that are not part of a conversion specification. Literal
characters are printed on stdout exactly as they appear in the format.
Conversion Specifications
The following list shows the different components of a conversion
specification in their correct sequence:
1. A percent sign (%), which signals the beginning of a conversion
specification; to output a literal percent sign, you must type two
percent signs (%%).
2. Zero or more flags, which affect the way a value is printed (see
below).
3. An optional decimal digit string which specifies a minimum field
width.
4. An optional precision consisting of a dot (.) followed by a
decimal digit string.
5. An optional l, h, or L indicating that the argument is of an
alternate type. When used in conjunction with an integer
conversion character, an l or h indicates a long or short integer
argument, respectively. When used in conjunction with a
floating-point conversion character, an L indicates a long double
argument.
6. A conversion character, which indicates the type of data to be
converted and printed.
A one-to-one correlation must exist between each specification
encountered and each item in the item list.
The available flags are:
- Causes the data to be left-justified within its output
field. Normally, the data is right-justified.
+ Causes all signed data to begin with a sign (+ or -).
Normally, only negative values have signs.
blank Causes a blank to be inserted before a positive signed
value. This is used to line up positive and negative
values in columnar data. Otherwise, the first digit of a
positive value is lined up with the negative sign of a
negative value. If the blank and + flags both appear, the
blank flag is ignored.
# Causes the data to be printed in an alternate form. Refer
to the descriptions of the conversion characters below for
details concerning the effects of this flag.
0 For d, i, o, u, x, X, e, E, f, g, and G conversions,
leading zeros (following any indication of sign or base)
are used to pad to the field width. Space padding is not
performed. If the 0 and - flag s both appear, the 0 flag
is ignored. The 0 flag is also ignored for d, i, o, u, x,
and X conversions if a precision is specified.
A field width, if specified, determines the minimum number of spaces
allocated to the output field for the particular piece of data being
printed. If the data happens to be smaller than the field width, the
data is blank-padded on the left (or on the right, if the - flag is
specified) to fill the field. If the data is larger than the field
width, the field width is simply expanded to accommodate the data. An
insufficient field width never causes data to be truncated. If field
width is not specified, the resulting field is made just large enough to
hold the data.
The precision is a value which means different things depending on the
conversion character specified. Refer to the descriptions of the
conversion characters below for more details.
NOTE A field width or precision may be replaced by an asterisk (*). If
so, the next item in the item list is fetched, and its value is
used as the field width or precision. The item fetched must be an
integer.
Conversion Characters
Conversion characters specify the type of data to expect in the item list
and cause the data to be formatted and printed appropriately. The
integer conversion characters are:
d, i An integer item is converted to signed decimal. The
precision, if given, specifies the minimum number of digits
to appear. If the value has fewer digits than that
specified by the precision, the value is expanded with
leading zeros. The default precision is 1. A null string
results if a zero value is printed with a zero precision.
The # flag has no effect.
u An integer item is converted to unsigned decimal. The
effects of the precision and the # flag are the same as for
d.
o An integer item is converted to unsigned octal. The #
flag, if specified, causes the precision to be expanded,
and the octal value is printed with a leading zero (a C
convention). The precision behaves the same as in d above,
except that printing a zero value with a zero precision
results in only the leading zero being printed, if the #
flag is specified.
x An integer item is converted to hexadecimal. The letters
abcdef are used in printing hexadecimal values. The #
flag, if specified, causes the precision to be expanded,
and the hexadecimal value is printed with a leading "0x" (a
C convention). The precision behaves as in d above, except
that printing a zero value with a zero precision results in
only the leading "0x" being printed, if the # flag is
specified.
X Same as x above, except that the letters ABCDEF are used to
print the hexadecimal value, and the # flag causes the
value to be printed with a leading "0X".
The character conversion characters are as follows:
c The character specified by the char item is printed. The
precision is meaningless, and the # flag has no effect.
s The string pointed to by the character pointer item is
printed. If a precision is specified, characters from the
string are printed until the number of characters indicated
by the precision is reached, or until a null character is
encountered, whichever comes first. If the precision is
omitted, all characters up to the first null character are
printed. The # flag has no effect.
The floating-point conversion characters are:
f The float or double item is converted to decimal
notation in style f; that is, in the form
[-]ddd.ddd
where the number of digits after the decimal point
is equal to the precision. If precision is not
specified, six digits are printed after the decimal
point. If the precision is explicitly zero, the
decimal point is eliminated entirely. If the #
flag is specified, a decimal point always appears,
even if no digits follow the decimal point.
e The float or double item is converted to scientific
notation in style e; that is, in the form
[-]d.ddde+-ddd
where there is always one digit before the decimal
point. The number of digits after the decimal
point is equal to the precision. If precision is
not given, six digits are printed after the decimal
point. If the precision is explicitly zero, the
decimal point is eliminated entirely. The exponent
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 items 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>
main()
{
double x;
printf("Enter your number: ");
scanf("%F", &x);
printf("Your number is %g\n", x);
printf("Its square is %g\nIts cube is %g\n", x*x, x*x*x); }
The g conversion character is used so that the decision about using an
exponent is automatic. Note that the item list contains expressions to
calculate x squared and x cubed. Also note that the address of the
variable is required in order to read a value for it with scanf(), but
printf() requires the variable name itself.
The following program accepts a decimal integer and prints the integer
itself, its square, and its cube in decimal, octal, and hexadecimal. The
program also prints the headings "Decimal," "Octal," and "Hexadecimal"
and prints the data in tabular form.
#include <stdio.h>
main()
{
long n, n2, n3;
/* get value */
printf("Enter your number: ");
scanf("%D", &n);
/* print headings */
printf("\n\n Decimal Octal Hexadecimal\n");
/* do the computation */
n2 = n * n;
n3 = n * n * n;
printf("n itself: %7ld %9lo %6lx\n", n, n, n);
printf("n squared: %7ld %9lo %6lx\n", n2, n2, n2);
printf("n cubed: %7ld %9lo %6lx\n", n3, n3, n3);
}
Strings are easy to manipulate using the printf() function. The
following program shows how strings can be inserted in text.
#include <stdio.h>
main()
{
char first[15], last[25];
printf("Enter your first and last names: ");
scanf("%s%s", first, last);
printf("\nWell, hello %s, it's good to meet you!\n", first);
printf("%s, huh? Are you any relation to that famous\n", last);
printf("computer programmer, Mortimer Zigfelder %s?\n", last);
printf("No, sorry, that was my mistake. I was thinking of\n");
printf("O'%s, not %s.\n", last, last);
}
See Also
fprintf(), vprintf(), sprintf(), putc(), setlocale(), scanf(), ANSI C
4.9.6.3, POSIX.1 8.1
MPE/iX 5.0 Documentation