Here\'s some more weird macro behavior I was hoping somebody could shed light on:
#define MAX(a,b) (a>b?a:b)
void main(void)
{
int a = 3, b=4;
print
There are two reasons for the result that you're getting here:
A macro is nothing but code that is expanded and pasted when you compile. So your macro
MAX(a,b) (a>b?a:b)
becomes this
a++>b++?a++:b++
and your resulting functions is this:
printf("%d %d %d\n",a,b, a++>b++?a++:b++);
Now it should be clear why b is incremented twice: first in comparison, second when returning it. This is not undefined behaviour, it is well defined, just analyse the code and you'll see what exactly to expect. (it is predictable in a way)
The second problem here is that in C parameters are passed from last to first to the stack, hence all the incrementation will be done before you print the original values of a and b, even if they're listed first. Try this line of code and you'll understand what I mean:
int main(void)
{
int a = 3, b=4;
printf("%d %d %d\n",a,b, b++);
return 0;
}
The output will be 3 5 4.
I hope this explains the behaviour and will help you to predict the result.
BUT, the last point depends in your compiler