Why does the standard library declare getchar to return int rather than char?
Correct answer
B. So the out-of-band EOF value cannot be confused with any valid character code
16 views
B. So the out-of-band EOF value cannot be confused with any valid character code
16 views
getchar returns either an unsigned char converted to int or the macro EOF, a negative value outside every character code, so storing the result in a char can make EOF indistinguishable from a valid byte. Assigning to int and comparing against EOF is the portable loop condition. The same reasoning applies to the int parameter of isalpha and toupper, which must not receive a negative value other than EOF.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is B? Ask here and get an explanation.