Welcome to Dream.In.Code
Become a C++ Expert!

Join 136,919 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,768 people online right now. Registration is fast and FREE... Join Now!




operator precedence

 
Reply to this topicStart new topic

operator precedence

amitdhar
9 Mar, 2008 - 01:31 AM
Post #1

New D.I.C Head
*

Joined: 1 Feb, 2008
Posts: 32



Thanked: 1 times
My Contributions
I am not getting the logic of the following oeperator precedence.

c= a++ + a++ + a; (where a is 10)
The output of c is coming 30, whereas according to my calculation it is coming 33.
What I calculate is
c = 10 + 11 + 12;
Can anybody explain the process how the expression is giving 30?
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Operator Precedence
9 Mar, 2008 - 01:52 AM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,495



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(amitdhar @ 9 Mar, 2008 - 02:31 AM) *

c= a++ + a++ + a; (where a is 10)
The output of c is coming 30, whereas according to my calculation it is coming 33.

CODE

Cygwin:/home/user/code/c >$./plus
30
Cygwin:/home/user/code/c >$cat plus.c
#include <stdio.h>

int main(void) {
  int c,a=10;

  c=a++ + a++ + a;

  printf("%d\n",c);

  return 0;
}

I get the same results as you.
User is online!Profile CardPM
+Quote Post

Bench
RE: Operator Precedence
9 Mar, 2008 - 02:50 AM
Post #3

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 626



Thanked: 16 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
its undefined behaviour, the result could be anything

http://www.research.att.com/~bs/bs_faq2.ht...valuation-order
User is offlineProfile CardPM
+Quote Post

TheMagnitude
RE: Operator Precedence
9 Mar, 2008 - 12:09 PM
Post #4

D.I.C Head
Group Icon

Joined: 12 Jan, 2008
Posts: 88


Dream Kudos: 125
My Contributions
doing a++, adds 1 to the variable a after the statement I thought? No, hmm... ok somethings fucked up with C++ here.

This post has been edited by TheMagnitude: 9 Mar, 2008 - 12:13 PM
User is offlineProfile CardPM
+Quote Post

MorphiusFaydal
RE: Operator Precedence
9 Mar, 2008 - 12:24 PM
Post #5

D.I.C Lover
Group Icon

Joined: 12 May, 2005
Posts: 1,112



Thanked: 9 times
Expert In: Hardware, Networking

My Contributions
I thought the same as TheMagnitude. I thought that the variable++ operatoor would add 1 to it after the operation, whereas ++variable, adds it before
User is online!Profile CardPM
+Quote Post

NickDMax
RE: Operator Precedence
9 Mar, 2008 - 03:29 PM
Post #6

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,859



Thanked: 50 times
Dream Kudos: 550
My Contributions
Well, as Bench pointed out, this is undefined behavior, but lets think about why.

Technically speaking the post-increment operator returns the current value of the variable, then increments the value. So, given a=10 then the expression:
c = a++ + a++ + a; should evaluate as, 10 + 11 + 12, the 10, because that if the current value of a, but a gets incremented, so the next a is 11, and then incremented, and finally a= 12, c = 33.

Some compilers may in fact return this value sometimes. The problem is that different compilers will evaluate the expression in different ways. For example it may be evaluated from right to left giving 11 + 10 + 10. Many compilers will not increment the value until after the expression has been evaluated, which yields, 10 + 10 + 10, and then a becoms 12. The point is, that there are no rules defining how the compiler must evaluate this type of expression.

So... to reiterate:
QUOTE(Bench @ 9 Mar, 2008 - 03:50 AM) *
its undefined behavior, the result could be anything

User is offlineProfile CardPM
+Quote Post

amitdhar
RE: Operator Precedence
15 Mar, 2008 - 07:55 PM
Post #7

New D.I.C Head
*

Joined: 1 Feb, 2008
Posts: 32



Thanked: 1 times
My Contributions
QUOTE(NickDMax @ 9 Mar, 2008 - 04:29 PM) *

Well, as Bench pointed out, this is undefined behavior, but lets think about why.

Technically speaking the post-increment operator returns the current value of the variable, then increments the value. So, given a=10 then the expression:
c = a++ + a++ + a; should evaluate as, 10 + 11 + 12, the 10, because that if the current value of a, but a gets incremented, so the next a is 11, and then incremented, and finally a= 12, c = 33.

Some compilers may in fact return this value sometimes. The problem is that different compilers will evaluate the expression in different ways. For example it may be evaluated from right to left giving 11 + 10 + 10. Many compilers will not increment the value until after the expression has been evaluated, which yields, 10 + 10 + 10, and then a becoms 12. The point is, that there are no rules defining how the compiler must evaluate this type of expression.

So... to reiterate:
QUOTE(Bench @ 9 Mar, 2008 - 03:50 AM) *
its undefined behavior, the result could be anything




A good explanation. Thanks. But shall we assume that this is a drawback in C++? If there is no specific rule then how can we write any expression logicaly?
User is offlineProfile CardPM
+Quote Post

Bench
RE: Operator Precedence
16 Mar, 2008 - 02:30 AM
Post #8

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 626



Thanked: 16 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(amitdhar @ 16 Mar, 2008 - 03:55 AM) *

A good explanation. Thanks. But shall we assume that this is a drawback in C++? If there is no specific rule then how can we write any expression logicaly?

There's nothing stopping you checking the compiler's documentation, and taking advantage of compiler-specific behaviour, but I'd question why you wanted to do it in the first place. Its better for everybody if you break it up into smaller steps across multiple lines, instead of trying to write ugly "clever" code which would otherwise make for difficult reading.

I believe the reason the C++ standard doesn't specify evaluation order, is simply because it places an unnecessary restriction upon compiler vendors in the way which compilers are written, and a standard defined evaluation order wouldn't add anything useful to the language anyway, as far as I can see (Since compilers which do differ in their implementations are just as capable as each another).
User is offlineProfile CardPM
+Quote Post

amitdhar
RE: Operator Precedence
16 Mar, 2008 - 08:35 PM
Post #9

New D.I.C Head
*

Joined: 1 Feb, 2008
Posts: 32



Thanked: 1 times
My Contributions
QUOTE(Bench @ 16 Mar, 2008 - 03:30 AM) *

QUOTE(amitdhar @ 16 Mar, 2008 - 03:55 AM) *

A good explanation. Thanks. But shall we assume that this is a drawback in C++? If there is no specific rule then how can we write any expression logicaly?

There's nothing stopping you checking the compiler's documentation, and taking advantage of compiler-specific behaviour, but I'd question why you wanted to do it in the first place. Its better for everybody if you break it up into smaller steps across multiple lines, instead of trying to write ugly "clever" code which would otherwise make for difficult reading.

I believe the reason the C++ standard doesn't specify evaluation order, is simply because it places an unnecessary restriction upon compiler vendors in the way which compilers are written, and a standard defined evaluation order wouldn't add anything useful to the language anyway, as far as I can see (Since compilers which do differ in their implementations are just as capable as each another).




Thanks a lot. I am satisfied with your logic.
take care
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 10:11PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month