Why must the variable used to hold getchar's return value be declared as int?
Solution 1:
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.
Solution 2:
The return type is int to accommodate for the special value EOF.
EOF is a macro which expands to an integer constant expression with type int and an implementation dependent negative value but is very commonly -1.
Solution 3:
Read this link: link
Here it is written that:
Do not convert the value returned by a character I/O function to char if that value will be compared to EOF. Once the return value of these functions has been converted to a char type, character values may be indistinguishable from EOF. Also, if sizeof(int) == sizeof(char), then the int used to capture the return value may be indistinguishable from EOF. See FIO35-C. Use feof() and ferror() to detect end-of-file and file errors when sizeof(int) == sizeof(char) for more details about when sizeof(int) == sizeof(char). See STR00-C. Represent characters using an appropriate type for more information on the proper use of character types.
This rule applies to the use of all character I/O functions.