Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-28-2012, 05:11 AM   #1
Registered: Sep 2011
Posts: 33

Rep: Reputation: Disabled
ouput of the program varies with gcc version


Below is my sample code :


int main(int argc,char *argv[])
int i = 1 ;
printf("%d%d%d", i++,--i,i--);
return 0;


When I complied this code with version gcc version 3.4.6 output was -1 -1 1 (expected) as c++ comuptes from left to right.

However when I complied it with gcc version 4.6.2 . output was -1 0 1.

Please explain this beahviour .

Old 11-28-2012, 06:07 AM   #2
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Originally Posted by ankitm View Post
c++ comuptes from left to right.
No, it does not. The order of evaluation depends on operator precedence and associativity. So, for example,
1 + 2 + 3 + 4;
cout << a << b << c << d;
will be evaluated from left to right, but

a = b = c = d = 1;
      a += b += c;
will be evaluated from right to left.

Originally Posted by ankitm View Post
However when I complied it with gcc version 4.6.2 . output was -1 0 1.
You should also note that the C(++) standard does not specify order in which the function arguments will be evaluated, so this may very well depend on compiler and compile options such as optimization.

You may also examine the assembly code of your program to see what exactly happens there.

Edit: Also, read this

Last edited by millgates; 11-28-2012 at 06:17 AM.
Old 11-28-2012, 10:09 AM   #3
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
It is the order of evaluation of function arguments that is germane here. In this case, the ',' (comma) is not an operator, but simply a token separator. The C language says that the order of evaluation of function arguments is compiler dependent, therefore undefined.
This is a classic example of why you should not use such a construct.

--- rod.
1 members found this post helpful.
Old 11-28-2012, 11:14 AM   #4
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
Originally Posted by ankitm View Post
printf("%d%d%d", i++,--i,i--);
This is not just an order of operations issue.

C++ does not specify which sequence the parameters are computed in, so any change in the way you compile could change that sequence (optimization level, compiler version, platform, phase of the moon).

But the problem is more serious than that. Multiple changes to the same variable in one expression is expressly undefined behavior. You not only have no reasonable expection of what sequence i++, --i and i-- will occur in, you have no reasonable expectation that they will all even occur. The value of i when this expression completes is unpredictable.

If this were just an order of evaluation issue, the three values passed to printf would be unpredictable within a very narrow range, while the resulting value of i would be reliably reduced by a net 1. In actual C++ the values passed are unpredictable across a wider range and the resulting value of i is also unpredictable.

The standard goes even further in specifying that use of an undefined construct make the entire behavior of the whole program undefined. You can't rely on what three values get passed to printf. You can't rely on what value ends up in i. You also can't rely on anything else done by any part of your program. In my opinion, the first two of those are very real concerns in a case like this and the third is silly. But the standard says all three should be concerns.

Last edited by johnsfine; 11-28-2012 at 11:23 AM.
1 members found this post helpful.
Old 12-02-2012, 08:06 AM   #5
Senior Member
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,420

Rep: Reputation: 198Reputation: 198
It is silly and wrong.
Concentrate on good practice - not bad.
Life is too short.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Whats the difference with gcc 4.5.2-8ubuntu and gcc 4.5.2 generic version Arcosanti Ubuntu 4 08-20-2012 12:05 AM
[SOLVED] Upgraded GCC on Gentoo, different GCC version used when not root. fakie_flip Linux - Software 2 07-14-2012 12:11 PM
Regarding distribution + kernel version + gcc version + glib version. JCipriani Linux - General 8 04-19-2008 03:54 PM
redhat fedora gcc version (compiled for 2.4.20) doesn't match kernel version 2.4.22 start1000 Linux - Software 0 03-16-2004 09:17 PM
export CC=/usr/bin/gcc-3.2 - switch gcc version? ferreter Linux - Software 1 08-20-2003 01:07 AM

All times are GMT -5. The time now is 11:33 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration