1. Home
  2. Computer Science MCQs
  3. C Programming
  4. Why does the standard library declare getchar to return…

Added 30 Nov -0001

Why does the standard library declare getchar to return int rather than char?

  1. Because the function reports read errors by returning a negative byte count
  2. So the out-of-band EOF value cannot be confused with any valid character code Correct answer
  3. Because a returned character may be a multi-byte sequence that is wider than 8 bits
  4. Because plain char is unsigned on most targets, which would otherwise discard the sign

Correct answer

B. So the out-of-band EOF value cannot be confused with any valid character code

16 views

MCQSEXAM

Explanation

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.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is B? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs