C fprintf() Function Tutorial

In this section, we will learn what the fprintf() function is and how it works in C.

Writing to a File in C: C fprintf() Function

The `fprintf()` function is just like the `printf()` function, with the exception that it can send the content to a declared file instead of just the output stream. Basically, it will take the address of an opened file and send any content we put as its argument to that target file.

Note: the prototype of this function exists in the `stdio.h` header file and so we need to include the file in order to work with this function.

C fprintf() Function Syntax:

Here’s the prototype of the function:

int fprintf(FILE *stream, const char *format, ...)

C fprintf() Function Parameters

The first argument of the function is the address of the memory-space allocated to the FILE-structure of the target file.

Note: we get this address via the call to the `fopen()` function.

The rest of the arguments for the `fprintf` function are exactly like the `printf()` function. (Read the printf() section to learn more about other arguments like format specifier etc.)

C fprintf() Function Return Value

In a successful operation, the number of characters that were written to the target file will be returned. Otherwise, a negative value will return.

Example: using fprintf() function in C

#include <stdio.h>
#include <stdlib.h>

int main() {

    //Call the the fopen function in order to open the file in read-write mode.
    FILE *file = fopen("G:/fileOne.txt","w+");
    //if there was a problem on opening the file, exit the program.
    if (file == NULL){
        printf("Could not open the file");


    char *string = "Hello,\nMy name is John Doe!\n";
    fprintf(file, "%s",string);


    return 0;



Top Technologies