-1
        int a = 0;

        cout << a << a+1 << a << 1+a << a;
        // output is 01010 , ok fine i understand it! :-)

        cout << endl;

        cout << a << ++a << a << a++ << a;
        // output is 22202 ,
        // Plzzz help me how compiler interprets my this statement
        // i cant understand  :-(



            int x = 1;
            cout << ++x + ++x;
            // output is 6
            // how ??

Please if anyone could explain it to me how these outputs are coming :-) Thanks in Advance!

Abdur-Rehman M
  • 311
  • 1
  • 6
  • 1
    The last one is pretty easy. You have 1, autoincrement it two times (3), add it two times (3+3). Anyway, I would only use pre/postincremnt in the simplest situations, it is too risky. – SJuan76 Jun 15 '13 at 13:42

1 Answers1

1

It depends on the type of x. If x is a built-in type (e.g., int) then you have undefined behavior because you're modifying x twice without an intervening sequence point1.

If x is a user-defined type, then you have unspecified behavior.


1. Technically C++11 has change the terminology so "sequence point" is no longer used, bu the effect is the same.

Community
  • 1
  • 1
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111