printf(3S) printf(3S)
NAME
printf, fprintf, sprintf - print formatted output
SYNOPSIS
#include <stdio.h>
int printf (format [ , arg ] ... )
char *format;
int fprintf (stream, format [ , arg ] ... )
FILE *stream;
char *format;
SYNOPSIS (SYSV.2)
int sprintf (s, format [ , arg ] ... )
char *s, format;
SYNOPSIS (4.2BSD)
char *sprintf (s, format [ , arg ] ... )
char *s, format;
DESCRIPTION
printf places output on the standard output stream stdout.
Fprintf places output on the named output stream. Sprintf
places ''output,'' followed by the null character (\0), in
consecutive bytes starting at *s; it is the user's responsi-
bility to ensure that enough storage is available.
Each of these functions converts, formats, and prints its
args under control of the format. The format is a character
string that contains two types of objects: plain charac-
ters, 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 remain, the excess args are simply
ignored.
Each conversion specification is introduced by the character
%. After the %, the following appear in sequence:
An optional field, consisting of a decimal digit string
followed by a $, specifying the next args to be con-
verted. If this field is not provided, the args fol-
lowing the last args converted will be used.
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 charac-
ters than the field width, it will be padded on the
Page 1 CX/UX Programmer's Reference Manual
printf(3S) printf(3S)
left (or right, if the left-adjustment flag '-',
described below, has been given) to the field width.
If the field width for an s conversion is preceded by a
0, the string is right adjusted with zero-padding on
the left.
A 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 printed from a
string in s conversion. The precision takes the form
of a period (.) followed by a decimal digit string; a
null digit string is treated as zero.
An optional h specifying that a following d, i, o, u,
x, or X conversion character applies to a short int or
unsigned short int arg; an optional h specifying that a
following n conversion character applies to a pointer
to short int arg; an optional l (ell) specifying that a
following d, i, o, u, x, or X conversion character
applies to a long int or unsigned long int arg; an
optional l specifying that a following n conversion
character applies to a pointer to long arg; or an
optional L specifying that a following e, E, f, g, or G
conversion character applies to a long double arg. A
h, l, or L before any other conversion character is
ignored.
A character that indicates the type of conversion to be
applied.
A field width or precision 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 preci-
sion must appear before the arg (if any) to be converted.
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 will always
begin with a sign (+ or -).
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 con-
verted to an ''alternate form.'' For c, d, i, s,
and u conversions, the flag has no effect. For o
Page 2 CX/UX Programmer's Reference Manual
printf(3S) printf(3S)
conversion, it increases the precision to force
the first digit of the result to be a zero. For x
or X conversion, a non-zero result will have 0x or
0X prefixed to it. For e, E, f, g, and G conver-
sions, the result will always contain a decimal
point, even if no digits follow the point (nor-
mally, a decimal point appears in the result of
these conversions only if a digit follows it).
For g and G conversions, trailing zeroes will not
be removed from the result (which they normally
are).
0 This flag causes padding with leading zeroes. For
d, i, o, u, x, X, e, E, f, g, and G conversions,
leading zeroes (following any sign or base indica-
tions) are used to pad to the field width (i.e.,
no space padding is performed). If both the 0 and
the - flags appear, the default action is to
ignore the the second of the flags. In programs
that are linked in one of the ANSI C compilation
modes (see hc(1)), and in programs which are
linked in 88open OCS-compliant mode, the 0 flag
will be ignored. For d, i, o, u, x, and X conver-
sions, the 0 flag will be ignored if a precision
is specified. For other conversions the behavior
is undefined.
The conversion characters and their meanings are:
d,i,o,u,x,X
The integer arg is converted to signed decimal,
unsigned octal, decimal, 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
converted can be represented in fewer digits, it
will be expanded with leading zeroes. (For compa-
tibility with older versions, padding with leading
zeroes may alternatively be specified by prepend-
ing a zero to 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 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 preci-
sion is explicitly 0, no decimal point appears.
e,E The float or double arg is converted in the style
''[-]d.ddde+dd,'' where there is one digit before
Page 3 CX/UX Programmer's Reference Manual
printf(3S) printf(3S)
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 preci-
sion 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 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 signi-
ficant 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
zeroes are removed from the result; a decimal
point appears only if it is followed by a digit.
c The character arg is printed.
s The arg is taken to be a string (character
pointer) and characters from the string are
printed until a null character (\0) is encountered
or the number of characters indicated by the pre-
cision specification is reached. If the precision
is missing, it is taken to be infinite, so all
characters up to the first null character are
printed. A NULL value for arg will yield unde-
fined results.
p The args should be a pointer to void. The value
of the pointer is converted to an implementation-
defined set of sequences of printable characters,
which should be the same as the set of sequences
that are matched by the %p conversion of the scanf
function.
n The arg is taken to be an integer pointer into
which is written the number of characters output
so far to the output stream by the current printf
call. No argument is converted.
% Print a %; no argument is converted.
In no case does a non-existent 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
printf and fprintf are printed as if putc(3S) had been
called.
RETURN VALUE
In the att universe, each function returns the number of
characters transmitted (not including the \0 in the case of
sprintf), or a negative value if an output error was encoun-
tered.
Page 4 CX/UX Programmer's Reference Manual
printf(3S) printf(3S)
In the ucb universe, printf and fprintf return 0 upon suc-
cessful completion, or a negative value if an output error
was encountered. Sprintf returns its first argument: s.
EXAMPLES
To print a date and time in the form ''Sunday, July 3,
10:02,'' where weekday and month are pointers to null-
terminated strings:
printf("%s, %s %d, %d:%.2d", weekday, month, day, hour, min);
To print pi to 5 decimal places:
printf("pi = %.5f", 4 * atan(1.0));
NOTES
The f, e, and g conversion specifiers cause the correspond-
ing argument to be accessed as if it were a double precision
floating point argument. When floating point arguments are
passed as single precision values, they may print out
incorrectly under one of these specifiers. It is advised
that floating point arguments be passed as double precision
values in such cases.
SEE ALSO
hc(1), ecvt(3C), putc(3S), scanf(3S), stdio(3S).
Page 5 CX/UX Programmer's Reference Manual