Explain C Code

#define sro(X) (X*X)
main()
{

int i=3,j,k;
j=sro(i++);
k=sro(++i);
printf("n%d %d",j,k);
getch();
}

output is 9 49
can any one explain why we get second output as 49

Questions by lydia john

Showing Answers 1 - 1 of 1 Answers

prabhjots

  • Feb 6th, 2008
 



ya this is kind of weird problem dat u will always get with macros..
wat happens is wen u write j= sro(i++) it gets expanded to (i++*i++)
which assigns 9 to j. now since u r getting i++ twice it gets incremented twice so becomes 5 after executing j=sro(i++)..now with k= (++i*++i) u get it to b 49 becoz i was already 5 ..now since u r having ++i twice in a line "i" becomes 7 now..with results being 49 for this particular call..
i hope it clears ur doubt...

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions