3 ms·
You cannot check for overflow like this, because you're causing it. There are other ways to do this without adding first: #include <limits.h> int safe_add
by coliveira 3y ago
You cannot check for overflow like this, because you're causing it. There are other ways to do this without adding first:
#include <limits.h>
int safe_add(int a, int b) {
if (a > 0 && b > INT_MAX - a) {
/* deal with overflow... */
} else if (a < 0 && b < INT_MIN - a) {
/* deal underflow.. */
}
return a + b;
}