Facebook Page Twitter Page LinkedIn Page
× C Standard Library

The C <stdio.h> fclose() function is used to close the given file stream. Any unwritten buffered data are flushed. Any unread buffered data are discarded.

Even if the call fails, the stream and its buffers are no longer associated with the file. The behavior is undefined if the value of the pointer stream is used after the call.


int fclose ( FILE * stream );


stream Specify a pointer to a FILE object that specifies the stream to be closed.

Return Value

Returns 0 if the file is successfully closed, otherwise EOF is returned.


Lets assume that we have a file called test.txt. This file contains following content:

This is a test file.
This is a test file.

In the below example, file is opened using fopen. If the int value of the argument is not EOF, it will read the file. Then, it reads a single character from the input and return an int value. If it fails, it returns EOF.

#include <stdio.h>
int main (){
  //open the file in read mode
  FILE *pFile = fopen("test.txt", "r");
  //first character in the file
  int c = getc(pFile);
  //if first character is not EOF, reads and writes
  //characters from the file until EOF is not reached
  while (c != EOF) {
    c = getc(pFile);
  //close the file
  return 0;

The output of the above code will be:

This is a test file.
This is a test file.

❮ C <stdio.h> Library