Why must the variable used to hold getchar’s return value be declared as int?

Precisely because of that EOF-value. Because a char in a file may be any possible char value, including the null character that C-strings use for termination, getchar() must use a larger integer type to add an EOF-value.

It simply happens to use int for that purpose, but it could use any type with at least 9 bit.

Leave a Comment