EOF, getc() and feof() in C

In C programming, EOF (End of File), getc(), and feof() are essential for file handling operations. EOF is a constant that indicates the end of a file stream, while getc() reads characters from files and feof() checks if the end-of-file indicator has been set.

EOF (End of File)

EOF stands for End of File and is a macro defined in <stdio.h>. The getc() function returns EOF when it reaches the end of file or encounters an error.

Syntax

#define EOF (-1)

Example: Using EOF with getc()

Note: This example assumes a file named "input.txt" exists. Create this file with sample content to test the program.

#include <stdio.h>

int main() {
    FILE *f = fopen("input.txt", "r");
    if (f == NULL) {
        printf("Error opening file!<br>");
        return 1;
    }
    
    int c = getc(f);
    printf("File contents:<br>");
    while (c != EOF) {
        putchar(c);
        c = getc(f);
    }
    
    fclose(f);
    return 0;
}
File contents:
This is demo!
This is demo!

getc() Function

The getc() function reads a single character from a file stream and returns it as an integer. If it reaches end of file or encounters an error, it returns EOF.

Syntax

int getc(FILE *stream);

Parameters

  • stream − Pointer to a FILE object that specifies the input stream

Return Value

  • Returns the character read as an int value
  • Returns EOF on end of file or error

Example: Reading Characters with getc()

#include <stdio.h>

int main() {
    FILE *f = fopen("sample.txt", "w");
    if (f != NULL) {
        fprintf(f, "Hello World!");
        fclose(f);
    }
    
    f = fopen("sample.txt", "r");
    if (f == NULL) {
        printf("Error opening file!<br>");
        return 1;
    }
    
    printf("Reading file character by character:<br>");
    int c;
    while ((c = getc(f)) != EOF) {
        printf("'%c' ", c);
    }
    
    fclose(f);
    return 0;
}
Reading file character by character:
'H' 'e' 'l' 'l' 'o' ' ' 'W' 'o' 'r' 'l' 'd' '!'

feof() Function

The feof() function tests the end-of-file indicator for a given file stream. It's used to check if the end of file has been reached after an EOF condition occurs.

Syntax

int feof(FILE *stream);

Parameters

  • stream − Pointer to a FILE object that identifies the stream

Return Value

  • Returns non-zero value if end-of-file indicator is set
  • Returns zero otherwise

Example: Using feof() to Check End of File

#include <stdio.h>

int main() {
    FILE *f = fopen("test.txt", "w");
    if (f != NULL) {
        fprintf(f, "Line 1\nLine 2\nLine 3");
        fclose(f);
    }
    
    f = fopen("test.txt", "r");
    if (f == NULL) {
        printf("Error opening file!<br>");
        return 1;
    }
    
    int c;
    while ((c = getc(f)) != EOF) {
        putchar(c);
    }
    
    if (feof(f)) {
        printf("\nSuccessfully reached end of file.<br>");
    } else {
        printf("\nError occurred while reading file.<br>");
    }
    
    fclose(f);
    return 0;
}
Line 1
Line 2
Line 3
Successfully reached end of file.

Key Differences

Function Purpose Return Value When to Use
EOF End-of-file constant -1 (typically) Compare with getc() return
getc() Read single character Character or EOF Reading file contents
feof() Check end-of-file indicator Non-zero or zero Verify EOF condition

Conclusion

EOF, getc(), and feof() work together for robust file handling in C. Use getc() to read characters, check against EOF for end conditions, and use feof() to distinguish between end-of-file and error conditions.

Updated on: 2026-03-15T09:56:54+05:30

27K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements