Click here to Skip to main content
15,908,166 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include <stdio.h>
int main(int argc, char** argv)
{
   int x = 3;
   printf("%d ";, x++ + ++x);
   return 1;
}</stdio.h>

Please, I need to know why the output will be 8 since I think it has to be 7?
Posted
Updated 29-Nov-10 3:30am
v2

thanks for ur reply Mamun
but i want to ask something
i know that x++ here won't increment 3 during printf but after it and save as x=4 then comes the addition operation this means till now we have 3+ then comes the ++x and this increment x firstly so x now will be 5 beacause it was 4 and increased by 1
finally we have 3+5=8

is this what u mean
thanks :)
 
Share this answer
 
Comments
Abdul Quader Mamun 29-Nov-10 9:59am    
NO, NO, 3+ 5= 8 its wrong, It is 4+4 =8. So How,


printf("%d ";, x++ + ++x); // in the line


First x++ that 3+1= 4.
Now x holds 4 in memory OK.
x++ + ++x,
4 + 4 =8 then
so,

printf("%d ";, x++ + ++x);

Will display 8.

then the following statement execute but it will not for output.

++x=3


Thanks,
Mamun
fjdiewornncalwe 29-Nov-10 10:23am    
Think of the syntax as being (x++) + (++x) and it will make sense. Order or precedence makes this 4+4 and not 3+4
Than you for your question,

C
int x = 3; //now three
printf("%d ";, x++ + ++x); // in the line
x++ means 3 + 1 = 4
++x this statement will decrease 1 from 4 but after addition.
so, x++ + ++x will be 8. 


Thanks,
Mamun
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900