ProgrammingVery hardProduct company OA

Assume plain char is signed 8-bit, two's complement, and EOF is −1. What is printed?

Assume plain char is signed 8-bit, two's complement, and EOF is −1. What is printed?
char c = '\xff';
printf("%d %d %d %d", c == 0xFF, (unsigned char)c == 0xFF, c == EOF, c + 1);
  1. A. 1 1 0 256
  2. B. 0 1 0 0
  3. C. 1 1 1 0
  4. D. 0 1 1 0

Correct answer: D. 0 1 1 0

With a signed char, the byte 0xFF holds −1. In every comparison c is promoted to int with sign extension, so c == 0xFF compares −1 with 255 → 0, while c == EOF is −1 == −1 → 1. Converting to unsigned char first gives 255 → 1. c + 1 = 0. This is why getchar() must be stored in an int: a char cannot distinguish the valid byte 0xFF from EOF.

More C Programming questions

Exams that ask this topic