0

Why the code answer is -3 ? Tell me.

Here is the code, its answer is -3. How and why? https://code.sololearn.com/cv5evERYq0bZ/?ref=app

1st May 2019, 2:49 PM
Vikash Kumar
Vikash Kumar - avatar
2 Antworten
+ 5
int a = -5; → Declares and initializes the variable a to -5 int k = (a++, ++a); Recall that Parenthesis has the highest precedence. Therefore, the expression inside the Parenthesis (a++, ++a) has to be evaluated first. a++, ++a The comma operator evaluates the first operand. It is a post increment. But there is no assignment involved (because the comma operator discards the result of the first operand). Increments the value of a. The value of a becomes -4 (-5 +1 =-4) The second operator is a pre increment. Notice that there is assignment involved here ( because the comma operator evaluates the second operand and returns this value to be assigned to variable k ). Since there is pre increment, previous value of a is incremented ( -4 +1 = -3). The value becomes -3. This value is assigned to k. Therefore the values of k and a are -3.
1st May 2019, 2:55 PM
Edwin
Edwin - avatar
+ 3
Easy! x was initialized -5. And in the printf statement x's value has been incremented 2 times. So x's value increases by 2 Then -5 + 2 = -3
1st May 2019, 2:52 PM
Seniru
Seniru - avatar