0

Possible Duplicate:
What is more efficient i++ or ++i?

No difference between these:

i++;
++i;

But when using them like this:

anArray[ i++ ] = 0;
anArray[ ++i ] = 0;

Is there a difference?

TO BE EDITED: Thanks

Community
  • 1
  • 1
  • There are a ton of questions asked about this: http://stackoverflow.com/questions/561588/what-is-more-efficient-i-or-i yes there is a difference between the two – Grammin Mar 03 '11 at 21:29
  • @Mark, yes very difficult. Am so sorry I hope you take my appologise for it. Thought this would be THE place for questions about it. Well propably not THE most complete answer tank about the subject. too bad –  Mar 03 '11 at 21:32
  • @Steijn, no need to apologize, I was being serious and sympathetic. For the future, the terms you need to use are Preincrement and Postincrement. – Mark Ransom Mar 03 '11 at 21:40
  • 1
    @Mark well that's something to work with. The place to ask is THE place to give answers in a constructive way. But it's how 'social media' works I guess. It is anti-social. It's propable you who downvoted so I can't do anything. Well except cancelling my account wich I will. Goodbye. –  Mar 03 '11 at 21:46

6 Answers6

3

Very much so.

i++ -> use i first and then increment it's value
++i -> increment i first and then use i's new value
Andrew White
  • 52,720
  • 19
  • 113
  • 137
2
i++ uses the value and then increments it.
++i increments the value and then uses it.
John K.
  • 5,426
  • 1
  • 21
  • 20
2

Suppose i=3.

anArray[ i++ ] = 0;

Sets element at array index 3 to 0.

anArray[ ++i ] = 0;

Sets element at array index 4 to 0.

Xeo
  • 129,499
  • 52
  • 291
  • 397
1

It is all about order of operations:

E.g.

int i = 1;
int a = i++;

Is equivalent to:

int i = 1;
int a = i;
i++;

While the opposite is:

int i = 1;
i++;
int a = i;

Do note that statements like these are undefined in C++.

int i = 0;
i = i++;
Maister
  • 4,978
  • 1
  • 31
  • 34
0

Yes! Big difference!

i++ increments i after the line of code is executed expression is evaluated. ++i increments it before. For example:

int i = 2;
printf("%i\n", i++); //prints "2"
printf("%i\n", i);   //prints "3"

Compare with:

int i = 2;
printf("%i\n", ++i); //prints "3"
printf("%i\n", i);   //prints "3"

And I've heard that ++i is ever so slightly faster. Here is more on that.

Hope this helps!

UPDATE 2: Killed the last update, since the code technically had undefined behaviour. Moral of that story: Don't use foo(i, ++i), foo(++i, ++i), foo(i++, i), etc. Or even array[i] = ++i. You don't know what order the expressions get evaluated in!

Community
  • 1
  • 1
Xavier Holt
  • 14,471
  • 4
  • 43
  • 56
  • Have accepted this answer due to the given example to test it yourself –  Mar 03 '11 at 21:38
  • After the line of code is executed? Definitely not! The standard guarantees that when evaluating function arguments, all side effects take place before the function is entered. So in this case, `i` is definitely incremented *before* `printf` begins executing. It's just that the value of `i++` is the old value, no matter when the increment happens exactly. – fredoverflow Mar 03 '11 at 22:05
  • Not after the line but rather afterencountering the statement. In MSVC# you can follow the way a line is executed real great. –  Mar 03 '11 at 22:19
  • @Ste: What do you mean, after the statement? After the call to `printf`? As I said, the increment definitely happens before that. Imagine that `i` was a global variable. If `printf` somehow accessed that global variable, it would already see the new value, not the old one. – fredoverflow Mar 03 '11 at 22:23
  • FredOverflow said not after the line is executed. Think myself a line is executed in parts and not as whole. `printf( "%i\n", i )` first i is calculated (if any calc needed) than the string is calculated (if any) and than passed into the function param list wich gets executed as last. –  Mar 03 '11 at 22:37
  • @Fred @Steijn - Sorry about that! You're absolutely right - the increment happens before/after the _expression_ (in this case, simply `++i` or `i++`) is evaluated, not the line of code. I've patched my answer, and I've added a more useful example. Apologies for the inconvenience! – Xavier Holt Mar 03 '11 at 23:52
  • Actually, evaluating `++a` and `a` without an intervening sequence point is [undefined behavior](http://stackoverflow.com/questions/4176328/). – fredoverflow Mar 03 '11 at 23:55
  • @Fred - I did put a "only GCC for sure" disclaimer on that last bit, but to clarify: Are you saying that the order of evaluation is undefined even when using a fixed compiler+version? I assume that a (deterministic) compiler will always produce the same assembly for a given program... Does the ambiguity in the C standard leave room for the compiler to make "optimizations" that would change the order of evaluation depending on what the expressions are? – Xavier Holt Mar 04 '11 at 00:08
0

If you use them as statements by themselves there is no difference in the output. But if you use them as an expression then there is a subtle difference.

In the case of anArray[ i++ ] = 0; i is incremented after the assignment is done. Whereas in the case of anArray[ ++i ] = 0; i is incremented before the assignment is done.

For example of i = 0, then anArray[ i++ ] = 0; will set anArray[ 0 ] to 0 and i will be incremented to 1. But if you use anArray[ ++i ] = 0; then anArray[ 1 ] is set to 0 since i is already incremented.

yasouser
  • 5,113
  • 2
  • 27
  • 41