0
X=5,y=6 then what value of x and y will be used in int c= x+++y+++x,
Please tell me the values during execution of the equation not after the operation.
2 odpowiedzi
+ 3
x= 5
y = 6
int c = (x++) + (y++) + x
in above equation value for your variables are
int c = (5) + (6) + 6
int c = 17
+ 1
The result is Undefined behaviour. The expression x+++y+++x will indeed be translated to ((x++)+(y++))+x:
x+++y+++x // Original expression
(x++)+(y++)+x // Operator precedence (links: (1))
((x++)+(y++))+x // Operator associativity (links: (1), (2), (4))
But the order of the operands' evaluation is unspecified (see link (1) — first paragraphs and UB examples like: "n = ++i + i"). So there may be two possible outcomes of evaluation (and few more ways to achieve them):
1) ((x++)+(y++))+x => (5+(y++))+x => (5+6)+x => (5+6)+6 => 11+6 => 17
2) ((x++)+(y++))+x => ((x++)+(y++))+5 => ((x++)+6)+5 => (5+6)+5 => 11+5 => 16
Links:
(1) http://en.cppreference.com/w/cpp/language/eval_order
(2) http://en.cppreference.com/w/cpp/language/operator_precedence
(3) http://c-faq.com/expr/index.html
(4) https://en.wikipedia.org/wiki/Operator_associativity
(5) https://en.wikipedia.org/wiki/Sequence_point