Let\'s say I have a variable i that comes from external sources:
int i = get_i();
Assuming i is INT_MIN
Is negating INT_MIN undefined behaviour?
Yes, when INT_MIN < -INT_MAX - which is very common (2's complement). It is integer overflow.
int i = get_i();
#if INT_MIN < -INT_MAX
if (i == INT_MIN) {
fprintf(stderr, "Houston, we have a problem\n");
// Maybe return or exit here.
}
#endif
int j = -i;
Houston, we have a problem