Associative associativity in a specific prefix C and postfix increment and decrement

In C, associativity as such is used for increment, decrement, and assignment.

2. postfix ++ and -- 3. prefix ++ and -- 16. Direct assignment = 

A complete list can be found here . Wikipedia Operators in C

My question is when do we have

 int a, b; b = 1; a = b++; printf("%d", a); // a is equal to 1 b = 1; a = ++b; printf("%d", a); //a is equal to 2 

Why is it equal to 1 with b ++ when the postfix increment operator should happen before the direct assignment?

And why is the prefix increment operator different from the postfix when they are both before the assignment?

I am sure that I do not understand, which is very important when it comes to associativity.

+4
source share
3 answers

The postfix operator a++ will increment a and then return the original value, i.e. similar to this:

 { temp=a; a=a+1; return temp; } 

and the prefix ++a will return the new value ie

 { a=a+1; return a; } 

This is not related to operator priority.

(And associativity determines whether abc (ab)-c or a-(bc) is equal.)

+14
source

The priority and associativity of the operators do not tell you what happens before and what happens after. The priority / associativity of operators has nothing to do with this. In C, temporal relationships such as "before" or "after" are defined by so-called sequence points and only sequence points (and this is a completely separate story).

Operator precedence / associativity simply tells you which operands belong to which operators. For example, the expression a = b++ can be formally interpreted as (a = b)++ and as a = (b++) . The precedence / associativity of the operators in this case simply tells you that the last interpretation is correct, and the first is incorrect (i.e. ++ is applied to b , and not to the result a = b ).

This again does not mean that b should be increased first. The priority / associativity of the operators, again, relates to what happens "first" and what happens "next." It just tells you that the result of the b++ expression is assigned to a . By definition, the result of b++ (postfix increment) is the original value of b . This is why a will get the original value of b , which is 1. When the variable b increases, it will be completely irrelevant until a gets the assigned b original value. The compiler is allowed to evaluate this expression in any order and increment b at any time: everything goes while a somehow gets the original value of b (and no one really cares about how this "somehow" works inside).

For example, the compiler may evaluate a = b++ as the following sequence of elementary operations

 (1) a := b (2) b := b + 1 

or he can evaluate it as follows

 (1) b := b + 1 (2) a = b - 1 

Note that in the first case, b actually increases at the end, while in the second case, b increases in the first order. But in both cases, a receives the same correct value - the original value of b , which is how it should turn out.

But I must repeat that the above two examples are given here for illustrative purposes only. In fact, expressions like a = ++b and a = b++ do not have sequence points inside, which means that from your point of view, everything in these expressions happens simultaneously. There is no “before,” “after,” “first,” “next,” or “last.” Such expressions are “atomic” in a sense that they cannot be meaningfully decomposed into a sequence of smaller steps.

+5
source

As AndreyT has already pointed out, priority and associativity do not talk about the evaluation order. They only tell you about the group. For example, priority is what says a*b+c grouped as (a*b)+c instead of a*(b+c) . The compiler can freely evaluate a , b and c in any order that it considers appropriate with any of these expressions. Associativity tells you about grouping, when you have operators with the same priority, most often the same operators. For example, this suggests that abc equivalent to (ab)-c , not a-(bc) (otherwise, subtraction remains associative).

The evaluation order is determined by the points in the sequence. There is a sequence point at the end of the full expression (by the way). At the point of the sequence, all previous evaluations should have taken place, and no subsequent evaluation has yet taken place.

Having a look at your specific examples, in a=b++; the result is mainly determined by the definition of the post-increment itself. The post-increment gives the previous value of the variable, and sometimes the value of this variable will increase to the next point in the sequence. The pre-increment gives the value of the variable in increments. In any case, however, does this mean that the variable should be increased in any particular order relative to the destination. For example, in the pre-incremented example, the compiler can completely do something equivalent:

 temp = b+1; a = temp; b = b + 1; 

Similarly, in the version with subsequent increment, the variable can be increased before or after the assignment:

 a = b; b = b + 1; 

or

 temp = b; b = b + 1; a = temp; 

In any case, the value assigned to a must be the value of b before it is incremented.

+1
source

Source: https://habr.com/ru/post/1303378/


All Articles