Reputation: 31
New to C. So I saw this line of code
system->word[system->index++] = system->next_char;
Is it equivalent to:
system->word[system->index] = system->next_char;
index++;
What is the precedence for post increment? Does it only increment index's value by 1 after all the operations on the line are done executing?
Upvotes: 0
Views: 1039
Reputation: 12668
Not.
system->word[system->index++] = system->next_char;
is equivalent to:
system->word[system->index] = system->next_char;
system->index++;
as index
is a field on a struct
pointed to by system
. In case you have also a scalar variable named index
you had had no errors but the wrong variable should have been incremented.
As a general rule, all the unary operators evaluate first on the right side of the variable, then the ones on the left (the right side operators have higher precedence thatn left side ones) and evaluate from closest to the operand, to farthest. So the operator closest to the operand evaluates first, then the one next in the right... so until there are no more right operators on the right, then the closest on the left side, and so on until there are no more left operators on the left.
So what happens if we have -x++
? The ++
is evaluated first to the value of x
and a post increment of the variable is schedule to occur, then the one on the left is evaluated, givin as result the value of x
before incrementing, changed of sign, and the variable x
gets its value incremented (only incremented, no sign change).
Another example: let's compute ++x->field
: first of all ->field
(the whole thing, the ->
arrow and the field name) is considered a right unary operator, given a pointer gets the value in field named field
fo structure pointed to by x
. This value is incremented (and stored again in x->field
) and the returned value is the final incremented one.
Another final example: let's compute *p++
. The value of p
is scheduled to be post incremented, but it's previous value is retained. Then the old value of p
is used to access the value pointed to by it, giving the original pointed to value. Finally p
is incremented and points to the value next to the value accessed.
Upvotes: 1
Reputation: 222724
Updating system->index
is defined as a side effect that is not sequenced (is not specified to come before or after) the other operations in the statement. The update may occur before, during, or after other operations.
The fact that it is not sequenced is irrelevant as long as it is not used elsewhere in the statement, because, if it is not used elsewhere, then nothing the statement does can be affected by when the update occurs. (Note that, even if the update to system->index
in memory is done before the value is used, the compiler is response for ensuring that the pre-update value is used.)
If the object being updated were used elsewhere in the statement in an unsequenced way (that is, no rule specifies which comes first, the update or the other use), then the behavior of the program would not be defined by the C standard.
This is not a consequence of precedence. Precedence determines the structure of how expressions are interpreted, not the sequencing of their operations.
Upvotes: 1