+ 2
how ++a and a++ works ? help with example
3 Answers
+ 4
First, it's clearly presented in the course.
++a is adding 1 to variable a and then use it,
a++ is first using the variable, then adding 1 to it.
int a = 3;
int y;
y = a++; //y=3, a=4
// OR
y = ++a; //y=4, a=4
+ 3
both are used to increment a value by one but ++a increment first before printing a number and a++ stores the value in memory but prints the original value not the incremented one and when u use a(variable) again it will use the incremented value rather than the original one.
for e.g.
int a=3;
cout<< a++ << ++a <<endl;
court<< a;
return 0;
output
3 5
5
0
Edit court to cout...