4 ms·
While I get the overall message I'm not sure last question is that unpredictable: I++ + ++I If postfix is executed first it returns 0 but prefix returns 2 and t
by paulftw 10y ago
While I get the overall message I'm not sure last question is that unpredictable:
I++ + ++I
If postfix is executed first it returns 0 but prefix returns 2 and the result is 2. If prefix is executed first it returns 1 and then postfix also returns 1, result of the addition is again 2.
What else is ambiguous about this snippet? I can't think of a parse tree that'd evaluate addition before any of the increments - that'd be a syntax error.
If the code were (++i + i++) there could be alternative interpretations, but again ++ requires lvalue...
- loup-vaillant 10y agoThe order of evaluation in such an expression is entirely unspecified. There are points in C programs that separate a "before" and an "after". The semicolon is a typical such point. There are others. Now between two adjacent points anything goes. In this particular example the evaluations and effects are allowed to be interleaved: pre = I + 1; // evaluating the pre-increment post = I; // evaluating the post-increment I++; // effect of pre-increment I++; // effect of post-increment return pre + post; // evaluating the sum Simply put, pre and post-increment are allowed to occur "simultaneously".
- mlvljr 10y agoThis is not (just) unspecified, but rather a classic UB.
- thick18cm 10y agowhat you are saying may be correct, but you are "overloading" the term "order of evaluation" to encompass atomicity (atomicity of operations like i++); is that kosher?
- loup-vaillant 10y agoIndeed I am. What I want to convey is, between 2 sequence points, C is actually non-strict. Any effect is like unsafeInterleaveIO from Haskell. Which is why I rarely mix up effects together or with a computation in a single instruction.