-2
int a = 10;

a = a++;

cout << a<<" ";

a++;

cout << a;

In microsoft Visual its output is 11 12

And in other compilers (like codechef and codeforces) its output is 10 11

This question's Output was asked in some mcq paper and the correct answer is 10 11 but can any1 also explain how

a = a++;
++a;
cout<<a;

produces output of 11 does a = a++ does't do anything?

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
  • 1
    https://en.cppreference.com/w/cpp/language/ub – Jesper Juhl Feb 01 '20 at 13:02
  • If you used the correct warning options, the code [wouldn't even compile](https://gcc.godbolt.org/z/6oBgM2) – Aykhan Hagverdili Feb 01 '20 at 13:15
  • @Ayxan That error message is a false positive warning treated as error. Since C++17 (you used C++2a in the link) the line has well-defined behavior. The warning is wrong with the `-std=c++2a` option. (Also side note: I would not recommend using `-Ofast` by default. It can break standard-conformance.) – walnut Feb 01 '20 at 14:01
  • @walnut It's the same with `-std=c++17` https://gcc.godbolt.org/z/bmsYN4 – Aykhan Hagverdili Feb 01 '20 at 14:04
  • @Ayxan Yes and in both cases it is a false positive. Only with `-std=c++14` and lower does the line actually have undefined behavior. – walnut Feb 01 '20 at 14:05
  • @walnut you're right about the `-Ofast`, but the way it doesn't conform the standard is acceptable imo. – Aykhan Hagverdili Feb 01 '20 at 14:05
  • @walnut it does compile with [Clang](https://gcc.godbolt.org/z/gJfi7x). Though, code like this shouldn't be used at all – Aykhan Hagverdili Feb 01 '20 at 14:10
  • There is a meaningful discussion of the topic in [What made i = i++ + 1; legal in C++17?](https://stackoverflow.com/questions/47702220/what-made-i-i-1-legal-in-c17) – anastaciu Feb 01 '20 at 14:21

1 Answers1

3

a = a++; is not a correct statement due to lack of sequence point or being unsequenced(C++11).

undefined behavior - there are no restrictions on the behavior of the program. Examples of undefined behavior are memory accesses outside of array bounds, signed integer overflow, null pointer dereference, more than one modifications of the same scalar in an expression without any intermediate sequence point (until C++11)that are unsequenced (since C++11), access to an object through a pointer of a different type, etc. Compilers are not required to diagnose undefined behavior (although many simple situations are diagnosed), and the compiled program is not required to do anything meaningful.

https://en.cppreference.com/w/cpp/language/ub

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • 1
    Note that since C++17 it has defined behavior. Also *sequence points* don't really exist anymore since C++11. Instead the correct terminology is that the assignment is *unsequenced* with the increment side effect. – walnut Feb 01 '20 at 13:58
  • Hi @walnut, the sequenced part is included in the highlighted bit of the citation, the fact that it is defined behaviour after C++17 is news to me, always learning. Thanks. – anastaciu Feb 01 '20 at 14:03