++ question for more inderstant

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • haloAE
    New Member
    • Nov 2015
    • 1

    #1

    ++ question for more inderstant

    #include<iostre am>
    using namespace std;
    int main()
    {

    int a = 5;
    int b = 5;
    int c = 5;
    int d = 5;
    int e = 5;
    int f = 5;
    a = a++ + a++;
    b = b++ + ++b;
    c = ++c + c++;
    d = ++d + ++d;
    e = e++;
    f = f + ++f;
    cout << a++ << endl;
    cout << ++b << endl;
    cout << ++a << endl;
    cout << c++ << endl;
    cout << ++d << endl;
    cout << d++ << endl;
    cout << ++c << endl;
    cout << ++f << endl;
    return 0;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Not good:

    Code:
    a = a++ + a++;
     b = b++ + ++b;
     c = ++c + c++;
     d = ++d + ++d;
    You are allowed to change the value of a variable only once in a statement.

    The compiler is free to calculate the value of the variables in any order. Maybe left-to-right. Maybe right-to-left.

    This code is called indeterminate. The same program compiled with different compilers can yield different results. Or maybe not.

    Don't do this.

    Comment

    Working...