int i=1;
printf(“hello world %d %d %d”,++j,j++,j);
outputis 3,1,3
y ?
output should be 3,1,1
int i=1;
printf(“hello world %d %d %d”,++j,j++,j);
outputis 3,1,3
y ?
output should be 3,1,1
EmbLogic Research & Competency Development Labs
Phone: +91 9818467776, 8527567776, 9650467776
Email: info@emblogic.com
Copyright © EmbLogic Embedded Technologies Pvt. Ltd.
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.
hi manoj,
i am having a query regarding your answer that why is it not updateing value of j++……
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
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..
its due to the property of pre increment operator. It updates the value of j in ++j and j
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…