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

    Join Date
    Jul 2009
    Posts
    4
    Rep Power
    0

    Question 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:

    Thanks in advance! :D
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,444
    Rep Power
    1873
    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.

    Comments on this post

    • jwdonahue agrees : Yup. Didn't see you response before I posted mine.
    • imagineer agrees : Excellent link! n informative post
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    May 2004
    Posts
    3,417
    Rep Power
    888
    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.

    Comments on this post

    • 280Z28 agrees : Concise & useful
    • imagineer agrees : very useful
    I no longer wish to be associated with this site.
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,119
    Rep Power
    1803
    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:
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,322
    Rep Power
    2224
    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.

    Comments on this post

    • 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.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Posts
    4
    Rep Power
    0

    Talking 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!
  12. #7
  13. Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jun 2005
    Posts
    5,964
    Rep Power
    4853
    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.
    Write no code whose complexity leaves you wondering what the hell you did.
    Politically Incorrect DaWei on Pointers Grumpy on Exceptions

IMN logo majestic logo threadwatch logo seochat tools logo