C Programming Tutorial 0/65 lessons ~6 min read Lesson 45

    EOF, getc() and feof()

    EOF, getc() and feof() builds on this idea: Detecting end-of-file while reading char by char.

    Course progress0%
    Focus
    9 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    EOF, getc() and feof() builds on this idea: Detecting end-of-file while reading char by char. You will see the syntax, a runnable snippet, and habits that keep programs safe.

    Understanding the topic

    What you will learn Detecting end-of-file while reading char by char.

    How it fits in C EOF, getc() and feof() shows up in real programs as declarations, expressions, and library calls — always compile with warnings enabled.

    Try the sample Copy the example, build it with gcc or clang, then change inputs to see how output shifts.

    Next steps Reuse EOF, getc() and feof() in a small exercise before mixing it with pointers, arrays, or file I/O.

    • What you will learn — Detecting end-of-file while reading char by char.
    • How it fits in C — EOF, getc() and feof() shows up in real programs as declarations, expressions, and library calls — always compile with warnings enabled.
    • Try the sample — Copy the example, build it with gcc or clang, then change inputs to see how output shifts.
    • Next steps — Reuse EOF, getc() and feof() in a small exercise before mixing it with pointers, arrays, or file I/O.

    Step-by-step explanation

    1. What you will learn — Detecting end-of-file while reading char by char.
    2. How it fits in C — EOF, getc() and feof() shows up in real programs as declarations, expressions, and library calls — always compile with warnings enabled.
    3. Try the sample — Copy the example, build it with gcc or clang, then change inputs to see how output shifts.
    4. Next steps — Reuse EOF, getc() and feof() in a small exercise before mixing it with pointers, arrays, or file I/O.

    Informative example

    Example program:

    c
    #include <stdio.h>
    int main(void) {
    printf("Demo: EOF, getc() and feof()\n");
    return 0;
    }

    Output

    Demo: EOF, getc() and feof()

    Execution workflow

    1EOF, getc() and feof() — step by step
    1 / 4

    What you will learn

    Detecting end-of-file while reading char by char.

    Best practices

    • Enable warnings: gcc -Wall -Wextra -std=c11 source.c -o app
    • Give every variable a defined value before it is read.
    • Stay inside array bounds — C will not stop you from over-running a buffer.

    Common mistakes

    • Reading uninitialized storage — behavior is undefined.
    • Dismissing compiler warnings instead of fixing root causes.
    • Ignoring NULL returns from malloc, fopen, and similar APIs.

    Hands-on exercise

    Practice problems:

    • Code a tiny demo of EOF, getc() and feof()
    • Mix EOF, getc() and feof() with a concept from the previous module

    Summary

    EOF, getc() and feof(): Detecting end-of-file while reading char by char.

    Ready to mark this lesson complete?Track your journey across the entire course.