CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: ++x + ++x

  1. #1
    Join Date
    Mar 2006
    Posts
    6

    Thumbs down ++x + ++x

    Hi, This is my first post, and hope not to be the last.

    Any one can tell me the result of this code, and the most importent: Why??

    the code is:
    =======================
    int x = 3;
    cout << ++x + ++x <<endl;
    cout << x;
    =======================

    please test it before replying

  2. #2
    Join Date
    Apr 2004
    Location
    In the back seat of New Horizons.
    Posts
    1,238

    Re: ++x + ++x

    Is this a homework assignment?
    Here are the rules, you must obey them or the gender bender will get you.

    And if you ever think of posting without code-tags, the evil monkey will come after you.

  3. #3
    Join Date
    Mar 2006
    Posts
    6

    Re: ++x + ++x

    Quote Originally Posted by YourSurrogateGod
    Is this a homework assignment?
    No its NOT!!

    I was studing for the next week exam, and this problem showed up.

    if you know any thing about it.. i'll be glad to hear it!!

  4. #4
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: ++x + ++x

    The code does not compile. Okay, no kidding

    Undefined behaviour... why? Because the C++ standard says so.

    Quote Originally Posted by C++ Standards Section 5 - Expressions para(4)
    Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified. Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. The requirements of this paragraph shall be met for each allowable ordering of the sub-expressions of a full-expression; otherwise the behavior is undefined.
    [Example:
    Code:
    i = v[i++]; // the behavior is undefined
    i = 7, i++, i++; // ‘i’ becomes 9
    i = ++i + 1; // the behavior is undefined
    i = i + 1; // the value of ’i’ is incremented
    —end example]
    Hope this helps. Regards.

  5. #5
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: ++x + ++x

    just check out in MSDN for Operator Precedence and Associativity and see how it will work.
    Code:
    int main()
    {
    		 int x=1;
    		 cout << ++x + ++x <<endl; //produce output 6
    		 return 0;
    //to run this program i used VC6.0 and it's perfectly valid
    }
    Thankyou
    Last edited by humptydumpty; March 31st, 2006 at 05:27 PM.

  6. #6
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

  7. #7
    Join Date
    Mar 2006
    Posts
    6

    Re: ++x + ++x

    Exterminator:
    why doesn't it compile? undefined behavior!!

    it did compile with me, but the result weren't as expected

    humptydumpty:
    I checked there and i know that it has some thing to do with the operators precedency... but there are nothing about such case.


    All:
    I'll be more specific:

    preincrement operator (++) has precedenc over the addition operator (+) but it compile from right to left,
    So, when we say :
    ++x + ++x
    this should be compiled and interpruted as:
    5 + 4
    then the result is:
    9
    but when you test it with C++ compiler : the result is 10
    this is the problem... and this is what i know.

    If i'm mistaken,, please correct me!

  8. #8
    Join Date
    Apr 2004
    Location
    In the back seat of New Horizons.
    Posts
    1,238

    Re: ++x + ++x

    Quote Originally Posted by Omarkth
    it did compile with me, but the result weren't as expected
    What compiler are you using and what were the unexpected results?
    Here are the rules, you must obey them or the gender bender will get you.

    And if you ever think of posting without code-tags, the evil monkey will come after you.

  9. #9
    Join Date
    Feb 2002
    Posts
    4,640

    Re: ++x + ++x

    Quote Originally Posted by Omarkth
    preincrement operator (++) has precedenc over the addition operator (+) but it compile from right to left,
    So, when we say :
    ++x + ++x
    this should be compiled and interpruted as:
    5 + 4
    then the result is:
    9
    but when you test it with C++ compiler : the result is 10
    this is the problem... and this is what i know.
    No. If I remember correctly, pre/post increment operators involved in function calls are undefined as to when they will be executed. For example:
    Code:
    int h = 2;
    f = g(++h) + i(++h);
    If 'h' is in int, and 'g' and 'i' are functions, what value is passed to 'g' and what value is passed to 'i'?

    I've run into bugs in code, for this exact problem. The code "worked" on one platform, but didn't "work" on another, because of the compiler's implementation of function calls and the pre-increment operator.

    Viggy

  10. #10
    Join Date
    Mar 2006
    Posts
    6

    Re: ++x + ++x

    I'm using VC6.0....

    Okey, MrViggy: can you explain why the result came out like that??

    if you wrote:
    x = 3;
    cout << ++x << ++x;
    OUTPUT: 54
    but if you make it:
    x=3;
    cout << ++x + ++x;
    OUTPUT: 10 // insted of 5+4=9 ???? WHY??

  11. #11
    Join Date
    Feb 2002
    Posts
    4,640

    Re: ++x + ++x

    5 + 5 == 10

    In other words, the compiler did the pre-increments first, then the addition.

    Viggy

  12. #12
    Join Date
    Mar 2006
    Posts
    6

    Re: ++x + ++x

    that's what i'm asking about :

    why 5 + 5???
    why not 5 + 4 ??

    x originally was 3 : first increment made it 4, second made it 5 ===> must be 4 + 5 , not 5 + 5

    u see what i'm talking about?

  13. #13
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: ++x + ++x

    Quote Originally Posted by Omarkth
    that's what i'm asking about :

    why 5 + 5???
    why not 5 + 4 ??

    x originally was 3 : first increment made it 4, second made it 5 ===> must be 4 + 5 , not 5 + 5

    u see what i'm talking about?
    Please re-read my post again. I was kidding when I said your code does not compile. In fact, it would not compile in the state you have provided it to us. But considering the fact that there is program that has such statement - the answer is what followed in the later lines (next to first one - the kidding one ).

    It is an undefined behaviour. Before posting any more please atleast read the quote that i provided and if still un-certain read the threads, the link to which I provided in my next post. That statement is free to give any result on any compiler. Regards.

  14. #14
    Join Date
    Feb 2002
    Posts
    4,640

    Re: ++x + ++x

    Because the order of those operations is undefined (as was stated earlier), and compiler dependent! Take this example:
    Code:
    #include <iostream>
    
    int SomeFuncA (int a)
    {
        std::cout << "In SomeFuncA...\n";
        return a;
    }
    
    int SomeFuncB (int b)
    {
        std::cout << "In SomeFuncB...\n";
        return b;
    }
    
    int main(/*int argc, char *argv[]*/)
    {
        int tmpVal = 3;
        std::cout << "Starting...\n";
        std::cout << SomeFuncA(++tmpVal) << SomeFuncB(++tmpVal) << std::endl;
    
        std::cout << "Now, just test the pre-inc...\n";
        tmpVal = 3;
        std::cout << ++tmpVal << " " << ++tmpVal << std::endl;
    
       return 0;
    }
    When I run this using Visual Studio 6, I get:
    Starting...
    In SomeFuncB...
    In SomeFuncA...
    54
    Now, just test the pre-inc...
    5 4
    The same exact program on Visual Studio 7 outputs:
    Starting...
    In SomeFuncB...
    In SomeFuncA...
    55
    Now, just test the pre-inc...
    5 5
    Two different compilers, two different results.

    Viggy

  15. #15
    Join Date
    Mar 2006
    Posts
    6

    Re: ++x + ++x

    thank you MrViggy, this is what i was really waiting for..

    thanx again

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured