/* * signed int addition overflow test * exit(EXIT_FAILURE) if result overflows * print the correct value if everything is good. */ #include #include int add(int x, int y) { int result = x + y; if ((x > 0 && y > 0 && result < 0) || (x < 0 && y < 0 && result > 0)) { printf("addition overflow\n"); printf("%d\n", result); exit(EXIT_FAILURE); } return result; } int main() { // overflow case int x = 2147483647; int y = 1; printf("%u\n", add(x, y)); return 0; }