really really simple program giving different outputs on 2 compilers&#

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 1289 Views - Last Post: 09 September 2011 - 04:31 PM Rate Topic: -----

#16 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: really really simple program giving different outputs on 2 compilers&#

Posted 09 September 2011 - 11:43 AM

as sepp2k has pointed out twice the problem is that this is undefined behavior. The languages does not impose a requirement on the order of evaluation of operands (for most operators).

so you have expr1 op expr2 the compiler may evaluate expr1 OR expr2 first. The operators that come to mind as having a defined order are && and ||.

note that there is no real *rule* stating that you "can only modify a value once between two sequence points" -- this is a consequence of the fact that side effects (and storing a value is a side effect) do not have to be complete until a sequence point. So any time you do something that has a side effect (changing a variable's stored value is a side effect, so it input/output) you need to think about sequence points where the operation is defined to be complete. Otherwise the compiler is still at liberty to do things differently (even within the same program in two different expressions with exactly the same form... if the compiler thinks it is better to evaluate one side before the other it will).

As for timetraveller - Alas you are mistaken. Although it sounds like you came to these conclusions though investigation which is good but relying upon "explaining" what you see when investigating one compiler can be dangerous. Make sure that you refer back to the standard (or as close a draft as you can find on the net) to ensure that your understanding correlates with what the language defines.

Your thoughts about the stack and calling conventions are important to function calls but play less of a roll in evaluating expressions.
Was This Post Helpful? 1
  • +
  • -

#17 timetraveller  Icon User is offline

  • New D.I.C Head

Reputation: -25
  • View blog
  • Posts: 10
  • Joined: 09-September 11

Re: really really simple program giving different outputs on 2 compilers&#

Posted 09 September 2011 - 11:54 AM

not one compiler sir.. i've studied this in detail
and the problem u showed i've even tried testing it b4 a month....
Posted Image
There r certain books that thought me better about compilers than the usual C programming books. Even Mr.Dennis Ritchie's seemed ambiguous so i had to stick with a more simple books to learn it the right way. For ex, let us C by yashwant kanetkar seemed perfect explaination. And the art of exploitation showed to a certain level how compilers work. Well, anyways i guess there is no room for me to talk more about this.. :D
Was This Post Helpful? -1
  • +
  • -

#18 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10669
  • View blog
  • Posts: 39,630
  • Joined: 27-December 08

Re: really really simple program giving different outputs on 2 compilers&#

Posted 09 September 2011 - 12:24 PM

I'm curious timetraveller- what compiler are you using? I looked up Let Us C on Amazon, and the fact that the author preaches Turbo C immediately disqualifies them from any semblance of credibility in my opinion. If this author cannot adhere to standards for C, I doubt his or her advice is sound on more advanced topics like compiler theory.
Was This Post Helpful? 0
  • +
  • -

#19 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1708
  • View blog
  • Posts: 3,283
  • Joined: 30-May 10

Re: really really simple program giving different outputs on 2 compilers&#

Posted 09 September 2011 - 01:09 PM

> But what i learned yet from books and teachers was that the parentheses changes the order of evaluation.
Books and teachers can be just as flawed as anything else (some more so than others).

If you really want to know what the language (and every compiler) is going to give you, then you need to consult the ISO standard(s).

This is the last public draft for C99 - but it is free (actual standards cost money). But the differences are only likely to be interesting to compiler writers.
http://www.open-std..../www/docs/n869/

Empirical "my teacher / book / compiler" says "foo" has only local significance. Sooner or later, a different "my teacher / book / compiler" will say something different. If the difference is written in the standard as "unspecified", "undefined" or "implementation specific", then neither would be right - just both would be equally wrong.

Either way, writing code which makes use of "unspecified", "undefined" or "implementation specific" behaviour is going to be in trouble at some point.

If as a programmer you set out to avoid such issues in the code you write, then you'll have a much simpler time moving from one compiler to another.
Was This Post Helpful? 4
  • +
  • -

#20 timetraveller  Icon User is offline

  • New D.I.C Head

Reputation: -25
  • View blog
  • Posts: 10
  • Joined: 09-September 11

Re: really really simple program giving different outputs on 2 compilers&#

Posted 09 September 2011 - 03:08 PM

the one in the screenie is visual studio 2010 (behind) and the one in front is code blocks. I modded it a bit to use notepad as my C program editor. Well, this kanetkar's book is for basics. Most foreign countries don't wanna take the turboC compiler coz its "OUTDATED" and i'm pretty sure it is!! Infact after 64 bit came turboC is ancient. But still kanetkar's book is really that gud. Amazon reviews won't convince u to get it. Thats y i suggest to just download the pdf.. google "let us C ext:pdf" and download it and read it. U'll understand what i mean by ambiguity in the ritchie book once u read this! The C programming language book is C standard.. true! But the concepts r written in a way that gives n impression that its more like a manual having all the codes and syntax without the logic behind the scenes. Try getting these two books.. let us C and the art of exploitation. They might not be the best books but if ur looking for basics then they r definitely good reads!! well just try it out and tell me if u like it! Its just a suggestion

Edit: I use code blocks to learn thru that book instead of tc. So, i had to use different fns like system("cls"); for clrscr,etc. It was a gud experience learning. And anyone (from my country) will tell that kanetkar book was like an adventure during the beginning of their programming life!

This post has been edited by timetraveller: 09 September 2011 - 03:11 PM

Was This Post Helpful? 0
  • +
  • -

#21 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4531
  • View blog
  • Posts: 7,903
  • Joined: 08-June 10

Re: really really simple program giving different outputs on 2 compilers&#

Posted 09 September 2011 - 03:44 PM

And now you're suggesting stealing copyrighted material.
Was This Post Helpful? 0
  • +
  • -

#22 timetraveller  Icon User is offline

  • New D.I.C Head

Reputation: -25
  • View blog
  • Posts: 10
  • Joined: 09-September 11

Re: really really simple program giving different outputs on 2 compilers&#

Posted 09 September 2011 - 04:08 PM

i'm pretty sure getting a pdf like that is legal. who r u planning to sue now... google??
Besides.. i only asked him to download it and check it out(i guess u buy books to check if they r worth buying!).Hey u know what.. its ok..! I get it! I'm just wasting my time here. anywayz good luck finding out the right stuff. BTW this program is stupid compared to the programs i've made. A leaving note - that kanetkar book is for BASICS and if u ever find it useful and want the next step try contacting me
Was This Post Helpful? 0
  • +
  • -

#23 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4531
  • View blog
  • Posts: 7,903
  • Joined: 08-June 10

Re: really really simple program giving different outputs on 2 compilers&#

Posted 09 September 2011 - 04:31 PM

It's most certainly not legal. It's exactly the same as downloading a movie or song or program.

I still haven't decided if you're trolling or not.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2