-1

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

Stack Danny
  • 7,754
  • 2
  • 26
  • 55
MyName
  • 31
  • 3
  • 2
    This seems obvious, if you want to use the value before the increment use `x++`, if you want to use the value after the increment use `++x`, if you don't care (all you care about is incrementing the variable) then use either. Personally I prefer `++x` in the don't care case, but I seem to be in a minority. – john Jul 05 '19 at 07:09
  • 3
    As I'm old-fashioned, I always use `++x` over `x++` if I have a choice as the former is not going to be slower than the latter, particularly if the operators are overloaded. But that's falling out of fashion - if it ever was in fashion. The statement "the compiler will optimise it out" is often said in such a discussion. – Bathsheba Jul 05 '19 at 07:13
  • There are performance ramifications when x is a class, such as `std::map::iterator`. See [my answer to a related question](https://stackoverflow.com/a/38948073/434551). – R Sahu Jul 05 '19 at 07:14
  • @Bathsheba, I follow the same coding guideline as you for incrementing a variable. It hasn't fallen out of style for me. – R Sahu Jul 05 '19 at 07:15
  • If I need the increment to be evaluate in the present expression, then I use `++x` for all other cases `x++`. – David C. Rankin Jul 05 '19 at 07:15
  • @DavidC.Rankin, interesting. I find that I rarely need to use `x++`. – R Sahu Jul 05 '19 at 07:24
  • West increment team assemble! – Quentin Jul 05 '19 at 07:32

4 Answers4

1

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.

john
  • 85,011
  • 4
  • 57
  • 81
1

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.

Saravanan
  • 1,270
  • 1
  • 8
  • 15
0

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;
}
Mohtasim
  • 111
  • 5
  • Somehow your explanation is comprehensible if you already knows the difference between ++x and x++ but you are going to far in your example (my opinion). – Stef Geysels Jul 05 '19 at 07:32
  • 1
    Thanks. Yes, I agree. But i thought the question was when to use x++ or ++x. The person here asking question already knows the difference as he states in the post. That's why i chose to show a working example. – Mohtasim Jul 05 '19 at 09:27
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.