I am beginner in C programming language, recently I have studied about getchar
function, which will accept a character from the console or from a file, display it immediately while typing and we need to press Enter key for proceeding.
It returns the unsigned char
that they read. If end-of-file or an error is encountered getchar()
functions return EOF.
My question is that, When it returns unsigned char
, then why its returned value is stored in int
variable?
Please help me.
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.