ProgrammingHardIBM · Deloitte pattern

Assume 32-bit int. What is printed?

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

Correct answer: D. 4294967295

Converting a negative int to unsigned int is well-defined: the standard requires adding 2^32 (modulo arithmetic) until the value fits in the unsigned range. -1's bit pattern is all 1s, which as an unsigned 32-bit value is 4294967295 (2^32 - 1).

More C Programming questions

Exams that ask this topic