I was reading about techniques to detect overflow in C . one of the examples to show incorrect solution to detect overflow in addition was this one :
/* Determine whether arguments can be added without overflow */
int tadd_ok(int x, int y) {
int sum = x+y;
return (sum-x == y) && (sum-y == x);
}
and it said it doesn't work because :
two’s-complement addition forms an abelian group, and so the expression (x+y)-x will evaluate to y regardless of whether or not the addition overflows, and that (x+y)-y will always evaluate to x
What does it exactly mean ? Does it mean that C compiler replace sum with x+y ?
To figure out what is it saying I even traced assembly code of the program, but there was no sign of replacement .
Update : The essence of my question is, does GCC evaluates an expression without calculating it ?
This is NOT a question about two's complement.
You can see a sample output in here .