EmbLogic's Blog

facing problem with i++ & ++i

#include<stdio.h>

main()
{

int i=0;                                                                // value of i assigned
printf(“the value of i is %d\n”,i);                       // value of i printed
printf(“the ++i %d   ++i %d \n”,++i ,++i);      // prefix operations are
printf(“the i++ %d   i++ %d \n”,i++ ,i++);      // postfix operations are

return 0;

}  /*  predicted results
*  the value of i is 0
*  the ++i 2   ++i 2
*/ the i++ 2   i++ 3

BUT OUT PUT IS

/*     the value of i is 0
*      the ++i 2   ++i 2
*/      the i++ 3   i++ 2

CAN ANY ONE TELL ME THAT WHY IS “PRINTF” NOT SHOWING VALUE IN SEQUENCE RESPECTIVELY

One Response to facing problem with i++ & ++i

  1. Manoj says:

    hi..

    printf uses a stack internally. the rightmost value is stored very firstly. In the last printf statement value of i was 2 initially. so , first 2 was inserted into the the stack and than it was incremented. then 3 was inserted into the stack, and then printing starts from top of the stack, and 3,2 is being printed…
    ..
    ++i stores the final updated value of the i into the stack at every place.

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>