1

When I run the following code:

int x=2,y=3;
printf("x:%d, y:%d, x*y++:%d, x:%d, y:%d\n", x, y, x*y++, x, y);

x=2; y=3;
printf("x:%d, y:%d, x*++y:%d, x:%d, y:%d\n", x, y, x*++y, x, y);

I get the expected result (that is, what I'm personally expecting, not knowing much C):

x:2, y:3, x*y++:6, x:2, y:4
x:2, y:3, x*++y:8, x:2, y:4

But I also get a warning:

warning: unsequenced modification and access to 'y' [-Wunsequenced]

Why does the above produce a warning? My thought is that the printf function would return y=2 before the increment is done, and then y=4 afterwards. The results show that that is the case (at least using GCC on Mac), but why does this produce a warning?

samuelbrody1249
  • 4,379
  • 1
  • 15
  • 58

0 Answers0