Ramakrishna
Ramakrishna

Reputation: 99

Parentheses operator clarification

int a=10,b=20;
b = a+b-(a=b); 

In this expression why (a=b) is not first operation? if it is performs according to priority then it b has to get 20 itself. But b is getting 10 itself, why? Could any one clarify my doubt?

Upvotes: 0

Views: 92

Answers (1)

haccks
haccks

Reputation: 106012

This invokes undefined behavior. Anything could be happen. Note that it is sure here that (a=b) evaluates before the subtraction but it does not guarantee that the value of b get assigned to a just after the evaluation. a may get modified after the next sequence point (the ; of the statement here).

The Standard states that

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored.

Suggested reading: c-faq Question 3.8

Upvotes: 4

Related Questions