ProgrammingVery hardProduct company OA

What is printed?

What is printed?
/* 8-bit unsigned char, 32-bit int */
unsigned char c = 0x80;
printf("%d %d %d %d", c << 1, (unsigned char)(c << 1), ~c, c >> 8);
  1. A. 0 0 127 0
  2. B. 256 0 -129 0
  3. C. 256 0 127 0
  4. D. 256 256 -129 0

Correct answer: B. 256 0 -129 0

Before any arithmetic, an unsigned char is promoted to int. So c << 1 is the int 256 (no 8-bit overflow), and only the explicit cast back to unsigned char wraps it to 0. ~c complements all 32 bits of the int 128, giving −129, not 127. c >> 8 is 128 >> 8 = 0.

More C Programming questions

Exams that ask this topic