0

What is the value of i in ++i=1;

Is it different from i++=1; ?

12th Oct 2016, 2:29 PM
Juan Pablo Giuttari
Juan Pablo Giuttari - avatar
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
12th Oct 2016, 6:56 PM
Schindlabua
Schindlabua - avatar
+ 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.
6th Jan 2017, 3:50 AM
nextco
nextco - avatar
0
0
12th Oct 2016, 2:33 PM
Yashwant Yadav
Yashwant Yadav - avatar
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.
12th Oct 2016, 2:41 PM
ZUNAIRA
ZUNAIRA - avatar
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.
12th Oct 2016, 5:21 PM
Juan Pablo Giuttari
Juan Pablo Giuttari - avatar