Exercise 1-7. Write a program to print the value of
EOF.
This is one of those exercises that looks trivial — one printf and you’re done — but it opens the door to one of the most important design decisions in the C standard library. Before you can really understand why Chapter 1’s input loops work, you need to know what EOF actually is.
What Is EOF?
EOF is a macro defined in <stdio.h>. It is not a character. There is no byte in your input stream with the value EOF. It is an integer constant, guaranteed by the C standard to be negative, that the standard library uses to signal “the stream has ended” (or an error occurred). On virtually every real system you will ever use, its value is -1.
Here is why that matters. getchar() must be able to return every possible byte value: 0 through 255. If it returned a char, there would be no room left for a distinct “end of input” signal — -1 would either be impossible to represent or would collide with the byte value 255 on systems where char is unsigned. The solution is that getchar() returns an int, giving it the full range it needs to hold both any byte value and a negative EOF sentinel. This is why the K&R idiom always uses int c = getchar(), never char c = getchar().
Exercise 1-6 asked you to verify that getchar() != EOF is 0 or 1. Now you can complete that picture: that expression is comparing the return value of getchar() to -1. Loop terminates when the stream ends, not because some magic EOF character arrived, but because the integer -1 was returned.
Solution
/* K&R Exercise 1-7: print the value of EOF */
/* Compile: gcc -ansi -Wall ex1-7.c -o ex1-7 */
#include <stdio.h>
int main(void)
{
printf("EOF = %d\n", EOF);
return 0;
}
Compile and Run
gcc -ansi -Wall ex1-7.c -o ex1-7
./ex1-7
Sample Output
EOF = -1
The value -1 is correct on every mainstream platform (Linux, macOS, Windows, BSDs). The C standard only guarantees that EOF is a negative int constant, so in principle a conforming implementation could choose -2 or any other negative value. In practice, -1 is universal. You can verify this yourself on any machine you have access to.
What This Exercise Teaches
EOFis an integer macro in<stdio.h>, not a character or byte value in the input stream — it is a signal returned by I/O functions.- The C standard only guarantees
EOFis a negativeint; the value -1 is a near-universal convention, not a requirement. getchar()returnsint, notchar, precisely because it must encode both any byte value (0–255) and the separate EOF signal in a single return value — assigning the result tocharis a classic beginner bug.- The loop condition
while ((c = getchar()) != EOF)is comparing anintto -1; understanding this makes the idiom click rather than feel like magic.
Set Up Your C Environment
To compile and run this solution, you need GCC installed. If you haven’t set up C on your machine yet:
- Complete C Development Environment Setup — start here if you’re new
- Install GCC on Windows 11
- Install GCC on macOS
- Install GCC on Ubuntu/Linux
- VS Code for C Programming — recommended editor
← Exercise 1-6 |
Chapter 1 Solutions |
Exercise 1-8 →
Book:
The C Programming Language, 2nd Ed — Kernighan & Ritchie