C C++ Errors Question:
Download Questions PDF

void main()
{
int i=5,y=3,z=2,ans;
clrscr();
printf("%d",++i + --z + i++ + --i * ++y);
i=5,y=3,z=2;
ans=++i + --z + i++ + --i * ++y;
printf("\n%d",ans);
getch();
}

Its output is 37 and 31....
Please explain me why its different
How it works?

Answer:

here in first statement

printf("%d",++i + --z + i++ + --i * ++y);

argument is : ++i + --z + i++ + --i * ++y.

first it will maintain stack operation like

++y (now fifth, it will execute and, y=4)(top 4)
--i (now fourth, it will execute and, i=6)
(because, last value of i were 7, once i++ were
executed, now --i will less one value in i)(top 3)
i++ (now third, it will execute and, i=6, it will
as it is, its value will for next stack value.)
(top 2)
--z (now second, it will execute and, z=1)(top 1)
++i (first it will execute and, i=6)(top 0)

now (++i + --z + i++ + --i * ++y)
(6+1+6+6*4)=(37)

it is output, say 37,for this printf("%d",++i + --z + i++
+ --i * ++y);

.................................................

Now Let me go with second statement, that is :

ans=++i + --z + i++ + --i * ++y;

here,
first of all ++y will contain the value of variable y=4
++y=4

after this, --i will less the value of variable i, say now
i = 4,
(--i=4),

after this, i++ will execute and, it will not increase the
value of variable i, right now, so value of i, say now i =
4, as it is.

after this, --z will less the value of variable z, say now
z = 1,
(--z=1)

now, ++i will increase the value of variable i.
Say i = 5.
.......................................................

now value of valiable i in memory is 5.
ans=5+1+5+5*4
ans=5+1+5+20
ans=31..........,

Download C C++ Errors Interview Questions And Answers PDF

Previous QuestionNext Question
void main()
{
int i=7;
printf("N= %*d",i,i);
}
What is syntax error?