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

What is the difference between a++ and ++a?

1st Apr 2017, 11:47 PM
prosper
prosper - avatar
4 Answers
+ 10
'++a' is pre-fix 'a++' is post-fix This just means that in '++a' the operation of incrementing 'a' is done BEFORE the 'a' is processed by a function (i.e. cout). In 'a++' the operation to increment is done AFTER 'a' is processed by a function (i.e. cout). cout << ++1 OUTPUT: 2 cout << 1++ OUTPUT: 1 Hope this helps! -bern
1st Apr 2017, 11:57 PM
bernscode
bernscode - avatar
+ 4
https://www.sololearn.com/Discuss/160327/?ref=app the increment operator works the same in the majority of languages.
1st Apr 2017, 11:52 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
yea like int i = 5; int k = 1; int a = i + k++ // a = 6 // k is now 2 vs int i = 5; int k = 1; int a = i + ++k // a = 7 // k is now 2
2nd Apr 2017, 12:00 AM
Edward
+ 2
Thank you all
2nd Apr 2017, 12:14 AM
prosper
prosper - avatar