EmbLogic's Blog

siddharth sir-problem on precedence

int i=1;
printf(“hello world %d %d %d”,++j,j++,j);
outputis 3,1,3
y ?
output should be 3,1,1

6 Responses to siddharth sir-problem on precedence

  1. manoj says:

    Hi,

    printf maintains a stack of values to be printed. It does not print each value separately, first it will complete it’s stack than starts printing.

    In case of ++j and j printf will store the last updated value in the stack. and for j++ it stores the current value in the stack and then increments it.

    now u will be able to solve this.

    • shalini singhal says:

      hi manoj,

      i am having a query regarding your answer that why is it not updateing value of j++……

      • manoj says:

        Hi shalini,

        It is updating… But it will update the values only at the end of the expression..

        for eg. : if u use : printf(p++ + p++) nd if p = 5
        Then result will be 10.
        But in next statement print the value of p . It would have become 7. It is updating but at the end of the expression.

        Thanks

      • manoj says:

        Hi,

        Other comment is related to the expression..
        But if u see, in case of printf(++j,j++,j) then u have to start from Right to left : j=1 (not stored in stack , waiting for the updated value), j++ = 1 (used).. j becomes 2 and then ++j = 3 .. LAst updated value is 3..

        So the output is 3,1,3

        Cheers..

  2. arjumand says:

    its due to the property of pre increment operator. It updates the value of j in ++j and j

  3. abhijeet.panwar says:

    post increment store the valu 1st and then pass pre increment and final the pass to other valu like(j pass 1 not to store,j++store 1and pass 2, ++j have 3 then final store 3 and pass to j and j also stor 3) all these in stack…

Leave a Reply to shalini singhal Cancel 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>