different outputs in gcc and tc

C

Casanova

int main()
{
int i=5;
printf("\n%d",++i + ++i + ++i + ++i + ++i );
}

Why does the above program print 40 as output in TC and 41 in GCC?
logically it appeals to me that 40 makes more sense since the integer
argument to printf should evaluate to 6+7+8+9+10 = 40

Thank You

Prashanth
http://prashblog.be
 
J

Jordan Abel

int main()
{
int i=5;
printf("\n%d",++i + ++i + ++i + ++i + ++i );
}

Why does the above program print 40 as output in TC and 41 in GCC?

Who knows? Who cares? If I were writing a compiler, it would print 30.
Or 42, on even-numbered days that fall on a full moon.
 
W

winfield301

it may be different.it depends on the translator(vc,tc.etc...);
'cuz it may use the old value first ,also can use the new value first.
i am not good at english.hope you could understand my explanation
 
R

Robert Gamble

Casanova said:
int main()
{
int i=5;
printf("\n%d",++i + ++i + ++i + ++i + ++i );
}

Why does the above program print 40 as output in TC and 41 in GCC?
logically it appeals to me that 40 makes more sense since the integer
argument to printf should evaluate to 6+7+8+9+10 = 40

Modifying the value of an object more than once without an intervening
sequence point is undefined behavior and anything may happen. For
additional insight see: http://c-faq.com/expr/evalorder2.html

Robert Gamble
 
M

Martin Ambuhl

Casanova said:
int main()
{
int i=5;
printf("\n%d",++i + ++i + ++i + ++i + ++i );
}

Why does the above program print 40 as output in TC and 41 in GCC?

Because that's the way TC & GCC decided to handle this piece of crap
masquerading as C. Be glad that they didn't decide to blow your machine up.
logically it appeals to me that 40 makes more sense since the integer
argument to printf should evaluate to 6+7+8+9+10 = 40

Why should you think this crap should make any sense at all? Please
check the damn FAQ before posting tired, worn-out guesses about what
something that is meaningless should mean.
 
M

Martin Ambuhl

it may be different.it depends on the translator(vc,tc.etc...);
'cuz it may use the old value first ,also can use the new value first.
i am not good at english.hope you could understand my explanation

There are some things in the universe for which no explanation suffices.
One is what a meaningless line of "code" means. Another is why some
people insist on posting with no context. Is gmail the new AOL?
 
D

Default User

Martin said:
Why should you think this crap should make any sense at all? Please
check the damn FAQ before posting tired, worn-out guesses about what
something that is meaningless should mean.

Or read the prior messages in the group. We've had very similar
questions to this several times recently.



Brian
 
C

CBFalconer

Default said:
Or read the prior messages in the group. We've had very similar
questions to this several times recently.

Interminably. Which is why it is a FAQ. I have to hope the
presence of the FAQ has cut off at least a few repetitions.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
J

John Bode

Casanova said:
int main()
{
int i=5;
printf("\n%d",++i + ++i + ++i + ++i + ++i );
}

Why does the above program print 40 as output in TC and 41 in GCC?
logically it appeals to me that 40 makes more sense since the integer
argument to printf should evaluate to 6+7+8+9+10 = 40

You have invoked undefined behavior; you cannot autoincrement the same
item more than once between sequence points. Since you have invoked
undefined behavior, *any* result is suspect, even one that you would
expect.

IOW, there is *no* correct result.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top