In practice, the difference is in the location where the preprocessor searches for the included file.
For #include <filename>
the preprocessor searches in an implementation dependent manner, normally in search directories pre-designated by the compiler/IDE. This method is normally used to include standard library header files.
For #include "filename"
the preprocessor searches first in the same directory as the file containing the directive, and then follows the search path used for the #include <filename>
form. This method is normally used to include programmer-defined header files.
A more complete description is available in the GCC documentation on search paths.
Executive summary: No.
i++
could potentially be slower than ++i
, since the old value of i
might need to be saved for later use, but in practice all modern
compilers will optimize this away.
We can demonstrate this by looking at the code for this function,
both with ++i
and i++
.
$ cat i++.c
extern void g(int i);
void f()
{
int i;
for (i = 0; i < 100; i++)
g(i);
}
The files are the same, except for ++i
and i++
:
$ diff i++.c ++i.c
6c6
< for (i = 0; i < 100; i++)
---
> for (i = 0; i < 100; ++i)
We'll compile them, and also get the generated assembler:
$ gcc -c i++.c ++i.c
$ gcc -S i++.c ++i.c
And we can see that both the generated object and assembler files are the same.
$ md5 i++.s ++i.s
MD5 (i++.s) = 90f620dda862cd0205cd5db1f2c8c06e
MD5 (++i.s) = 90f620dda862cd0205cd5db1f2c8c06e
$ md5 *.o
MD5 (++i.o) = dd3ef1408d3a9e4287facccec53f7d22
MD5 (i++.o) = dd3ef1408d3a9e4287facccec53f7d22
Best Answer
++i
will increment the value ofi
, and then return the incremented value.i++
will increment the value ofi
, but return the original value thati
held before being incremented.For a
for
loop, either works.++i
seems more common, perhaps because that is what is used in K&R.In any case, follow the guideline "prefer
++i
overi++
" and you won't go wrong.There's a couple of comments regarding the efficiency of
++i
andi++
. In any non-student-project compiler, there will be no performance difference. You can verify this by looking at the generated code, which will be identical.The efficiency question is interesting... here's my attempt at an answer: Is there a performance difference between i++ and ++i in C?
As @OnFreund notes, it's different for a C++ object, since
operator++()
is a function and the compiler can't know to optimize away the creation of a temporary object to hold the intermediate value.