views:

191

answers:

4
+15  A: 

Both produce undefined behavior. Neither is evaluated in any meaningful way. In both cases the variable i is used in illegal way.

And in the umpteenth time (referring to the title): Operator precedence does not dictate the order of evaluation. Operator precedence (and associativity) only defines the grouping between operators and their operands: which operand belongs to which operator. The actual temporal orderings in C (what happens first and what happens next), are defined by so called sequence points. Without sequence points, the ordering is undefined. Everything between two adjacent sequence points can be thought of as happening in any order or even simultaneously.

AndreyT
+4  A: 

No, that isn't defined by the standard. It has nothing to do with operator precedence, either. The undefined behavior is that you can't be sure in what order effects will be applied between sequence points, and there is none between the a[i] and the --i.

Chuck
+3  A: 

Operator precedence has no effect on this at all. It's completely unrelated.

This depends entirely on sequence points. With the increment and decrement operators, the variable must be updated to its new value by the end of the expression, so the next expression sees the new value -- but anything else in the same expression can see either the old or the new value, or (at least theoretically) even a value that's (for example) been partially updated, so it's not equal to either the old or the new value.

Jerry Coffin
Since it's "undefined behaviour" it can even crash.
caf
@caf: true in theory -- and invoking some undefined behavior (e.g., writing to an unitialized pointer) will often do exactly that, but it would be *quite* unusual for this particular case.
Jerry Coffin
+3  A: 

Even if it is defined, you'd have to be insane or competing in the International Obfuscated C Contest to use it. The results would be unreadable and unmaintainable.

DeadMG