fprintf() in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

In the C programming language, fprintf() sends formatted output to a file stream. The fprintf() function helps print content in a file instead of on the stdout console. The definition of the fprintf() function is included in stdio.h header file.

What is fprintf() in C ?

Sometimes we must write data into a file instead of in the stdout console. For such uses, C has a library function fprintf, also known as the format print function, that sends formatted output string to a stream. fprintf function is similar to the printf() function, except that it writes data in a file and not in the stdout console. The fprintf function returns the count of characters successfully written in the file, and an EOF is returned from the function if it fails.

Syntax of fprintf() in C

Let us see the declaration of fprintf() function in C :

The function call of the fprintf in C accepts a stream that is a pointer to a file object in which we need to write data and the format that consists of text to be written to the stream. If it is a success, the function returns an integer value, the count of characters written to the file.

Parameters of fprintf() in C

The fprintf in C accepts two arguments that are as follows :

stream

Stream is the pointer to a FILE object that helps identify the stream of file objects in which data needs to be written.

format

The C string represents the text written in the file pointed by the stream pointer. We can optionally pass format tags that can be replaced by the values specified in the subsequent arguments.

The prototype of the format tag is
% [flags][width][.precision][length]specifier [flags] [width] [.precision] [length] specifier.

The components are explained below:

Flags

FlagsDescription
(space)If no sign is mentioned, a blank space is inserted before the value
-Left-justify the printed text
+This flag forces to prefix the result with a plus (+) or a minus (-) sign
0Instead of spaces, it left-pads the number with the value zero
#This flag is used with o, x or X specifier, and the values are preceded with 0, 0x or 0X respectively for all values other than zero

Width

WidthDescription
(number)The number specifies the minimum number of characters to be written into the file. If the value that is to be printed is smaller than the number, the remaining characters (difference between the two numbers) are replaced with spaces
*It specifies that the width is not mentioned in the format string but is passed as an integer argument following the actual argument that requires formatting

Precision

.precisionDescription
.numberFor integer specifier, i.e., d,i,o,u,x,X,d, i, o, u, x, X,, it sets the minimum number of digits required in the number printed into the file. If the number is smaller than the number (specified in the argument), the number is appended with leading zeros (0). If the number is greater than the number itself, even then, the result is not truncated. All the characters are printed by default (if no value is specified) until a null character is encountered.
.*This indicates that the precision is not mentioned as a part of the format string and is passed as an integer argument to the fprintf() function.

Specifier

SpecifierUse
d or iSigned decimal integer
uUnsigned decimal integer
cC character
e(mantissa) Used for scientific notation using e character
E(mantissa) Used for scientific notation using the E character
fDecimal floating-point number
sString of characters
pPointer address
osigned octal
xunsigned hexadecimal number
Xunsigned hexadecimal number
gshorter of %f or %e
Gshorter of %f or %E

Length

LengthDescription
hThis length argument is interpreted either as unsigned short int or short int and applies only to integer specifier, i.e., d,i,u,x,od, i, u, x, o, and XX
lIt is interpreted as long or unsigned long int for character or character string (c and s) and integer specifier (d,i,o,u,xd, i, o, u, x and XX)
LThis argument is interpreted as a long double and only applies to floating-point specifiers like e,R,f,ge, R, f, g and GG

Additional Arguments

The fprintf in C may expect additional arguments based on the passed format string. These additional arguments expect a value for each % tag specified in the format string (if any).

Return Value of fprintf() in C

The fprintf in C returns an integer value representing the number of successfully written characters into the file. A negative value is returned from the function if an error occurs in writing in the file stream.

Example

To use the fprintf in C, first, we must open the text file in the specific location in write mode, which is done using a file pointer. Then the required string is written into the text file by passing the string and the file pointer in the fprintf() function. For example, to understand how fprintf() is used to write data into a file in C.

Output:

Explanation:

First, we open the file (file_name.txt) in which data needs to be written in write mode. If the file pointer is null, we terminate the program as the file does not exist in the file system. If the file pointer is not null, we are taking input from the user to know the number of names that need to be written in the text file. For each iteration of the for loop, we are taking the input name from the user and writing the name in the text file using the fprintf function fprintf (p_file, "Name %d: %s\n", i, name);. After the code executes, the name and index are visible in the file (file_name.txt) as shown below :

(File: file_name.txt)

fprintf function in C output explanation

More Examples

Let us see another example where we are printing the details of the student taken as input from the user and using formatting options while writing string into the text file.

Output:

In the above example, we format the student's GPA before writing it into the text file. This is why only two digits after the decimal are written in the text file, as we can see from the below screenshot of the file status after the program successfully executes.

fprintf function in C Example

Also, %-10.10s means left-justify content (-), with a minimum of ten characters (%-10.10s) and a maximum of ten characters (%-10.10s) for the string (s).

Conclusion

  • C has a library function fprintf, also known as the format print function, that sends formatted output string to a stream.
  • The fprintf in C is used to write data in a file using a stream object, not in the stdout console.
  • The fprintf function in C returns an integer value representing the number of characters successfully written into the file. A negative value is returned from the function if an error occurs in writing in the file stream.