Expected Result for This Code

int a=5;
printf("%d %d %d\n",a++,a++,++a);

Output to Gcc: 7 6 8

Can someone explain the answer. I apologize if this question is repeated, but I could not find it.

Thank!!

+5
source share
1 answer

The behavior is undefined because there are no sequence points between increment operators .

Explaining why the code does what it does is a meaningless exercise. You should not write code with undefined behavior, even if it works for you.

To answer the question raised in the comments: it is true that the comma operator

+19
source

All Articles