I have seen this one in this forum only..but i couldnt understand how come the answer is 10 for the below ???
int z,x=5,y=-10,a=4,b=2;
z = x++ - --y * b / a;
then output of z..???
|
Go4Expert Member
|
|
| 12Dec2012,22:19 | #2 |
|
i think the value of z is 10.
how:- step 1:- --y is evaluated which become -11. step 2:- (-11) will multiple with b which become -22 step 3:- this (-22) get devided by a now intermidiate result will be -5 step 4:- now expression become as z=x++ - -5; step 5 :- -5 become +5 step 6:- here the main game start the priority of post-increment is lower then assignement operater that's why first the value of x which is 5 is added with intermidiat result +5 which become 10 and this 10 get assigne into variable z . step 7:- variable x increment its value by 1 and become 6. |
|
Go4Expert Member
|
|
| 12Dec2012,22:27 | #3 |
|
But ++, -- have higher precedence than / ,*..???
|
|
Mentor
|
![]() |
| 13Dec2012,04:26 | #4 |
|
Don't forget that x++ means POSTincrement and ++x means PREincrement. That means before and after, and is usually before or after some execution point. So the equation in some senses (but definitely not all) is equivalent to:
Code:
--y; z = x - y * b / a; x++; Then z=5 - -11 * 2 / 4 =5 - -22/4 =5 - -5 =5 + 5 =10. Then x is incremented. |
|
Go4Expert Member
|
|
| 13Dec2012,08:28 | #5 |
|
Quote:
Originally Posted by xpi0t0s
|


