What is the difference between ++x and x++?
I know ++x increments then return the value. and x++ assign the value then adds.
But I'm still not sure how it works and when to use either one. Please help. Thanks in advance
What is the difference between ++x and x++?
I know ++x increments then return the value. and x++ assign the value then adds.
But I'm still not sure how it works and when to use either one. Please help. Thanks in advance
This code is an example where it doesn't matter
int array[3];
for (int i = 0; i < 3; ++i)
array[i] = 0;
This code does exactly the same
int array[3];
for (int i = 0; i < 3; i++)
array[i] = 0;
Here's some similar code where it does make a difference
int array[3];
int i = 0;
while (i < 3)
array[i++] = 0;
in this code we use the value of i
before the increment, so we do effectively do array[0] = 0; array[1] = 0; array[2] = 0;
. That's OK but this version is incorrect
int array[3];
int i = 0;
while (i < 3)
array[++i] = 0;
Here we use the value of i
after the increment, so we do effectively do array[1] = 0; array[2] = 0; array[3] = 0;
which is an error because array[0]
is not assigned and worse still array[3]
is not a valid array element.
x++ is post-increment & ++x is pre-increment. Say
++x:
int x=3;
int y=++x; //Here result of y is 4 and x is 4
x++:
int x=4;
int y=x++; //Here result of y is 4 and x is 5 After this line execution
When to use what pre/post, its totally depends on the your programming logic. Example code
int add(int x, int y)
{
return x + y;
}
int main()
{
int x = 5;
int value = add(x, ++x); // is this 5 + 6, or 6 + 6? It depends on what order your compiler evaluates the function arguments in
std::cout << value; // value could be 11 or 12, depending on how the above line evaluates!
return 0;
}
In case of GCC you will get output as 12.
It doesn't really hold much differences on its own. But this incremental operation matters on how and where you use them. For example, you are trying to assign some value in index 'x' and then that index will be incremented.
say, at some point of execution of your code, x = 3;
if you write, array[x++] = value;
Then 'value' will be assigned to array[3]. Because as you say, x++ will assign the value then adds.
Where as, if you write, array[++x] = value;
Then 'value' will be assigned to array[4]. Because as you say, ++x increments then return the value.
So ultimately its upto the coder to use this feature in suitable places. To observe it more clearly, i am posting a sample code below.
int dp[10];
int main(){
int x = 3;
dp[x++] = 4;
for(int i=1;i<10;i++){
cout<<i << " " << dp[i]<<endl;
}
cout<<"--------------------------"<<endl;
memset(dp, 0,sizeof dp);
x = 3;
dp[++x] = 4;
for(int i=1;i<10;i++){
cout<<i << " " << dp[i]<<endl;
}
return 0;
}
++x is increment and fetch
x++ is fetch and increment
If a user defined class is implementing post increment return type of it would be const .
But in case of pre increment, retrun type would be reference.
So post increment will cause creation and destruction of tempories. While pre increment does not suffer from such extra costs.
++x should be preferred whenever possible for user defined classes.