Let me expand this macro manually:
ABS(-3) == ((-3) < 0) ? -(-3) : (-3) ABS(-2) == ((-2) < 0) ? -(-2) : (-2) ABS(-2) - ABS(-3) == ((-2) < 0) ? -(-2) : (-2) - ((-3) < 0) ? -(-3) : (-3)
Now we have a problem: binary - has a higher priority than ?: So the above is parsed as:
((-2) < 0) ? -(-2) : ((-2) - ((-3) < 0) ? -(-3) : (-3))
Since (-2) < 0 true, we evaluate -(-2) . Then:
ABS( ABS(-2) - ABS(-3) ) == ((((-2) < 0) ? -(-2) : (-2) - ((-3) < 0) ? -(-3) : (-3)) < 0 ? -(((-2) < 0) ? -(-2) : (-2) - ((-3) < 0) ? -(-3) : (-3)) : (((-2) < 0) ? -(-2) : (-2) - ((-3) < 0) ? -(-3) : (-3))
We have already established that
((-2) < 0) ? -(-2) : (-2) - ((-3) < 0) ? -(-3) : (-3)
has a value of 2 , so the expression
(((-2) < 0) ? -(-2) : (-2) - ((-3) < 0) ? -(-3) : (-3))
estimated, and guess what? He rates a value of 2 .
What you need to do is change your macro as follows:
ABS(x) ( (x) < 0 ? -(x) : (x) )
Then everything expands as
ABS(-3) == ((-3) < 0 ? -(-3) : (-3)) ABS(-2) == ((-2) < 0 ? -(-2) : (-2)) ABS(-2) - ABS(-3) == ((-2) < 0 ? -(-2) : (-2)) - ((-3) < 0 ? -(-3) : (-3)) ABS(ABS(-2) - ABS(-3)) == (((-2) < 0 ? -(-2) : (-2)) - ((-3) < 0 ? -(-3) : (-3)) < 0 ? -(((-2) < 0 ? -(-2) : (-2)) - ((-3) < 0 ? -(-3) : (-3))) : ((-2) < 0 ? -(-2) : (-2)) - ((-3) < 0 ? -(-3) : (-3))
This time
(((-2) < 0 ? -(-2) : (-2)) - ((-3) < 0 ? -(-3) : (-3))
evaluated to -1 .
The rest should be clear here.
You should always wrap the body of macro expressions in an outer pair of parentheses for this reason.