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
+ 1

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

5th Nov 2016, 1:42 PM
Amr S Haider
Amr S Haider - avatar
3 Answers
+ 6
++a is prefix which increases the variables value and the uses the new value in expression. a++ is posfix which takes the variable value first in the expression and then increase the variable. E.g. prefix: int x = 4; int y = ++x; // y = 5 prosfix: int x = 4; int y = x++;// y = 4
5th Nov 2016, 1:45 PM
Abdelaziz Abubaker
Abdelaziz Abubaker - avatar
+ 1
Example with arrays; int numbers[3] = {7, 9, 1}; int i = 0; cout << numbers[i++] << endl; // Outputs 7 and now i = 1; cout << numbers[++i] << endl; //Outputs 1 and now i = 2; When postincrementing(i++), i was used as an index of the array and THEN incremented by one. When preinctementing(++i), i was incremented by one and then used as an index of array.
5th Nov 2016, 11:57 PM
Karolis Strazdas
Karolis Strazdas - avatar
0
a++ postponed increment and ++a is prepond increment
6th Nov 2016, 6:02 AM
ritik saini