We can define a++
as
a = a + 1;
Which is self-explaining that it makes a copy of a
then adds 1 to it and puts the result in a
.
But can we define ++a
the same way? Maybe the answer is very simple but I'm unaware of it.
We can define a++
as
a = a + 1;
Which is self-explaining that it makes a copy of a
then adds 1 to it and puts the result in a
.
But can we define ++a
the same way? Maybe the answer is very simple but I'm unaware of it.
a++ and ++a have differences in precedence. - a++: you evaluate a before a is incremented; - ++a: you increment a before it is evaluated or used
The difference is:
int a = 1;
int b = ++a;
// Now a == 2 and b == 2
int c = 1;
int d = c++;
// Now c == 2 and d == 1
The difference is in the returned value of the operation.