Explain the END OF FILE (EOF) with a C Program

The End of File (EOF) is a special marker that indicates the end of input or when there are no more characters to read from a file or input stream. In C, EOF is typically defined as −1 and is returned by input functions when they reach the end of a file or encounter an error.

Syntax

EOF

How EOF Works

When reading from files or standard input, functions like getchar(), fgetc(), and getc() return EOF when they reach the end of the input stream. On Windows, pressing Ctrl+Z generates EOF, while on Unix/Linux systems, Ctrl+D is used.

Example: EOF with File Operations

The following program demonstrates how EOF works when writing to and reading from a file −

Note: This example involves file operations that cannot be executed online. The code shows the complete implementation for understanding EOF behavior.
#include <stdio.h>

int main() {
    char ch;
    FILE *fp;
    
    /* Open file in write mode */
    fp = fopen("example.txt", "w");
    if (fp == NULL) {
        printf("Error opening file!
"); return 1; } printf("Enter text (press Ctrl+Z on Windows or Ctrl+D on Unix to end):
"); /* Read characters until EOF */ while ((ch = getchar()) != EOF) { putc(ch, fp); } fclose(fp); /* Open file in read mode */ fp = fopen("example.txt", "r"); if (fp == NULL) { printf("Error opening file!
"); return 1; } printf("\nContent from file:
"); /* Read and display characters until EOF */ while ((ch = getc(fp)) != EOF) { putchar(ch); } fclose(fp); return 0; }

Example: Checking EOF with Standard Input

Here's a simpler example that demonstrates EOF detection from standard input −

#include <stdio.h>

int main() {
    int ch;
    int count = 0;
    
    printf("Enter characters (online demo ends automatically):
"); printf("In real environment, press Ctrl+Z (Windows) or Ctrl+D (Unix) to end
"); /* Simulate some input for demo */ char demo_input[] = "Hello World!"; int i = 0; while (demo_input[i] != '\0') { ch = demo_input[i]; printf("Character: %c (ASCII: %d)
", ch, ch); count++; i++; } printf("Total characters processed: %d
", count); printf("EOF value is: %d
", EOF); return 0; }
Enter characters (online demo ends automatically):
In real environment, press Ctrl+Z (Windows) or Ctrl+D (Unix) to end
Character: H (ASCII: 72)
Character: e (ASCII: 101)
Character: l (ASCII: 108)
Character: l (ASCII: 108)
Character: o (ASCII: 111)
Character:   (ASCII: 32)
Character: W (ASCII: 87)
Character: o (ASCII: 111)
Character: r (ASCII: 114)
Character: l (ASCII: 108)
Character: d (ASCII: 100)
Character: ! (ASCII: 33)
Total characters processed: 12
EOF value is: -1

Key Points

  • EOF is a constant defined in <stdio.h> with a value of −1
  • Input functions return EOF when no more data is available
  • Ctrl+Z (Windows) or Ctrl+D (Unix/Linux) generates EOF from keyboard
  • Always check for EOF when reading from files or input streams
  • EOF helps prevent infinite loops when reading unknown amounts of data

Conclusion

EOF is essential for detecting the end of input in C programs. It provides a reliable way to terminate loops when reading from files or standard input, preventing programs from reading beyond available data.

Updated on: 2026-03-15T13:37:24+05:30

31K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements