Re:So you think you know C?
by paulftw on Tuesday November 08, 2016 @06:18PM
(#12904852)
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...
[ Parent
| on HN ]
Re:So you think you know C?
by loup-vaillant on Tuesday November 08, 2016 @07:00PM
(#12905093)
The 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".what 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?
Re:So you think you know C?
by loup-vaillant on Thursday November 10, 2016 @05:37PM
(#12925196)
Indeed 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.