I am running the following C code:
#define cube(x) (x*x*x)
void main()
{
int x=2,y;
y=cube(++x);
printf("%d %d",++x,y);
}
I am expecting result as
6,60
But it is giving different result. I think I have misconception on pre-processor. I think the code will be similar to
void main()
{
int x=2,y;
y=++x*++x*++x;
printf("%d %d",++x,y);
}
Please correct me if I am wrong.
I am interpretting the result to come as
3*4*5=60
but it is coming 125
You defined a macro, which works as a simple string substitution, so that the presented translation is correct.
However, the order of execution of subexpressions is undefined and they can be, for example, interleaved and this makes the undefined behaviour.