Sign in to follow this  
arithma

Problem with understanding Operators

Recommended Posts

My C++ instructor has recently been explaining the unary prefix and postfix operators (mainly ++ and --) It shows out that: ++a + ++a is equivalent to (++a, ++a, a + a) so if a = 1 it returns 6 I tried it on Borland and VC++6.0 and it worked as he said However I thought it would return 5, as in it adds one to a, (a=2), it adds one to the next a (a=3) so the expression will be 5 ++a + ++a ++1 + ++a 2 + ++a 2 + ++2 2 + 3 5 Am desperate, and I know it has to do with precedence

Share this post


Link to post
Share on other sites
Quote:
Original post by arithma
Am desperate, and I know it has to do with precedence

Speaking strictly from an operator precedence point of view, pre-increment has higher precedence than addition, so both increments will be done before the addition. Where things get really weird is with post-increment: what are the values of a and b after the following statements?
int b, a = 1;
b = a++ + a++;


This is why some languages don't have increment operators, like Python.

Share this post


Link to post
Share on other sites
Ooops,
I didn't see that the same person was posting as I rushed into viewing the reply

However, can I conclude that the standard doesnot impose the order-of-precedence point-of-view?

Share this post


Link to post
Share on other sites
Quote:
Original post by arithma
Ooops,
I didn't see that the same person was posting as I rushed into viewing the reply

However, can I conclude that the standard doesnot impose the order-of-precedence point-of-view?


Since the + operator is not a sequence point, the operands could be evaluated in any order.

Share this post


Link to post
Share on other sites
Quote:
Original post by arithma
However, can I conclude that the standard doesnot impose the order-of-precedence point-of-view?

It has nothing to do with order of operations. As Oluseyi has stated, multiple modifications of a single variable in between sequence points is undefined. It's not supported by the standard, and is compiler-implementation defined.

Read more about it here.

Share this post


Link to post
Share on other sites
realize that the problem is only because the ++ operator is used on the SAME value twice between sequence points ... the following is 100% defined:

int a = 2;
int b = 3;

++a + ++b;

the above will equal 7, as guaranteed by the standard.

Share this post


Link to post
Share on other sites
Of course it doesn't guarantee if a is incremented first or b is incremented first (as might matter if they were of class type rather than int type).

Share this post


Link to post
Share on other sites
Something that hasn't been mentioned thus far is that multiple increment operators within expressions is bad programming practice -- even obfuscation to some people. Your code is usually more easily read when you increment in a separate statement.

Share this post


Link to post
Share on other sites
Mmm. There are well know idioms that involve incrementing multiple things in the same statement. ex:

while (*src++ = *dest++);

Though this is more of a C-ism, it's a standard C-ism; a competent C programmer should be able to recognize it.

Share this post


Link to post
Share on other sites
Eh, first time I've ever seen anything like that. Mind mentioning the source?

It might take a while, especially for a beginner, to figure out what that actually does...copies from one portion of memory (a null terminated string?) to another until it reaches a null/0 slot, is how I interpreted it...

Share this post


Link to post
Share on other sites
Basic and common, but pointless in C++ and apparently deprecated in C.

In any case, I'm sure we all agree that for someone who's learning C++, simplicity and/or transparency are imperative.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this