...

View Full Version : Simple c program-please explain output.



hitesh.incept
09-13-2012, 11:37 AM
int main()
{
int a=7, t=0;
t=--a+--a+a+++a;
printf("%d",t);
}

output: 20

I could not understand why it prints '20' ? please explain it !

DELOCH
09-26-2012, 07:03 AM
a=7
t=(--a)+(--a)+(a++)+a;
=6 + 6 + 0 + 8 = 20

a++ becomes 0 in that situation for some reason which I do not understand

slickuser
10-03-2012, 05:01 AM
I believe the pre-decrement (or increment) will happen entirely before the final calculation. The post-increment (or decrement) will happen after the calculation. Thus:

a=7;
two decrements happen first and a=5 and the increment happens after the calculation;
so: 5 + 5 + 5 + 5 = 20.

The value of 'a' after the calculation is a=6.

If you instead did:
t=--a+--a+a+++a+++a;
the value of t=25 (5+5+5+5+5) and a=7.

rileyvanzeeland
10-20-2012, 07:50 AM
You are essentially doing this-


int main()
{
int a=7, t=0;
--a;
--a;

t=a;
t+=a;
t+=a;
t+=a;

a++;

printf("%d",t);
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum