0% found this document useful (0 votes)
5 views6 pages

Understanding the fprintf Function in C

The document provides an overview of the C standard input/output library, specifically focusing on the 'fprintf' function, which sends formatted text to a stream. It details the function's parameters, usage, and various formatting options including conversion specifications and flags. Additionally, it includes compatibility notes and specifics about handling file operations in different modes.

Uploaded by

mrvcarvalho
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Understanding the fprintf Function in C

The document provides an overview of the C standard input/output library, specifically focusing on the 'fprintf' function, which sends formatted text to a stream. It details the function's parameters, usage, and various formatting options including conversion specifications and flags. Additionally, it includes compatibility notes and specifics about handling file operations in different modes.

Uploaded by

mrvcarvalho
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

stdio.

h
Standard input/output

if (( f = fopen("foofoo", "a")) == NULL) {


printf("Can't append to file.\n");
exit (1) ;

// output numbers 10 to 19
for (; count <20; count++)
fprintf(f, "%5d\n", count);

// close file
fclose(f);

return 0;

fprintf
Description Send formatted text to a stream.

Compatibility This function is compatible with the following targets:

ANSI BeOS EMB/RTOS Mac OS Palm OS Win32

Prototype #include <stdio.h>


int fprintf(FILE *stream,
const char

Parameters Parameters for this facility are:


stream FILE * A pointer to a FILE stream
format const char * Th e format string

Remarks The fprintf ( ) function writes formatted text to s t ream and ad-
\rances the file position indicator. Its operation is the same as
printf ( ) with the addition of the stream argument. Refer to the
description of printf O .

MCR-238 MSL C Reference


stdio.h
Standard input/output

If the file is opened in update mode (+) the file cannot be written to
and then read from unless the write operation and read operation
are separated by an operation that flushes the stream's buffer. This
can be done with the fflush() function or one of the file positioning
operations (fseek(), fsetpos(), or rewind()).

NOTE: On embedded/ RTOS systems this function only is imple-


mented for stdin, stdout and stderr files.

Output Control String and Conversion Specifiers

The f ormat character array contains normal text and conversion


specifications. Conversion specifications must have matching argu-
ments in the same order in which they occur in f ormat.

The various elements of the format string is specified in the ANSI


standards to be in this order from left to right.
• A percent sign
• Optional flags -,+,0,# or space
• Optional minimum field width specification
• Optional precision specification
• Optional size specification
• Conversion operator c,d,e,E,f,g,G,i,n,o,p,s,u,x,X or `)/0

A conversion specification describes the format its associated argu-


ment is to be converted to. A specification starts with a percent sign
(%), optional flag characters, an optional minimum width, an op-
tional precision width, and the necessary, terminating conversion
type. Doubling the percent sign (96%) results in the output of a single

An optional flag character modifies the formatting of the output; it


can be left or right justified, and numerical values can be padded
with zeroes or output in alternate forms. More than one optional
flag character can be used in a conversion specificatiort. "Format
modifier types for formatted output functions" on page 240 de-
scribes the flag characters.

MSL C Reference MCR-239


stdio.h
Standard input/output

The optional minimum width is a decimal digit string. If the con-


verted value has more characters that the minimum width, it is ex-
panded as required. If the converted value has fewer characters
than the minimum width, it is, by default, right justified (padded on
the left). If the - flag character is used, the converted value is left jus-
tified (padded on the right).

NOTE: The maximum minimum field width allowed in MSL


Standard Libraries is 509 characters.

The optional precision width is a period character ( . ) followed by


decimal digit string. For floating point values, the precision width
specifies the number of digits to print after the decimal point. For
integer values, the precision width functions identically to, and can-
cels, the minimum width specification. When used with a character
array, the precision width indicates the maximum width of the out-
put.

A minimum width and a precision width can also be specified with


an asterisk (*) instead of a decimal digit string. An asterisk indicates
that there is a matching argument, preceding the conversion argu-
ment, specifying the minimum width or precision width.

The terminating character, the conversion type, specifies the conver-


sion applied to the conversion specification's matching argument.
"Format modifier types for formatted output functions" on page
240 describes the conyersion type characters.

Table 25.2 Format modifier types for formatted output functions

Modifier Description

Size

h The h flag followed by d, 1, o, u, x, or X con-


version specifier indicates that the corresponding ar-
gument is a short int or unsigned short int.

MCR-240 MSL C Reference


stdio.h
Standard input/output

1 The lower case L followed by d, i, o, u, x, ar


X conversion specifier indicates the argument is a
long int or unsigned long int .

11 The double 1 followed by d, o, u, x, or X


conversion specifier indicates the argument is a long
long ar unsigned long long

The upper case L followed by e, E, f, g, ar G


conversion specifier indicates a long
double.

Flags
The conversion will be left justified.
The conversion, if numeric, will be prefixed with a
sign
(+ or -). By default, only negative numeric values are
prefixed with a minus sign (-).

space If the first character of the conversion is not a sign


character, it is prefixed with a space. Because the plus
sign flag character (+) always prefixes a numeric
value with a sign, the space flag has no effect when
combined with the plus flag.
For c, d, i , and u conversion types, the # flag has no
effect. For s conversion types, a pointer to a Pascal
string, is output as a character string. For o conver-
sion types, the # flag prefixes the conversion with a O.
For x conversion types with this flag, the conversion
is prefixed with a Ox. For e, E, f, g, and G conversions,
the # flag forces a decimal point in the output. For g
and G conversions with this flag, trailing zeroes after
the decimal point are not removed.

MSL C Reference MCR-241


stdio.h
Standard input/output

o This flag pads zeroes on the left of the conversion. It


applies to d, i, o, u, x, X, e, E, f, g, and G conversion
types. The leading zeroes follow sign and base indica-
tion characters, replacing what would normally be
space characters. The minus sign flag character over-
rides the 0 flag character. The O flag is ignored when
used with a precision width for d, i , o, u, x, and x
conversion types.
Conversions
The corresponding argument is converted to a signed
decimal.
The corresponding argument is converted to a signed
decimal.
o The argument is converted to an unsigned octal.
The argument is converted to an unsigned decimal.
x, X The argument is converted to an unsigned hexadeci-
mal. The x conversion type uses lowercase letters
(abcdef) while )( uses uppercase letters (ABCDEF).
This conversion type stores the number of items out-
put by printf ( ) so far. Its corresponding argument
must be a pointer to an int.
The corresponding floating point argument (float,
or double) is printed in decimal notation. The default
precision is 6 (6 digits after the decimal point). If the
precision width is explicitly O, the decimal point is not
printed.

MCR-242 MSL C Reference


stdio.h
Standard input/output

e, E The floating point argument (f 1 oat or doubl e) is


output in scientific notation: [Link]±Eee. There is
one digit (b) before the decimal point. Unless indi-
cated by an optional precision width, the default is 6
digits after the decimal point (aaa). If the precision
width is O, no decimal point is output. The exponent
(ee) is at least 2 digits long.
The e conversion type uses lowercase e as the expo-
nent prefix. The E conversion type uses uppercase E
as the exponent prefix.
g, G The g conversion type uses the f or e conversion
types and the G conversion type uses the f or E con-
version types. Conversion type e (or E) is used only if
the converted exponent is less than -4 or greater than
the precision width. The precision width indicates the
number of significant digits. No decimal point is out-
put if there are no digits following it.
c The corresponding argument is output as a character.

The corresponding argument, a pointer to a character


array, is output as a character string. Character string
output is completed when a null character is reached.
The null character is not output.
The corresponding argument is taken to be a pointer.
The argument is output using the x conversion type
format.

CodeWarrior Extensions

#s The corresponding argument, a pointer to a Pascal


string, is output as a character string. A Pascal charac-
ter string is a length byte followed by the number
characters specified in the length byte.
Note: This conversion type is an extension to the
ANSI C library but applied in the same manner as for
other format variations.

Return fprintf ( ) returns the number of arguments written or a negative


number if an error occurs.

MSL C Reference MCR-243

You might also like