ProgrammingVery hard

A character loop reads bytes with char c = getchar() and tests while (c != EOF). Why can this loop be wrong?

  1. A. char may be unsigned, so EOF (-1) can never be stored, or a 0xFF byte equals EOF when char is signed
  2. B. getchar is slow, so the loop is too slow
  3. C. EOF is 0, so the loop stops at the first zero byte
  4. D. The loop never terminates because c is a pointer

Correct answer: A. char may be unsigned, so EOF (-1) can never be stored, or a 0xFF byte equals EOF when char is signed

EOF is a negative int, and storing it in a plain char is not reliable. The result of getchar must be stored in an int.

More C Programming questions

Exams that ask this topic