printf(3) CLIX printf(3)
NAME
printf, fprintf, sprintf - Displays formatted output
LIBRARY
Standard C Library (libc.a)
SYNOPSIS
#include <stdio.h>
int printf(
char *format ,
arg, ... );
int fprintf(
FILE *stream ,
char *format ,
arg, ... );
int sprintf(
char *string ,
char *format ,
arg, ... );
PARAMETERS
stream Specifies the stream on which the output is to be placed.
string Specifies a string to which output is to be written.
format Specifies the format in which output is to be displayed.
arg Specifies data to be formatted as output.
DESCRIPTION
The printf() function places output on stdout. The fprintf() function
places output on the named output stream. The sprintf() function places
``output,'' followed by the null character (\0), in consecutive bytes
starting at *string; it is the user's responsibility to ensure that enough
storage is available. Each function returns the number of characters
transmitted (not including the \0 in the case of the sprintf() function),
or a negative value if an output error was encountered.
Each of these functions converts, formats, and displays its args under
control of the format. The format is a character string that contains two
2/94 - Intergraph Corporation 1
printf(3) CLIX printf(3)
types of objects: plain characters, which are simply copied to the output
stream, and conversion specifications, each of which results in fetching
of zero or more args. The results are undefined if there are insufficient
args for the format. If the format is exhausted while args remains, the
excess args are simply ignored.
Each conversion specification is introduced by the character %. After the
%, the following appear in sequence:
⊕ Zero or more flags, which modify the meaning of the conversion
specification.
⊕ An optional decimal digit string specifying a minimum field width. If
the converted value has fewer characters than the field width, it will
be padded on the left (or right, if the left-adjustment flag `-',
described below, has been given) to the field width. The padding is
with blanks unless the field width digit string starts with 0, in which
case the padding is with 0s.
⊕ An optional precision that gives the minimum number of digits to appear
for the d, i, o, u, x, or X conversions, the number of digits to appear
after the decimal point for the e, E, and f conversions, the maximum
number of significant digits for the g and G conversion, or the maximum
number of characters to be displayed from a string in s conversion.
The precision takes the form of a dot (.) followed by a decimal digit
string; a null digit string is treated as 0. Padding specified by the
precision overrides the padding specified by the field width.
⊕ An optional h specifying that the following d, i, o, u, x, or X
conversion specifier applies to a short int or unsigned short int arg;
an optional h specifying that a following n conversion specifier
applies to a pointer to a short int arg; an optional l (ell)
specifying that a following d, i, o, u, x, or X conversion specifier
applies to a long int or unsigned long int arg; an optional l (ell)
specifying that a following n conversion specifier applies to a pointer
to a long int arg; or an optional L specifying that a following e, E,
f, g, or G conversion specifier applies to a long double argument. In
an h, l (ell), or L appears with any other conversion specifier, it is
ignored.
⊕ A character that indicates the type of conversion to be applied.
A field width or precision or both may 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 must appear before the arg (if any) to be converted. A
negative field width argument is taken as a - flag followed by a positive
field width. If the precision argument is negative, it will be changed to
0.
2 Intergraph Corporation - 2/94
printf(3) CLIX printf(3)
The flag characters and their meanings are:
- The result of the conversion will be left-justified within the
field.
+ The result of a signed conversion always begins with a + or /-
sign.
blank If the first character of a signed conversion is not a sign, a
blank will be prefixed to the result. This implies that if the
blank and + flags both appear, the blank flag will be ignored.
# This flag specifies that the value is to be converted to an
alternate form. For c, d, i, s, 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 0. For x or X conversion, a
nonzero result will have 0x or 0X prefixed to it. For e, E, f, g,
and G conversions, the result always contains a decimal point,
even if no digits follow the point (normally, a decimal point
appears in the result of these conversions only if a digit follows
it). For g and G conversions, trailing zeros are not removed from
the result (which they normally are).
The conversion characters and their meanings are:
d, i, o, u, x, X
The integer arg is converted to signed decimal (d or i), unsigned
octal (o), unsigned decimal (u), or unsigned hexadecimal notation
(x or 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 converted can be represented in fewer digits, it is expanded
with leading zeros. The default precision is 1. The result of
converting a 0 value with a precision of zero is a NULL string.
f The float or double arg is converted to decimal notation in the
style [-]ddd.ddd, where the number of digits after the decimal
point is equal to the precision specification. If the precision is
missing, six digits are output; if the precision is explicitly 0,
no decimal point appears.
e, E The float or double arg is converted in the style [-]d.ddd+dd,
where there is one digit before the decimal point 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
decimal point appears. The E format code will produce a number
with E instead of e introducing the exponent. The exponent always
contains at least two digits.
g, G The float or double arg is displayed in style f or e (or in style E
in the case of a G format code), with the precision specifying the
2/94 - Intergraph Corporation 3
printf(3) CLIX printf(3)
number of significant digits. The style used depends on the value
converted: style e will be used only if the exponent resulting from
the conversion is less than -4 or greater than the precision.
Trailing zeros are removed from the result; a decimal point appears
only if it is followed by a digit.
c The character arg is displayed.
Lf Prints a long double argument. The L modifier also works with the
other floating-point formats (e, E, g, and G).
g Switches from a floating-point format to an exponential format at
an exponent of -4 or -3.
i Is the same as d. Provided for compatibility with the scanf()
function.
n Indicates the argument is a pointer to an int into which is written
the number of characters written to the output stream so far by
this call. No argument is converted. This is useful for keeping
track of the current position in the overall output stream.
p Prints a pointer to a void. The value of the pointer is converted
to a sequence of printable characters, in an implementation-defined
manner. For Intergraph systems, the address is printed in
hexadecimal with no leading 0x.
s The arg is taken to be a string (character pointer) and characters
from the string are displayed until a null character (\0) is
encountered or the number of characters indicated by the precision
specification is reached. If the precision is missing, it is taken
to be infinite, so all characters up to the first null character
are displayed.
% Display a %; no argument is converted.
In displaying floating-point types (float and double), if the exponent is
0x7FF and the mantissa is not equal to 0, the output is as follows:
[-]NaN0xdddddddd
The 0xdddddddd is the hexadecimal representation of the leftmost 32 bits
of the mantissa. If the mantissa is 0, the output is as follows:
[+]inf
In no case does a nonexistent or small 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. Characters
generated by the printf() and fprintf() functions are displayed as if the
putc() function had been called.
4 Intergraph Corporation - 2/94
printf(3) CLIX printf(3)
EXAMPLES
To display a date and time in the form ``Sunday, July 3, 10:02,'' where
weekday and month are pointers to null-terminated strings, use this
statement:
printf("%s, %s %i, %d:%.2d", weekday, month, day, hour, min);
To print to 5 decimal places, use this statement:
printf("pi = %.5f", 4 * atan(1.0));
RETURN VALUES
The functions printf(), fprintf(), and sprintf() each return the number of
characters transmitted. If an error is encountered they return a negative
value.
RELATED INFORMATION
Functions: ecvt(3), putc(3), scanf(3), stdio(3)
2/94 - Intergraph Corporation 5