-4

I am not able to understand the output of the following code can someone please help me out?

#define PRODUCT(x) (x*x)
main()
{
     int i=3,j,k;
     j=PRODUCT(i++);
     k=PRODUCT(++i);
     printf("\n %d%d",j,k);
}

For the above I got the output as:

9 and 49

I am not able to understand how 49 is coming and when I commented out

k=PRODUCT(++i);

I got the output as 25. Don't know what is going on in the program

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Rohit Saluja
  • 1,517
  • 2
  • 17
  • 25

1 Answers1

1

Macros do text substitution. PRODUCT(i++) expands to (i++*i++). Aside from being extremely surprising that there are now two modifications of i, this is also undefined behavior, because the two modifications are not sequenced.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157