Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 573 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the difference between ++i and i++?

#1
In C, what is the difference between using `++i` and `i++`, and which should be used in the incrementation block of a `for` loop?
Reply

#2
I assume you understand the difference in semantics now (though honestly I wonder why
people ask 'what does operator X mean' questions on stack overflow rather than reading,
you know, a book or web tutorial or something.

But anyway, as far as which one to use, ignore questions of performance, which are
unlikely important even in C++. This is the principle you should use when deciding
which to use:

Say what you mean in code.

If you don't need the value-before-increment in your statement, don't use that form of the operator. It's a minor issue, but unless you are working with a style guide that bans one
version in favor of the other altogether (aka a bone-headed style guide), you should use
the form that most exactly expresses what you are trying to do.

QED, use the pre-increment version:

for (int i = 0; i != X; ++i) ...

Reply

#3
Please don't worry about the "efficiency" (speed, really) of which one is faster. We have compilers these days that take care of these things. Use whichever one makes sense to use, based on which more clearly shows your intent.
Reply

#4
`++i` increments the value, then returns it.

`i++` returns the value, and then increments it.

It's a subtle difference.

For a for loop, use `++i`, as it's slightly faster. `i++` will create an extra copy that just gets thrown away.
Reply

#5
The reason `++i` *can* be slightly faster than `i++` is that `i++` can require a local copy of the value of i before it gets incremented, while `++i` never does. In some cases, some compilers will optimize it away if possible... but it's not always possible, and not all compilers do this.

I try not to rely too much on compilers optimizations, so I'd follow Ryan Fox's advice: when I can use both, I use `++i`.
Reply

#6
The difference can be understood by this simple C++ code below:

int i, j, k, l;
i = 1; //initialize int i with 1
j = i+1; //add 1 with i and set that as the value of j. i is still 1
k = i++; //k gets the current value of i, after that i is incremented. So here i is 2, but k is 1
l = ++i; // i is incremented first and then returned. So the value of i is 3 and so does l.
cout << i << ' ' << j << ' ' << k << ' '<< l << endl;
return 0;

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through