fprintf function — Writes formatted data
int fprintf(FILE* stream, const char* format, . . . )
The fprintf
function writes
formatted output to stream
. The
format
parameter contains the
formatting information, and the remaining arguments are printed
according to the format. The return value is the number of
characters printed, or a negative value for an error.
Characters in format
are
printed verbatim except for conversion specifications, which begin
with a percent sign (%
). Each
conversion specification is made up of the following parts (in
order): flags, field width, precision, size, and conversion
specifier.
The following are detailed descriptions of the parts of a conversion specification:
The flag characters are optional and can appear in any order. Table 13-5 lists the flag characters and their meanings.
Table 13-5. Formatting flag characters
Flag | Description |
---|---|
| Left-justified (default is right-justified). |
| Signed conversions always begin with a sign (default is to use a sign only if the value is negative). |
Space | The output is an initial space character if a signed conversion results in an empty string or a string that does not start with a sign character (+ takes precedence over space). |
| Use an alternate form: insert a |
| Fields are padded with leading zeros
(after the sign or base indication). The |
An optional number that specifies the minimum number of
characters that the field will occupy. If the field is an
asterisk (*
), the field
width is obtained from the next argument to be
processed.
An optional number of digits for an integer, number of decimal digits for a floating-point number, or maximum size for a string. The precision is specified as a dot (.) followed by a number or an asterisk.
The character h
,
l
, or L
. h
means an integer is short
or unsigned
short
. l
means an integer is long
or unsigned
long
, a character is wint_t
, or a string is a pointer to
wchar_t
. L
means a floating-point number is
long
double
.
Specifies the type of the argument containing the data to be printed using a conversion specification. It must be one of the following:
d
, i
Signed decimal integer.
o
Unsigned octal integer.
u
Unsigned decimal integer.
x
, X
Unsigned hexadecimal integer. x
writes the digits a
-f
in lowercase, and X
writes the digits A
-F
in uppercase.
f
Fixed-precision floating point.
e
, E
Exponential floating point. The exponent is
introduced with e
or
E
, matching the
conversion character.
g
, G
General floating point. Use style f
or e
. Use style e
if the exponent is less than
-4 or greater than the precision; otherwise, use style
f
. Trailing zeros are
dropped, and a trailing decimal point is dropped if it
would be the last character.
c
Character. The argument must be an unsigned
char
promoted to int
, or, if the l
size modifier is used, the
argument must be wchar_t
promoted to wint_t
, which is then printed
as a multibyte character.
s
String. The argument is a pointer to a
null-terminated array of characters, or, if the l
size modifier is used, the
argument must be a pointer to a wchar_t
array, which is
converted to a series of multibyte characters.
p
Pointer. The argument must be a pointer to
void
. The output
format is implementation-defined.
n
The argument must be a pointer to an integer;
fprintf
stores in the
integer the number of characters written so far. Use the
h
or l
size modifiers if the
argument is a pointer to short
int
or long
int
.
%
Prints a literal %
.
It is your responsibility to ensure that the argument types
match the format
. Any errors
result in undefined behavior. Mismatches between format
and the argument is a common and
sometimes subtle source of error. You can avoid these problems
entirely by using C++ I/O streams instead of fprintf
and related functions.
All the printf
-related
functions interpret the format string identically.
The following are examples of calling printf
:
long double pi = 3.141592653589792L; int i = 42; const char greeting[] = "Hello, how are you?"; printf(">%d %% %Lg<\n", i, pi); // Prints >42 % 3.14159< printf(">%4d<\n", i); // Prints > 42< printf(">%-16.8Le<\n", pi); // Prints >3.14159265e+00 < printf(">%#*.*x<\n", 8, 4, i); // Prints > 0x002a< printf(">%.5s<\n", greeting); // Prints >Hello<
fscanf function, printf function, sprintf function, vfprintf function, wcrtomb
in <cwchar>
, fwprintf
in <cwchar>