C stdio fprintf() Function
Example
Write into a file:
FILE *fptr;
// Open a file in writing mode
fptr =
fopen("filename.txt", "w");
// Write some text to the file
fprintf(fptr, "Some text");
// Close the file
fclose(fptr);
Try it Yourself »
Definition and Usage
The fprintf()
function writes a formatted string into a file.
The fprintf()
function is defined in the <stdio.h>
header file.
Format specifiers
The format string can contain format specifiers which describe where and how to represent additional arguments that are passed into the function.
The format specifiers have the form %[flags][width][.precision][length]specifier
. The components in [square brackets] are optional.
An explanation of each of the components:
flags
- Optional. A sequence of any of the following characters:-
- Makes the output left-justified by adding any padding spaces to the right instead of to the left.#
- Shows an alternate representation of the formatted data depending on the conversion.+
- Causes positive numbers to always be prefixed with "+".0
- Pads numbers with zeroes on the left.
width
- Optional. A whole number specifying the minimum number of characters that the output should occupy. If necessary, spaces are added to the right to reach this number, or to the left if the-
flag is used. If an*
asterisk is used then the width is given by the argument preceding the one being represented..precision
- Optional. A.
followed by a whole number indicating how many decimal digits to show in the formatted data.length
- Optional. A sequence of characters which changes the expected data type of the argument. It can be one of the following:hh
- Expectchar
type for whole numbers.h
- Expectshort int
type for whole numbers.l
- Expectlong int
type for whole numbers.
Expectwint_t
type for characters.
Expectwchar_t*
type for strings.ll
- Expectlong long int
type for whole numbers.j
- Expectintmax_t
oruintmax_t
type for whole numbers.z
- Expectsize_t
type for whole numbers.t
- Expectptrdiff_t
type for whole numbers.L
- Expectlong double
type for floating point numbers.
specifier
- Required. A character which indicates how an argument's data should be represented. The list of possible characters is shown in the table below.
List of specifiers
Character | Specifier | Description |
---|---|---|
d or i |
Decimal integer | Represents a whole number as a decimal integer. |
u |
Unsigned decimal integer | Represents a whole number as an unsigned decimal integer. |
o |
Octal integer | Represents a whole number as an octal integer. The "#" flag will prefix the number with "0". |
x or X |
Hexadecimal integer | Represents a whole number as a hexadecimal integer. The "#" flag will prefix the number with "0x". If "X" is used then digits A to F and the letter X are shown in uppercase. |
f or F |
Floating point number | Represents a floating point number. If "F" is used then letters (from values like "nan") will be represented in uppercase. The "#" flag will force a decimal point even if there are no decimal digits. |
e or E |
Scientific notation | Represents a floating point number in scientific notation. If "E" is used then letters will be represented in uppercase. The "#" flag will force a decimal point even if there are no decimal digits. |
g or G |
General number | Uses the shortest representation between f and e for a floating point number. If "G" is used then it chooses between F and E instead. |
a or A |
Hexadecimal floating point number | Writes a floating point number's internal representation with hexadecimal digits. If "A" is used then the digits are represented in uppercase. |
c |
Character | Represents a character. If the argument is an integer then it represents the character for the ASCII value specified by the integer. |
s |
String | Represents a string. |
p |
Pointer | Represents the memory address of a pointer, usually with hexadecimal digits. |
n |
No output | The number of characters that have been written to the file up to this point is written into the argument. The argument must be a pointer to an integer. |
% |
Percent symbol | Represents a literal "%" character. |
A variety of examples of how to use format specifiers can be found in the printf() function reference page.
Syntax
fprintf(FILE * fptr, const char * format, arg1, arg2...);
Parameter Values
Parameter | Description |
---|---|
fptr |
Required. A file pointer, usually created by the fopen() function.
|
format | Required. A string representing the format of the data to be written to the file. |
arg1, arg2... | Optional. Any number of additional arguments, their values can be formatted and written to the file through the specifiers in the format argument. |
Technical Details
Returns: | An int value representing the number of characters written to the file. If an error occurred then it returns a negative number. |
---|