f78
printf,
fprintf,
sprintf
[format output]
SYNOPSIS #include <stdio.h> int printf(const char *format [, arg, ...]); int fprintf(FILE *fd, const char *format [, arg, ...]); int sprintf(char *str, const char *format [, arg, ...]);
DESCRIPTION
printf
accepts a series of arguments, applies
to each a format specifier from *format,
and writes the formatted data to stdout,
terminated with a null character. The behavior of printf
is undefined if there are not enough
arguments for the format. printf
returns when it reaches the end of
the format string. If there are more arguments than the format requires,
excess arguments are ignored.
fprintf and sprintf are identical to printf, other than the destination of the formatted output: fprintf sends the output to a specified file, fd, while sprintf stores the output in the specified char array, str. For sprintf, the behavior is also undefined if the output, *str, overlaps with one of the arguments. format is a pointer to a character string containing two types of objects: ordinary characters (other than %), which are copied unchanged to the output, and conversion specifications, each of which is introduced by %. (To include % in the output, use %% in the format string.) A conversion specification has the following form:
%[flags][width][.prec][size][type]
The fields of the conversion specification have the following meanings.
-
The result of the conversion is
left justified, and the right is padded with blanks. If you do not use
this flag, the result is right justified, and padded on the left.
+
The result of a signed conversion
(as determined by type)
will always begin with a plus or minus sign. (If you do not use this flag,
positive values do not begin with a plus sign.)
space
If the first character of a signed conversion specification is not a sign,
or if a signed conversion results in no characters, the result will begin
with a space. If the space flag and the plus (+)
flag both appear, the space flag is ignored.
0
If the type character is d,
i,
o,
u,
x,
X,
e,
E,
f,
g,
or G:
leading zeroes, are used to pad the field width (following any indication
of sign or base); no spaces are used for padding. If the zero (0) and minus
(-)
flags both appear, the zero (0)
flag will be ignored. For d,
i,
o,
u,
x,
and X
conversions, if a precision prec
is specified, the zero (0)
flag is ignored. Note that 0
is interpreted as a flag, not as the beginning of a field width.
#
The result is to be converted to
an alternative form, according to the next character:
0
Increases precision to force the
first digit of the result to be a zero.
x
A non-zero result will have a 0x
prefix.
X
A non-zero result will have a 0X
prefix.
e,
E
or f
The result will always contain
a decimal point even if no digits follow the point. (Normally, a decimal
point appears only if a digit follows it.) Trailing zeroes are removed.
g
or G
Same as e
or E,
but trailing zeroes are not removed.
All others
Undefined.
%
Prints the
percent character (%).
c
Prints arg
as single character.
s
Prints characters
until precision is reached or a null terminator is encountered; takes a
string pointer.
d
Prints a signed
decimal integer; takes an int
(same as
ffb
FONT>i).
i
Prints a signed
decimal integer; takes an int
(same as d).
o
Prints a signed
octal integer; takes an int.
u
Prints an
unsigned decimal integer; takes an int.
x
Prints an
unsigned hexadecimal integer (using abcdef
as digits beyond 9);
takes an int.
X
Prints an
unsigned hexadecimal integer (using ABCDEF
as digits beyond 9);
takes an int.
f
Prints a signed
value of the form [-]9999.9999;
takes a floating point number.
e
Prints a signed
value of the form [-]9.9999e[+|-]999;
takes a floating point number.
E
Prints the
same way as e,
but using E
to introduce the exponent; takes a floating point number.
g
Prints a signed
value in either f
or e
form, based on given value and precision—trailing zeros and the decimal
point are pri
ffb
nted only if necessary; takes a floating point number.
G
Prints the
same way as g,
but using E
for the exponent if an exponent is needed; takes a floating point number.
n
Stores (in
the same object) a count of the characters written; takes a pointer to
int.
p
Prints a pointer
in an implementation-defined format. This implementation treats the pointer
as an unsigned
long
(same as Lu).
RETURNS
sprintf
returns the number of bytes in the
output string, save that the concluding NULL
is not counted. printf
and fprintf
return the number of characters transmitted.
If an error occurs, printf
and fprintf
return EOF.
No error returns occur for sprintf.
COMPLIANCE
The ANSI standard for C specifies that implementations must support
formatted output of up to 509 characters.
Supporting OS subroutines
required: close,
fstat,
isatty,
lseek,
read,
sbrk,
write