This program is a simple demonstration of why you always need to parenthesize macro parameters when you use them in expressions. It also illustrates a broader point of why you need to be very careful with macros in general.
I'd think that the output of F1 and F2 should be the same
Had F1
and F2
been functions, not macros, the output would indeed be the same. However, macros are simple textual substitutions, so the two expressions that you get with and without parenthesizing of X
are different:
F1:
((a-b > 0) ? a-b : -(a-b))
F2:
(a-b > 0) ? a-b : -a-b
Note that due to lack of parentheses F2
applies unary minus to a
, not to (a-b)
.
This remains a relatively simple use of macros. Imagine the problems you would get in case when X
is substituted for an expression with side effects, as in F2(a++, b--)
.