ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    int a = 2147483647;
    printf("%u", (unsigned)a + 1u);
}
  1. A. 2147483647
  2. B. 2147483648
  3. C. 0
  4. D. 4294967295

Correct answer: B. 2147483648

The conversion keeps the value 2147483647, and adding 1u gives 2147483648, which fits in unsigned int. Adding 1 as a signed int would overflow, which is undefined, so the unsigned form is the one that is well defined.

More C Programming questions

Exams that ask this topic