ProgrammingHardIBM · Deloitte pattern

What is printed?

What is printed?
int x = 255;
unsigned char y = (unsigned char)(x << 1);
printf("%d", y);
  1. A. 510
  2. B. 254
  3. C. 0
  4. D. 256

Correct answer: B. 254

x<<1 as an int is 510 (0x1FE). Truncating to unsigned char keeps only the low 8 bits: 0x1FE & 0xFF = 0xFE = 254.

More C Programming questions

Exams that ask this topic