CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: postfix

  1. #1
    Join Date
    Sep 2008
    Posts
    1

    postfix

    Hi,

    Could someone please explain how the following ends up outputting 0?

    Code:
    #include <iostream>
    int main()
    {
    	int a=0; 
    	std::cout << a++ + a++ << std::endl;
    	return (0); 
    }
    I interpret this as follows.
    Compiler reads from right to left. So first the a++ on the right gets processed. Since its postfix, operator + gets 0. and then a is set to 1. Now the a++ on the left gets processed. a=1 is passed to +, and then a is incremented by 1 to 2. So the final result should be 1.

    cheers,

    pooya
    Last edited by pooya; September 20th, 2008 at 08:51 PM.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: postfix

    Just.....don't do that.

    Honestly I get suspicious enough of code that uses ++ inside a larger expression. Using it twice is just asking for trouble.....

    What's probably happening is that the compiler's logic for argument passing is separate from its logic for "all expressions with side-effects must be evaluated". As far as the function-call goes, it sees the same expression twice, says "Oh, I already know what that is", and just passes it. Sometime later it actually does the second increment.
    Last edited by Lindley; September 20th, 2008 at 09:37 PM.

  3. #3
    Join Date
    Aug 2007
    Posts
    858

    Re: postfix

    a + a is output, then a is incremented twice. If you want to test it, try

    Code:
    #include <iostream>
    
    int main( )
    {
      int x = 1;
      int y = 2;
    
      std::cout << x++ + y++; // output 3
      return 0;
    }
    And as Lindley said, that type of thing is pure evil. In no small part due to confusion like yours.
    Last edited by Speedo; September 20th, 2008 at 09:40 PM.

  4. #4
    Join Date
    Sep 2008
    Posts
    13

    Re: postfix

    Yeah, I have to agree, I wouldn't do that.

    For one, the a++ type expressions are generally used in for loops, for things like reading array values, etc..

    Code:
    for ( int i=0; i < 100; i++ )
    {
    std::cout << "Print me 100 times" << std::endl;
    }
    (Thats not even getting into the problems that can be created by using two of them on the same variable, in one operation.)

    Second, cout is for output, it's for printing results, etc, you should do your operations beforehand, and then print the results using cout.

    Code:
    int a=1;
    int b=1;
    int c=0;
    
    c= a+b;
    
    cout << c << endl;
    Last edited by Joe_Dert; September 21st, 2008 at 12:51 AM.

  5. #5
    Join Date
    Nov 2003
    Posts
    1,405

    Re: postfix

    Quote Originally Posted by pooya
    Compiler reads from right to left.
    Your assumption is wrong. In fact in C++ the order of evaluation of subexpressions within an expression is unspecified (except for a few operators that enforce a specific order).

    Quote Originally Posted by pooya
    So first the a++ on the right gets processed. Since its postfix, operator + gets 0. and then a is set to 1. Now the a++ on the left gets processed. a=1 is passed to +, and then a is incremented by 1 to 2. So the final result should be 1.
    Because the order is unspecified any postfix subexpression may be evaluated first but it doesn't matter which because the example is symmetric.

    You have

    a = 0;
    b = a++ + a++;

    When one of a++ is evaluated it's evaluated to 0. Because a is 0 when the other a++ is evaluated a stays 0. The two a++ evaluations have resulted in a being 0 so when then a+a is evaluated it becomes 0+0 = 0, which is assigned to b.

    Say you have this instead,

    a = 0;
    b = ++a + ++a;

    When one of ++a is evaluated it becomes 1. Because a is 1 when the other ++a is evaluated a becomes 2. The two ++a evaluations have resulted in a being 2 so when then a+a is evaluated it becomes 2+2 = 4, which is assigned to b.

    The above may seem strange but that's what the MS compiler does. I'm sure other compilers could come up with other results. The reason for this is that the evaluation order is unspecified. The MS compiler's strategy seems to be to first perform all increments on a and then use the resulting a value in the sum, something it's entitled to do according to C++.
    Last edited by _uj; September 21st, 2008 at 04:16 AM.

  6. #6
    Join Date
    Dec 2007
    Posts
    13

    Re: postfix

    you should know that the difference between the a++ and ++a.

    if a=0, then a++ equals 0, but ++a equals 1.

    So you should know what your problem is.





    -----------------------------------------------------
    codeuu,source code

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: postfix

    _uj's explanation is correct. If you are not convinced, then read Stroustrup's answer to the FAQ What's the value of i++ + i++?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

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