ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
/* 16-bit unsigned short, 32-bit int and unsigned */
unsigned short a = 3, b = 5;
printf("%d %u %d", a - b, 3u - 5u, (unsigned short)(a - b));
  1. A. 65534 4294967294 65534
  2. B. -2 -2 -2
  3. C. -2 4294967294 -2
  4. D. -2 4294967294 65534

Correct answer: D. -2 4294967294 65534

unsigned short is narrower than int, so both operands are promoted to (signed) int and a − b is simply −2. With genuine unsigned ints, 3u − 5u wraps modulo 2^32 to 4294967294. Casting −2 to unsigned short wraps modulo 2^16 to 65534, which is then promoted back to int for printf.

More C Programming questions

Exams that ask this topic