int i=-1;
int a=65;
int b=a*i + ++i;
What is the value of b? Here associativity of =,+ is left to right and associativity of *,prefix increment (++) is right to left.
So What order of evaluation should I consider for int b=a*i + ++i;
left to right? right to left? Why?
Do not think about assosiativity. Think about order of evaluation and unfortunately it is undefined in this case.
In the expression
int b=a*i + ++i;
you are modifying i
as well as using it in the same expression which invokes undefined behavior.
C99 states that:
Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored.
Further I would suggest you to read c-faq: Q 3.8.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With