ProgrammingVery hardProduct company OA

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

What is printed? Assume int is 32-bit, two's complement.
int x = INT_MAX;
int y = x + 1;
printf("%d", y);
  1. A. 2147483648, guaranteed by the standard
  2. B. 0, guaranteed by the standard
  3. C. Compilation error: constant overflow detected
  4. D. Undefined behavior (signed overflow), though INT_MIN is commonly observed in practice

Correct answer: D. Undefined behavior (signed overflow), though INT_MIN is commonly observed in practice

Signed integer overflow is undefined behavior in C (unlike unsigned, which wraps modulo 2^n by definition). Many compilers emit wrap-around two's-complement arithmetic in practice (giving INT_MIN), but optimizers may also assume overflow never happens and transform the code in surprising ways, so it is not guaranteed.

More C Programming questions

Exams that ask this topic