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
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes! Big difference!
i++incrementsiafter theline of code is executedexpression is evaluated.++iincrements it before. For example:Compare with:
And I’ve heard that
++iis 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 evenarray[i] = ++i. You don’t know what order the expressions get evaluated in!