EOF, getc() and feof() in C

Last Updated : 27 Jul, 2026

When reading data from files in C, it is important to detect when the end of a file is reached and handle input operations correctly. The EOF constant, along with functions such as getc() and feof(), helps developers perform reliable and error-free file reading.

  • Learn how EOF, getc(), and feof() work together to detect the end of a file during file input operations.
  • Understand why feof() is needed to distinguish between an actual end-of-file condition and a file read error.

EOF (End of File)

EOF (End of File) is a predefined constant macro in C that indicates the end of a file during file input operations. It is defined in the <stdio.h> header file.

  • Used by file input functions like getc(), fgetc(), and scanf() to detect the end of a file.
  • Its value is implementation-defined, but it is commonly -1.
C
#include <stdio.h>

int main() {
    FILE *fp = fopen("data.txt", "r");

    if (fp == NULL) {
        printf("File not found.\n");
        return 1;
    }

    int ch;

    while ((ch = fgetc(fp)) != EOF) {
        printf("%c", ch);
    }

    fclose(fp);
    return 0;
}

Syntax

#include <stdio.h>
if (ch == EOF){
// End of file reached
}

getc() Function

The getc() function is used to read a single character from a file stream. It is defined in the <stdio.h> header file.

  • Reads one character at a time from the specified file stream.
  • Returns the character read, or EOF if the end of the file is reached or an error occurs.
C
#include <stdio.h>

int main() {
    FILE *fp = fopen("data.txt", "r");

    if (fp == NULL) {
        printf("File not found.\n");
        return 1;
    }

    int ch;

    while ((ch = getc(fp)) != EOF) {
        printf("%c", ch);
    }

    fclose(fp);
    return 0;
}

Syntax

int getc(FILE *fptr);

feof() Function

The feof() function is used to check whether the end of a file has been reached for a given file stream. It is defined in the <stdio.h> header file.

  • Returns a non-zero value if the end of the file is reached.
  • Returns 0 if the end of the file has not been reached.
C
#include <stdio.h>

int main() {
    FILE *fp = fopen("data.txt", "r");

    if (fp == NULL) {
        printf("File not found.\n");
        return 1;
    }

    while (getc(fp) != EOF);

    if (feof(fp)) {
        printf("End of file reached.\n");
    }

    fclose(fp);
    return 0;
}

Syntax

int feof(FILE *fptr);

Need of feof()

getc() returns the End of File (EOF) when the end of the file is reached. getc() also returns EOF when it fails. So, only comparing the value returned by getc() with EOF is not sufficient to check for the actual end of the file. To solve this problem, C provides feof().

C++
#include <stdio.h>

int main() {
  
    FILE *fptr = fopen("file.txt", "w");
    char ch;

    // Try to read a character using getc()
    ch = getc(fptr);
  
  	// Handle the EOF return value
    if (ch == EOF)
        printf("End of File or Unable to Read");
    else
        printf("Read Character: %c", ch);
  
    fclose(fptr);
    return 0;
}

Output
End of File or Unable to Read

In the above program, the getc() function should be unable to read as the file is opened in the write mode only. But it still returns EOF because of which it becomes difficult to find the source of error. Here, the feof() function can be specifically used to check for End of File.

C
#include <stdio.h>

int main() {
  
    FILE *fptr = fopen("file.txt", "w");
    char ch;

    // Try to read a character using getc()
    ch = getc(fptr);
  
  	// Handle the EOF return value
    if (ch == EOF) {
      
      	// Check if the end of file is reached
      	// or just getc() is unable to read
      	if (feof(fptr) == EOF)
        	printf("End of File");
      else
        	printf("Unable to Read");
    }
    else
        printf("Read Character: %c", ch);
  
    fclose(fptr);
    return 0;
}

Output
Unable to Read
Comment