x *= x++ + ++x;

This is a discussion on x *= x++ + ++x; within the C Programming forums, part of the General Programming Boards category; I want to understand clearly how pre and post increment ops work. Code: int main() { x = 1; x ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    1

    x *= x++ + ++x;

    I want to understand clearly how pre and post increment ops work.

    Code:
    int main()
    {
      x = 1;
      x *= x ++ + ++ x;
     printf("\n %d \n ",x);
    }
    The highlighted expression above can be seen as follows:

    x op1 x op2 op3 op4 x

    The C Operator Precedence Chart gives the following order of precedence:

    op4 followed by op2 followed by op3 followed op1 (?)
    ... which means the following happens:

    1. 2 is returned to the expression after op4 happens. Now, the value of x in memory becomes 2 (?)

    2. 2 is returned to the expression after op2 happens. Now the value of x in memory becomes 3 (?)

    3. 4 is returned to the expression after op3 happens. No change to x.

    4. op1 evaluates to 3 * 4 which means x becomes 12.

    But the output of the program is 8. Which means that in step 4, op1 still reads the value of x as 2 not 3 ? why?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Anything could happen, the expression is undefined because you modify x more than once between sequence points.
    My best code is written with the delete key.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    1
    when i run the code with tc3,the result is 9,why?

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Read the link!

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    the pre/post in/de-crement operators are just a matter of when the increment occurs. if it's pre, then then increment is performed before evaluation. if it's post, the increment is peformed after evaluation. run the following:

    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    int main()
    {
         char hello[] = "Hello World";
         char *p;
     
         //POST
         p = hello;
         cout << *p++ << endl;
         cout << *p << endl << endl;
     
         //PRE
         p = hello;
         cout << *++p << endl;
         cout << *p << endl;
         return 0;
    }
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by misplaced
    the pre/post in/de-crement operators are just a matter of when the increment occurs. if it's pre, then then increment is performed before evaluation. if it's post, the increment is peformed after evaluation.
    One more time! Everybody, all together now: "READ THE LINK!"

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    We get this question often enough that you would think everyone with more than 20 posts would know the answer by now.
    My best code is written with the delete key.

  9. #9
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Even worse than that, however, is the fact that some teachers quiz students on innane stuff like this -- and they're wrong!
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    http://www.eskimo.com/~scs/C-faq/q3.2.html
    Yeah I know Dave said it, and everyone else said it as well, so I just thought I'd say it again just incase some noobs simply don't get it.

    The last noob who tried to argue their way out of the pits of hell.
    Side_effect: (++x + y*x + y*x) != (y*x + ++x + y*x) ?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems storing values in varibles
    By stodd04 in forum C Programming
    Replies: 7
    Last Post: 02-08-2005, 10:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21