1

Before voting to close this as a duplicate, be aware that i = i++ has well defined behavior in C++17. See the comments and answers.

Why it is become 6 is it not supposed to be 5? in another ide the output is 5 but in visual studio it is become 6

#include<stdio.h>

int main(){

   int i = 5;
   i = i++;

   printf("%i", i);


}
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • 2
    Additional note: For such a things blame the compiler not the IDE xaxa – Ivan Kaloyanov Jan 19 '20 at 17:53
  • 6
    "... is it not supposed to be 5?" No, it is undefined behavior. It could be [puppies](https://stackoverflow.com/questions/56979248/why-does-this-simple-program-result-in-puppies-puppies-puppies-to-the-console). – Eljay Jan 19 '20 at 18:01
  • It is Undefined Behavior. Don't do it. – Maestro Jan 19 '20 at 19:31
  • @M.M the compiler decides if the variable is incremented after it is assigned to itself or not - This is undefined behaviour – dan1st Jan 19 '20 at 20:25
  • 2
    please stop closing this as duplicate of a UB question, there is no UB – M.M Jan 19 '20 at 20:26
  • 1
    It's been 3 years since this was changed, one of the main complaints about stackoverflow is shutting down questions as "duplicates" of old stuff that is now wrong (or in this case, no longer a duplicate) – M.M Jan 19 '20 at 20:31
  • Try to set both compilers to c++17 and check again. – zdf Jan 19 '20 at 21:43
  • @M.M: It's possible I jumped to conclusions. `i = i++` certainly has undefined behavior in C, but I understand the rules have changed a bit in C++. `g++ -std=c++17 -pedantic` warns "operation on ‘i’ may be undefined". I'm looking at the C++ standard for more information. Meanwhile, if it's well defined due to a change in C++, can you cite a standard reference supporting that? – Keith Thompson Jan 19 '20 at 22:49
  • @M.M I see an example in C++17 4.6 [intro.execution] saying that `i = i++ + 1;` is well defined. That example is not in C++14. I'm not yet sure what normative wording made that change. I've reopened the question. – Keith Thompson Jan 19 '20 at 23:14
  • 1
    @KeithThompson see https://stackoverflow.com/questions/47702220/what-made-i-i-1-legal-in-c17 – M.M Jan 20 '20 at 01:31
  • I originally marked this question as a duplicate myself. It isn't, or at least it isn't a duplicate of https://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points. `i = i++` is well defined in C++17. https://stackoverflow.com/questions/47702220/what-made-i-i-1-legal-in-c17 is related, but I don't think this is quite a duplicate of it. (I can't vote to reopen it because I already did.) If it's reopened again, please read the comments and answers before voting to close it. – Keith Thompson Jan 20 '20 at 17:57

2 Answers2

2

In standard C++ up to and including the 2014 edition of the standard, the expression i = i++ has undefined behavior, because the object i is modified twice and the two modifications are unsequenced.

The 2017 edition (C++17) made some changes in the description of expression evaluation. An example in C++17 4.6 [intro.execution] says:

void g(int i) {
    i = 7, i++, i++; // i becomes 9

    i = i++ + 1;     // the value of i is incremented
    i = i++ + i;     // the behavior is undefined
    i = i + 1;       // the value of i is incremented
}   

A lot of people reading that code are going to assume that the behavior of i = i++ is undefined. In fact, if your compiler implements C++17 correctly (and you invoke it in a way that tells it to conform to C++17, for example -std=c++17 for gcc or clang), then the value of i after the assignment will be 5. But for a compiler conforming to an older version of C++, or invoked in a non-conforming mode, the behavior is still undefined.

I'll note that g++ -std=c++17 -Wall (built from the latest gcc sources about a week ago) still warns that i = i++ may be undefined, though running a program still produces the C++17-correct result.

Most likely the compiler you're using does not conform to C++17.

More generally, there's really no good reason to write i = i++ except as a test of compiler conformance. In a conforming C++17 (or later) compiler, it does nothing. Evaluating i++ yields the previous value of i and, as a side effect, updates i -- but the updated value is overwritten by the assignment.

(In C, i = i++ still has undefined behavior.)

(I welcome comments from anyone who has read the C++14 and C++17 standards and can cite the normative text that implies this change.)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
0

In an assignment expression, the right operand is sequenced before the left operand (this includes all evaulations and side-effects), and the assignment is sequenced after both.

So i++ changes i to 6, but evaluates to 5. Then the assignment assigns this 5 to i resulting in i being 5.

If you see 6 then it is either a compiler bug or you are operating the compiler in pre-C++17 mode where the behaviour was undefined. You could either upgrade your compiler or use switches to invoke C++17 mode.

Further info: What made i = i++ + 1; legal in C++17?

M.M
  • 138,810
  • 21
  • 208
  • 365