The output for this code:
#define RECIPROCAL_1(x) 1/(x)
#define RECIPROCAL_2(x) 1/x
main()
{
float x=8.0, y;
y = RECIPROCAL_1(x+10.0);
printf("1/%3.1f = %8.5f\n", x, y);
y = RECIPROCAL_2(x+10.0);
printf("1/%3.1f = %8.5f\n", x, y);
}
is output =
1/8.0 = 0.05556
1/8.0 = 10.12500
I can’t see how this works though. I appreciate any tips .
The macro substitutions are expanded like this:
becomes
and
becomes
Because
/has a higher precedence that+the values foryare different.This is an excellent example of why the discerning programmer only reaches for macros when no other solution is viable. And even then, those discerning programmers that feel compelled to use macros, will always use sufficient parenthesise as to make sure such pitfalls are avoided.