ProgrammingHardHCL · Tech Mahindra pattern

Assume 32-bit int, two's complement. What is printed?

Assume 32-bit int, two's complement. What is printed?
int x = ~0;
printf("%d %u", x, (unsigned int)x);
  1. A. 0 0
  2. B. -1 0
  3. C. 1 4294967295
  4. D. -1 4294967295

Correct answer: D. -1 4294967295

~0 flips every bit of 0, giving all-ones, which as a signed two's-complement int is -1. Reinterpreting that same bit pattern as unsigned gives the maximum unsigned value, 4294967295.

More C Programming questions

Exams that ask this topic