Can someone explain me what is the difference between ++a and a++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Can someone explain me what is the difference between ++a and a++?

#include <iostream> using namespace std; int main() { int a=1; int b=1; b++; ++a; cout<<a<<" , "<<b; return 0; }

16th May 2020, 2:14 AM
Luis.G
Luis.G - avatar
2 Answers
+ 3
Luis In your code b++ means adding 1 to your variable b, and ++a means adding 1 to both a and b and have the same value of a and b.
22nd May 2020, 8:12 AM
Ketan Pal
Ketan Pal - avatar
+ 1
a++ takes the variable and adds to it. ++a takes the variable, adds to it, then makes that new value the value of the variable.
16th May 2020, 2:56 AM
GermoOKD
GermoOKD - avatar