### Thread: Using pre & post increment with assignment statements!

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2009
Posts
4
Rep Power
0

#### Using pre & post increment with assignment statements!

how can we compute the output of pre & post increment operators with assignments statements? i used to think that we have to start computing from the right....but this kind of code always gives me the worng answer:

Code:

int a=10,b;
b=a++ + ++a + a++;
cout<<b;

Output = 33

Where am I going wrong? Pls explain how is this statement working... :chomp:

2. a) the code is broken, so there never can be a meaningful answer to begin with.

http://c-faq.com/expr/index.html
Learn all about sequence points, and how you're only supposed to modify each object at most once between them.

> b=a++ + ++a + a++;
This tries to modify a three times, so you're S-O-L

b) any book / tutor who tells you otherwise isn't worth the space they occupy. Ignore them.

• jwdonahue agrees : Yup. Didn't see you response before I posted mine.
• imagineer agrees : Excellent link! n informative post
3. No Profile Picture
Contributing User
Devshed Loyal (3000 - 3499 posts)

Join Date
May 2004
Posts
3,417
Rep Power
887
It doesn't work. a++ + ++a + a++ is undefined. How your particular compiler handles that or not is entirely up to your compiler writer. Never write code like that.

http://www.cppreference.com/wiki/operator_precedence

The problem is that the order of evaluation and the order of side effects in an expression are not the same. a++ is basically just a+1 but when that value will be stored is not defined and hence whether any of the increment operators in your expression operates on 10, 11, 13 or 22 is anybody's guess.

• 280Z28 agrees : Concise & useful
• imagineer agrees : very useful
4. Such code only serves as a lesson as to why you should not write such code, so there is no reason in the world to bend your brain to try to understand or predict it, and as already pointed out such effort is futile.

When do you think that such code will be useful? :rolleyes:
5. You're just trying to get us to do your homework for you.

The purpose of the assignment is to demonstrate to you that you must not do such a stupid thing. As the others have told you, the internals of how a particular compiler must generate the code to evaluate that expression is not defined by any standard, so it will vary from compiler to compiler. In short, it is undefined so you cannot predict what you're going to get. Read salem's signature about undefined behavior and heed it well.

But if I may mention, the example you were given is not very good. Here's a better one (please also note the use of code tags, especially since you must learn to use them if you're going to stick around here for very long):
Code:
```    x = 3;
y = x + x * ++x;
printf("x + x * ++x = %d\n", y);```
As far as we had worked with it (this was presented in a Java lecture), that expression evaluates to either 15 or 20, depending on the programming language and compiler and hardware platform. Java, JavaScript, and a Palm-based C evaluated it on a stack and all came up with 15 ( 3 + 3 * 4 ). But C, perl, and AWK all worked directly with the memory location containing the value of x, so since the pre-increment was done first, that changed the value of x for the rest of the evaluation and so they came up with 20 ( 4 + 4 * 4 ).

It's undefined, so don't do it.

• imagineer agrees : Very informative & to-the-point, also the longest explanation to my question! Thanks for the time! :)
Last edited by dwise1_aol; July 13th, 2009 at 05:18 PM.
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2009
Posts
4
Rep Power
0

#### Thank You all

Thanks a lot!!
i must say u guys are good!! i was trying to solve this for a long time!

@ thewise1_aol,
i will use code tags in the future, sorry.
also this wasnt my homework, believe me...actually 2 yrs back(in school) my Comp Sc teacher did a problem like this (and then i never encountered anything like this: must have served as a clue)...and i worked it on the comp to see it was wrong! now in college i tried to understand it by brute-force, i saw that the evaluate-from-right-rule worked fine with printf statements but failed with assignment statement! i also asked my teacher this, searched the forum but no luck!...sorry for the misunderstanding! and Thanks a lot!
7. evaluate-from-right-rule
You need to read your rule book again. That would be the standard. Twelve skadillion teachers and practitioners have never laid eyes on it.