ProgrammingHardWipro · Cognizant pattern

What is printed? (Assume int is 32 bits.)

What is printed? (Assume int is 32 bits.)
int a = -1;
unsigned int b = (unsigned int)a;
printf("%u", b);
  1. A. 4294967295
  2. B. -1
  3. C. 0
  4. D. 2147483647

Correct answer: A. 4294967295

-1 in two's complement 32-bit representation is all 1-bits; reinterpreting that same bit pattern as unsigned int gives the maximum unsigned 32-bit value, 4294967295 (UINT_MAX) — this conversion is well-defined (unlike signed overflow), per the standard's modulo-2^n rule for signed-to-unsigned conversion.

More C Programming questions

Exams that ask this topic