سؤال

It seems to me that

for(int i = 0; i < 2; i++)

and

for(int i = 0; i < 2; ++i)

should not do the same thing. For the second example it's more logic to me that i should equals 1 since the begining of the loop.

هل كانت مفيدة؟

المحلول

On a few processors, such as PDP-11, VAX-11 and 68K, there are modes for some instructions that do the equivalent of *ptr++ and *--ptr. Depending on the exact way the code is written, it may or may not be easy for the compiler to utilize these instructions - so, using the "right" variant of *++ptr or *ptr++ [which is better depends a bit on the usage in the actual loop] inside the loop can make a decent difference in performance in these particular cases. However, for ARM and x86 (and I think, PowerPC and MIPS, thus covering nearly all modern processors), this type of construct isn't part of the instruction set anyway, and compilers are a lot more clever these days than they were, say, 10 or 20 years ago.

Any reasonable compiler will do the same thing for basic types, the i++ or ++i is not an issue.

If we have a "complicated" type, such as a struct or class with an operator++(int), and opterator++() there may be a small difference [or in extreme cases, a big difference], because the object has to be copied. Say we have a bigmath class, and our i is a 1000 digit long value. Now we have to make a(n extra) copy of 1000 digits in the middle of the increment. If the compiler is able to "understand" what is going on, it may elide that copy, but it's not guaranteed to happen.

نصائح أخرى

It seems to me that they should do exactly the same thing. Both have the effect of incrementing i.

The only difference is the value of the expression; i++ gives the value before the increment, and ++i gives the value after the increment. But in this context, the value is discarded, and the expression only evaluated for its side effects; so there is no difference in the effect of the code.

In some cases (if i isn't int but some user-defined type with an overloaded operator too complicated for the compiler to understand), ++i can be more efficient. That's because i++ needs to make a copy to return the old value, while ++i only needs to return a reference to the new value. And, of course, a sufficiently insane user-defined type could make the two overloads do completely different things. But for simple types there will be no difference there either.

Although ++i and i++ have different functionality when you're taking the return value into account, in pretty much every case, the compiler will optimize i++ into ++i for for loops.

If you don't know what the difference is that ++i increments and then returns the value of the new i, whereas i++ increments and then returns the value of the old i. The former is technically more efficient because you don't have to store an intermediate value, but overall, they really don't have that much difference unless you're trying to do code golf and use the return values as well.

In your example: no difference. Had the type been a user defined one, there could be great differences.

As to which one is more logical is certainly a debatable issue. Recommend you go with the style of your team or whatever work's for you.

In your context, it's the same. The difference between ++i and i++ is order of operations. If you were a mad evil C programmer, you might code something like this:

int i = 3;
if( ++i++ == 4 ) 
    printf( "%d\n", i);

which would output

5

because i would be 4 during the if test, but 5 immediately after the test.

But don't do that.

My lecturer went to great pains to explain this to the class in my first semester. From what i recall im pretty sure the ins and outs of it are mostly to do with the 'return' value, for example:

++i will increment the value of i, and return the incremented value, until the operation has completed, Whereas although i++ will also increment the value, if the variable is called for during the operation, it will return the un-incremented value.

So if you had this:

a = 1;
b = ++a;
c = 10;
d = c++;

then the values will be

a = 2
b = 2
c = 11
d = 10

(d is this because c has been returned with the 'post' ++'s rather than the usual 'pre' ++'s).

In a loop it makes absolutely no difference, because the increment is in the loop statement not in the operations after it. The only time it would make a difference is

for(i = 0; i<5;)
    printf("%d ",++i);

where the 0 value wouldnt be printed and the 5 value would be, because its incremented DURING the print operation.

Clear as mud eh?? Dont ask me why this has been programmed into C! In my opinion it seems to be redundant, and something that would pop up so rarely any program needing it could get around the problem with one extra line of code altering the value in a different variable!

in your case it is the same, since the return value of your expression is not used.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top