473,386 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

different outputs in gcc and tc

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

Mar 19 '06 #1
9 1641
On 2006-03-19, Casanova <pr********@gmail.com> wrote:
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.
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

Mar 19 '06 #2
In article <11**********************@i39g2000cwa.googlegroups .com>,
Casanova <pr********@gmail.com> wrote:
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?


See Question 3.2 in http://c-faq.com/

Mar 19 '06 #3
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

Mar 19 '06 #4
Casanova wrote:
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

Mar 19 '06 #5
Casanova wrote:
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.
Mar 19 '06 #6
wi*********@gmail.com wrote:
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?
Mar 19 '06 #7
Martin Ambuhl wrote:
Casanova wrote:
int main()
{
int i=5;
printf("\n%d",++i + ++i + ++i + ++i + ++i ); 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.


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

Brian

--
If televison's a babysitter, the Internet is a drunk librarian who
won't shut up.
-- Dorothy Gambrell (http://catandgirl.com)
Mar 19 '06 #8
Default User wrote:
Martin Ambuhl wrote:
Casanova wrote:

int main()
{
int i=5;
printf("\n%d",++i + ++i + ++i + ++i + ++i ); 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.


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/>
Mar 19 '06 #9

Casanova wrote:
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.

Thank You

Prashanth
http://prashblog.be


Mar 19 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andrew Poulos | last post by:
I'm updating a db in which I have a column of Date/Time data type. The regional settings here for short date are: day / month / full year eg. 28/4/2005 It's easy enough for me to build a string...
3
by: Tao.Young | last post by:
Hi, I met with a very strange problem: the same function will generate different results, but when i uncomment Line1, it'll get the same results. anyone can tell me why? the code is...
13
by: Raja Kiran Kumar Reddy Sandireddy | last post by:
Hi, I observed a very strange phenomenon with my program outputs. I run my C-program on MSDOS environment and the same program on Sun-session. I get same output when the input is a small file...
2
by: john sun | last post by:
Hi, Dear gurus, In the web deployment project, there is one option called "Output Assemblies page". The first choice is "Merge all outputs to a single assembly", According to MSDN, "Merges...
3
by: pradeep | last post by:
Hello, Can any one tell me different functions or properties of javascript which are gives different outputs on different browsers ? Thanks in advance. Bye
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.