I had tried to debug but no luck I can\'t understand why the second printf() is call increment() thrice but the first one call twice as expected.
#include &l
This is a classic example of side effects in macros. Your max
macro examples out to this:
x > increment() ? x : increment()
Once the return value from increment() if bigger than x
the ternary operator will call increment()
twice, once to evaluate the condition and once to evaluate the false part (which is the second increment()
).
In this case you're best best is to max max_int
function:
int max_int(int a, int b)
{
return a > b ? a : b;
}
Calling this instead of MAX
will ensure your arguments are only ever evaluated once.