C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

C++

x++ and ++x ... does someone have a piece of code to make sure i understood ? would be nice i m not sure to understand ><

20th Nov 2016, 10:30 AM
David
David - avatar
5 Answers
+ 1
x++ first evaluates the expression with the previous value of x and than increment it. ++x on the other side, first increment x, then uses the updated value of it in the expression.
20th Nov 2016, 10:46 AM
Ernaso Kerbizi
Ernaso Kerbizi - avatar
+ 1
int main() { int a,b; a = 5; b = a++; cout << "b gets the value of a and than a is incremented by 1 \n a = " << a << " and b = " << b << endl; a = 5; b = ++a; cout << "a is incremented by 1 and than b gets the value of a \n a = " << a << " and b = " << b << endl; return 0; }
20th Nov 2016, 10:48 AM
Ethan
Ethan - avatar
+ 1
i++ always will be the last value declared for i ? and ++i = i+1 ? @ernaso kerbizi
20th Nov 2016, 10:50 AM
David
David - avatar
+ 1
thanks eitan and ernaso, i got it :)
20th Nov 2016, 10:55 AM
David
David - avatar
0
int i = 5; cout << i++; //Outputs 5 cout << ++i; //Outputs 6
20th Nov 2016, 10:43 AM
TheLastCookie
TheLastCookie - avatar