post and pre increment operator

This is a discussion on post and pre increment operator within the C Programming forums, part of the General Programming Boards category; I was learning sequence point and I get really confused by example given in wikipedia that "i=i++" is undefined, Can ...

1. post and pre increment operator

I was learning sequence point and I get really confused by example given in wikipedia that "i=i++" is undefined,

Can anybody explain me why it is so ? I can understand that "a[i] = i++" is undefined but I can not figure out why i = i++ is,

Second question is--
Is "a[i] = ++i;" is also undefined

2. Here's the rule:
Originally Posted by C99 Clause 6.5 Paragraph 2
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 read only to determine the value to be stored.
So, when we look at this statement:
Code:
`i = i++;`
We observe that there is a sequence point before and after this statement. We also notice that i++ modifies i, but the assignment to i also modifies i. Therefore, i has its stored value modified more than once by the evaluation of the expression in this statement, leading to undefined behaviour.

Originally Posted by _arjun
Second question is--
Is "a[i] = ++i;" is also undefined
What do you think?

3. Thanks you very much for such a clear answer .

Regarding second question I think it should be well defined, because I have read this at many places that "++i" will "first increment the value of i and then return the incremented value of i".

Therefore,
a[i] = ++i;
should write incremented value of i at incremented index of a, but the problem is that by going this logic "i = ++i;" also seems well defined, which is certainly not true according to the rule you have mentioned.
So please tell me where am I going wrong.

Thanks again

4. The C standard states that any expression (essentially: code between two sequence points, as described by laserlight) that modifies a variable twice invokes undefined behaviour.

How many times, in total, can the expression "a[i] = i++" modify i? How many times, in total, can the expression "i = ++i" modify i?

5. But that still doesn't get you off the hook as to whether a[i] is evaluated before OR after ++i is evaluated.

The compiler is free to do something like
int *answerGoesHere = &a[i];
++i;

Or it could evaluate answerGoesHere after the increment.
Either way, the code is broken.

6. Originally Posted by _arjun
Regarding second question I think it should be well defined, because I have read this at many places that "++i" will "first increment the value of i and then return the incremented value of i".

Therefore,
a[i] = ++i;
should write incremented value of i at incremented index of a, but the problem is that by going this logic "i = ++i;" also seems well defined, which is certainly not true according to the rule you have mentioned.
So please tell me where am I going wrong.
This is why I quoted the full paragraph of the rule
++i causes i to be read and modified, therefore by the second sentence, "the prior value shall be read only to determine the value to be stored". However, a[i] also causes the value of i to be read, not to determine the value to be stored into i, but to determine which element of a should be modified, thus violating this rule.

Salem gave a practical explanation of what is likely to happen, but note that this is not just implementation defined behaviour such that the compiler might choose between one of two options. Rather, the behaviour is undefined.

7. Thanks got it

8. Just confirming

is expression

i = i = 1;

also undefined because it also modifies the object more than once between sequence point

9. Yes.

Popular pages Recent additions