0
What is the value of i in ++i=1;
Is it different from i++=1; ?
5 Réponses
+ 2
I just want to point out that if you are using a version of C++ older than C++11, `++i = 1` is actually *undefined behaviour*, and you never want to go there.
If you're doing this for some coding challenge that's fine, but if you're ever in a situation where `i++` and `++i` produce different results you should probably think about rewriting your code :P
+ 2
i++ = 1 equivalent but no equals to i = i + 1 = 1 evaluation are right to left hence compilation error.
++i = 8; // i = 8
Testing under diferent language levels as the same result.
g++ pre.cpp -o pre -std=c++14
g++ pre.cpp -o pre -std=c++11
A mindblow code.
0
0
0
++i is prefix it means increment value of i first and then store it or assign it to new variable while i++ is postfix. In it value of i first stored and then incremented.
0
OK. I did try code playground and here are the results:
i++ = 1; //compiler error
++i = 1; // i ends up with value 1
++i = 5; // i ends up with value 5
Apparently the assignment occurs after the increment so it doesn't produce any change.
I had to solve it in a code challenge.