P
pete
pete said:J. J. Farrell said:It may or may not be true;
the "as if" rule says so. With anything that
deserves the name of "C compiler" in the case that the OP was asking
about, it is not true.
Indeed, though it's clear from the OP's posting that he was asking
about the simple case.
It's clear from
"I was told that in case 3) "i" is accessed once and modified once;
in case 4) "i" is accessed twice and modified once."
that what he was being told,
was taking the non simple case into account.
/* BEGIN new.c ouput */
After: array[0] = 0; array[1] = 7; counter = 0; ++i;
i is 7
array[0] is 1
array[1] is 7
After: array[0] = 0; array[1] = 7; counter = 0; i = i + 1;
i is 0
array[0] is 0
array[1] is 1
/* END new.c ouput */
In his posting, the OP defined that the end value of i is identical in
all four cases,
so your example is not relevant to the question in hand.
In the example I that gave
the value of i was 1, prior to ++i, and
the value of i was 1, prior to i = i + 1.
I meant to say that the value of i was 0,
prior to the assignment operation.
Anyway,
the comment about the difference bewteen case 3) and case 4),
was about the difference in semantics
between simple assignment and compound assignment,
and was very nearly a quote from the standard.
N869
6.5.16.2 Compound assignment
Semantics
[#3] A compound assignment of the form E1 op= E2 differs
from the simple assignment expression E1 = E1 op (E2) only
in that the lvalue E1 is evaluated only once.