help me urgently to understand this in C

Status
Not open for further replies.

arunks

Hey here is the aks
int a=10,s;

s= ++a + a++ + --a + a--;


what would be the poutput for s??


according to me it should be 44 but when i run this code in C it displays 40...

why is it so... plz anybdy help me to understand that?

but if i use this

int a=10,s;
printf("%d",++a + a++ + --a + a--);

this code displays 44 as expected by me..

But according to me no difference should be there in above two codes..

Plz reply fast as i need this answer urgently
 

casanova

The Frozen Nova
++a = preincrement i.e; increment then assign
so if, b=++a then b=11, a=11
a++ =postincrement i.e; assign then increment
so if, b=a++ then b=10, a=11

s=++a + a++ + --a +a-- ;
this statement is executed as follows
s= ++a + a++ + --a + a--
s= 11 + 10 + 9 + 10 //increment operators are substituted first
s= 40

printf (%d",++a + a++ + --a + a--)
this statement is executed in left to right precedence
therefore ans=44
 

piyush gupta

Cyborg Agent
in first case it will be implemented from reverrse order

i thinks cassanova already explained u in details
more here

*en.allexperts.com/q/C-1040/Increment-operators.htm
 
Status
Not open for further replies.
Top Bottom