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

    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Postfix and prefix dry running


    how do you evaluate the following expression for x?
    compiler gives x=31

    int main(void)
    {
    int a=5,b=5,x=5;
    x=++a*b + a--;
    printf("a is %d b is %d x is %d\n",a,b,x);

    return 0;
    }
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,609
    Rep Power
    1945
    That is exactly why you shouldn't have that kind of expression.

    Sounds like it does the multiplication before increasin a.

    which compiler do you use?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by MrFujin
    That is exactly why you shouldn't have that kind of expression.

    Sounds like it does the multiplication before increasin a.

    which compiler do you use?
    I use pelles c, i am just trying to understand how operations work in c. Sometimes exams i get questions like this, i want to know the method to solve them.
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    http://c-faq.com/expr/index.html
    Learn about sequence points, and the rule that every object can only be modified ONCE between sequence points.

    > Sometimes exams i get questions like this
    Unless one of the answers is "undefined behaviour", the examiner is just as clueless.

    > I use pelles c
    It doesn't matter which compiler you use.
    Same code with many compilers
    If the code has undefined behaviour, then all bets are off.
    You get whatever the compiler writer decided should be the result of such nonsense.
    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
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0
    Thanx for the link, yea examiners are absurd at times
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    In Java class we were shown a similar expression and the instructor was bewildered why he got the answer that he did since it was different from what C would give him. I tried it in several different C-like languages and I examined the assembly code that C generates along with demonstrating how a compiler which evaluated expressions on a stack (such as the Java virtual machine does). I emailed my report to my instructor.

    I've posted a copy of that report: Evaluating (x + x * ++x).

    The bottom line is that while we have our left-to-right evaluation rules, what really matters is the code that the compiler generates. In Java, which uses a stack to evaluate expressions, the value of the variable when it's encountered going left to right is preserved, whereas in a language that just uses memory, each use of a variable accesses the value from its memory location which could have been changed "unexpectedly" by a later part of the expression.

    Also review the concept of sequence points.

    And whenever an instructor or examiner presents such a problem without the intent of teaching that you must avoid such expressions, object to such problems and questions citing what you have learned here.
    Last edited by dwise1_aol; December 8th, 2013 at 12:26 PM.

IMN logo majestic logo threadwatch logo seochat tools logo