EmbLogic's Blog

Pre increment operator

sir, I have some problem in Pre-increment operator for the following expression.

i=10;

printf(“%d\n”,++i  +  ++i  +  ++i);

I am getting un-expected output. it’s answer is 37.. but how??

3 Responses to Pre increment operator

  1. vineet says:

    Hey manoj…

    I think what is going here is that…… the statement will be start executing from right and it take first i then preincrement it, then it find an addition operator so it goes to next i , here it finds another preincrement so now value of i is 12 and now the rightmost 2 ++i get added so result here is 24 and value of i=12…

    now there is another addition so it goes to next i, and again preincrement so i=13 and 13+24=37

    Hope I am right, yet see what other person say about it :)

  2. vineet says:

    Hey manoj…

    I think what is going here is that…… the statement will be start executing from right and it take first i then preincrement it, then it find an addition operator so it goes to next i , here it finds another preincrement so now value of i is 12 and now the rightmost 2 ++i get added so result here is 24 and value of i=12…

    now there is another addition so it goes to next i, and again preincrement so i=13 and 13+24=37

    Hope I am right, yet see what other person say about it :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>