ProgrammingHardIBM · Deloitte pattern

What happens? (Signed integer overflow.)

What happens? (Signed integer overflow.)
#include <limits.h>
int main(void) {
    int x = INT_MAX;
    int y = x + 1;
    printf("%d", y);
}
  1. A. Undefined behaviour: signed integer overflow is explicitly undefined in C, unlike unsigned overflow which wraps predictably
  2. B. It always triggers a compiler error
  3. C. It always prints 0
  4. D. It always wraps to INT_MIN (-2147483648), guaranteed by the standard

Correct answer: A. Undefined behaviour: signed integer overflow is explicitly undefined in C, unlike unsigned overflow which wraps predictably

Unlike unsigned integer arithmetic (which is guaranteed to wrap modulo 2^n), signed integer overflow is undefined behaviour per the C standard. In practice, many compilers with common optimisation settings do produce wraparound to INT_MIN, but the standard does not guarantee this, and aggressive optimizers may exploit the UB in surprising ways (e.g. assuming x+1 > x always).

More C Programming questions

Exams that ask this topic