c - Why does i=2+2*i++ give the wrong result? -


this question has answer here:

i felt such expression should invalid able compile , got answer 5. @ end felt if answer should 4 not 5.

int main(void) {   int i=1;    // how next line evaluated ie in sequence??    i=2+2*i++;   printf("%d",i);   return 0; } 

the output got 5. can not understand how should give value.

this undefined behaviour, since i modified more once between sequence points. instance, this compiler gives 4 answer, because puts increment after assignment. reasonable answer 6, if increment before assignment. but, you've found, compiler permitted make answer whatever wants, including 5.

see here more sequence points , undefined behaviour.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -