Ask your own question, for FREE!
Computer Science 8 Online
OpenStudy (anonymous):

explain output of int x = 4; x = x + x + x++; printf("%d\n",x); x = 4; printf("%d\n",x + x + x++); output of above code is 13 12

OpenStudy (asnaseer):

x++ means: first read the current value of x and then increment its value. so in the statement: x = x + x + x++ what happens is as follows: x = 4 + 4 + 4 = 12 then the "post increment" on x is applied (i.e. x++) resulting in x getting the value 13 In the second statement: printf("%d\n",x + x + x++); the same thing happens, except this time the result of "x + x + x++" is printed out first and then the current value of x is incremented - which in this case would mean x would end up with a value of 5.

OpenStudy (anonymous):

it doesn't work on #define p(x) printf("%d\n",x); int x = 4; x = x + x++ + x; p(x); x = 4; p(x + x++ + x); same output 13 12

OpenStudy (asnaseer):

what do you mean by it doesn't work?

OpenStudy (anonymous):

according to ur explanation first + is evaluated then increment but post increment has higher precedence than normal addition and assignment operator so it should be evaluated first

OpenStudy (asnaseer):

post increment always occurs AFTER the value has been read and processed

OpenStudy (anonymous):

thanks i get it.

OpenStudy (asnaseer):

yw :)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!