7 Replies - 195 Views - Last Post: 08 February 2013 - 09:25 AM Rate Topic: -----

#1 rishabhsharma  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 342
  • Joined: 26-March 09

Strange Expression Result

Posted 08 February 2013 - 08:01 AM

In C,
when I have, code lines as

int i,c ;
i = 5 ;
c = ++i + ++i ;



it produces output(value of c) as 14, and when I have code lines as

int i=5 ;
int c = ++i + ++i ;



it produces output(value of c) as 13. What's the difference between these two codes and what's really happening in there?

Is This A Good Question/Topic? 0
  • +

Replies To: Strange Expression Result

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5581
  • View blog
  • Posts: 11,934
  • Joined: 02-June 10

Re: Strange Expression Result

Posted 08 February 2013 - 08:18 AM

13 is the correct value.
No idea how you are seeing 14 in your debugger.
Where are you seeing 14? Console output? Message box? Breakpoint?

Let's see the complete code producing it - or a screenshot even showing this value in the debugger.
Was This Post Helpful? 0
  • +
  • -

#3 rishabhsharma  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 342
  • Joined: 26-March 09

Re: Strange Expression Result

Posted 08 February 2013 - 08:28 AM

a

This post has been edited by rishabhsharma: 08 February 2013 - 08:28 AM

Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2133
  • View blog
  • Posts: 3,267
  • Joined: 21-June 11

Re: Strange Expression Result

Posted 08 February 2013 - 08:29 AM

Mutating the same variable multiple times without a sequence point in between invokes undefined behavior in C. So the code may produce any output (or none at all) and the output may change for any reason. There's little point in trying to make sense of it (and any attempt to would be completely compiler-specific).

This post has been edited by sepp2k: 08 February 2013 - 08:30 AM

Was This Post Helpful? 3
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5581
  • View blog
  • Posts: 11,934
  • Joined: 02-June 10

Re: Strange Expression Result

Posted 08 February 2013 - 08:42 AM

No kidding?!?!?

Something as straight forward as this:
int i=5 ;
int c = ++i + ++i ;



gets you "undefined behavior" in C++?

I'm a C# guy with some C++ way back in my history. But I would have never expected this. This is the type of thing that I would have expected to have created so much uproar that it would have been fixed years ago.
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2133
  • View blog
  • Posts: 3,267
  • Joined: 21-June 11

Re: Strange Expression Result

Posted 08 February 2013 - 08:48 AM

View PosttlhIn`toq, on 08 February 2013 - 04:42 PM, said:

No kidding?!?!?

Something as straight forward as this:
int i=5 ;
int c = ++i + ++i ;



gets you "undefined behavior" in C++?


Yes, though to be fair, gcc will produce a warning about this and I imagine other modern compilers will do the same.

Quote

But I would have never expected this. This is the type of thing that I would have expected to have created so much uproar that it would have been fixed years ago.


It's not like being able to modify the same variable twice in the same expression would really be useful. I mean when would you ever write something like this in real code?
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5581
  • View blog
  • Posts: 11,934
  • Joined: 02-June 10

Re: Strange Expression Result

Posted 08 February 2013 - 09:10 AM

"Oh, when would anyone every need more than 5 megabytes of hard drive space" - That used to be the thinking as well.

Its not about whether or not someone can come up with an example scenario. SOmeone, somewhere will. And in their scenario it makes perfectly good sense to do it.

Of the top of my head I can see someone creating a formula for calculating a result.

            int alpha, bravo;
            alpha = 5;
            bravo = 10;
            int charlie = alpha > 0 ? ++alpha : 0 + bravo > 5 ? ++alpha : 0;


Was This Post Helpful? 0
  • +
  • -

#8 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2133
  • View blog
  • Posts: 3,267
  • Joined: 21-June 11

Re: Strange Expression Result

Posted 08 February 2013 - 09:25 AM

That won't invoke undefined behavior though because ? introduces a sequence point. I know I've said "within the same expression" in an earlier post - that wasn't quite accurate, sorry.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1