K&R Exercise 1-6
getchar() and EOF
K&R Exercise 1-6: prove that getchar() != EOF is only 0 or 1 — type a character for 1, send end-of-file for 0.
14th May 2022
Last updated on 8th September 2026
Author: Kulwinder Krishan

K&R Exercise 1-6: show that getchar() != EOF is only ever 0 or 1. Type a character and it is 1. Send end-of-file and it is 0.
This is the first character-input exercise. Later programs loop on that same test; here you prove the test itself.
What the expression means
getchar() waits for one character from standard input and returns it as an int. EOF is a special value from <stdio.h> that means “no more input.”
getchar() != EOF compares those two. In C, a comparison is an integer: 1 when the character is not end-of-file, 0 when it is. It does not print the letter you typed.
Write the program
Create a file called getchar-and-eof.c:
#include <stdio.h>
int main(void)
{
printf("Value of expression = %d\n", getchar() != EOF);
return 0;
}
One printf. The interesting work is the argument: read a character, compare it to EOF, print the 0 or 1.
Compile and run
In Terminal, from the folder that holds getchar-and-eof.c:
gcc getchar-and-eof.c -o getchar-and-eof
./getchar-and-eof
Type a letter (for example t) and press Return. You should see:
Value of expression = 1
Run it again:
./getchar-and-eof
This time do not type a letter. Press Ctrl+D at the start of the line. That is end-of-file on macOS. You should see:
Value of expression = 0
If gcc is missing, finish Getting your macOS ready for C first.
How to send EOF
On macOS, Ctrl+D with an empty line is EOF. If a character is already waiting, Ctrl+D flushes that line instead — so press it before you type anything.
The program reads one character and exits. That is enough for this exercise. The next lesson prints the numeric value of EOF itself.
What you should take away
A comparison in C is a number. getchar() != EOF is 1 for a real character and 0 at end-of-file — the same test a copy loop will use, over and over.
Next
Earlier comments(2)
Nice blog here Also your site loads up fast What host are you using Can I get your affiliate link to your host I wish my web site loaded up as quickly as yours lol
- Kulwinder Krishan
Thanks! I use SiteGround as hosting provider. Affiliate link to SiteGround is below:
https://www.siteground.com/index.htm?afcode=a67654a7246ae5dd6cc524b37cba6a15
Leave a comment
Comments coming soon — set the NEXT_PUBLIC_GISCUS_* env vars after enabling GitHub Discussions and Giscus.