Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
what is the basic difference between i++ and ++i ? which is better?
Posted
Updated 13-Jul-11 2:49am
v2
Comments
ThatsAlok 13-Jul-11 8:49am    
editor comment: changed second i++ to ++i typo mistake
Sergey Alexandrovich Kryukov 13-Jul-11 16:43pm    
Why not simply reading C++ reference?
--SA

n = ++i; // #1 : assigns i to n after adding 1 to i
n = i++; // #2 : assigns i to n before adding 1 to i

which is better?

++i should be preferred over i++; I believe Stroustrup, Myers and others have stated this in various books and/or post.

The reason we preferred this in C (especially for graphics), was that the compiler created a temporary (at assembly level) to hold the previous value of i; the temporary was then assigned to n after adding 1 to i. Therefore, it was less efficient.

For built-in types a modern compiler will normally optimize out the temporary; although they are not required to do so.

In C++ it is even more important to get in the habit of preferring ++i. The reason for this is operator overloading; which, unless the class is very simple, the compiler cannot optimize out the temporary; which can be very costly. You do not want to accidentally create unneeded overhead because you are in the habit of writing i++.

Just create a simple class and override the pre and post increments, then step through it to see the problem.

Most modern books consistently use i++ when presenting code examples. This is bad, because 99% of the time ++i could have been used instead.

for( i=0; i<10; ++i ) {} // Good habit
for( i=0; i<10; i++ ) {} // Bad habit
 
Share this answer
 
v2
Comments
Emilio Garavaglia 14-Jul-11 2:28am    
Good point addressing also the last OP question.
ThatsAlok 14-Jul-11 2:33am    
yes this is the answer i am looking for and in discussion with cpallini, +5ed
1. i++ will increment i and returns the i value.
2. i+1 is rvalue that wont effect i and returns i+1 value.
3. ++i, increments i and returns i+1 value;

example:
int i = 9;
int j;
j = i++; //After this statement j value will be 9, i value will be 10;
j = i+1; //After this statement j value will be 10, i value will be 9;
j = ++i; //After this statement j value will be 10, i value will be 10;


so normally in stand alone expressions like i++; and ++i; have no difference.but the difference comes when you use them in assign statements(as shown in example);
 
Share this answer
 
v2
Comments
Joan M 13-Jul-11 11:34am    
Good answer that covers all the possibilities... 5ed!
Thank you Joan Murt
Philippe Mori 16-Jul-11 19:04pm    
This is a good explanation for primitive types but it does not covers user defined type (like STL iterators).
try:
C
int i = 5;
int a = i++;
printf("%d\n", a);


vs

C
int i = 5;
int a = ++i;
printf("%d\n", a);



Hint: first snippet will output 5, second one will output 6.

Or you may even try reading the documentation (it won't hurt you).
 
Share this answer
 
v3
Comments
Good home work :-).My 5.
Guyverthree 13-Jul-11 8:44am    
don#'t forget that ++ is an operator and + 1 is not.

therefore if i is a pointer + 1 might only move the pointer a single byte.
where as ++i or i++ will move it the size of the data that the pointer points to :)
CPallini 13-Jul-11 8:48am    
You are wrong: the pointer math is consistent with the 'integers' one, hence (p+1) and, for instance, (++p) point to the same memory address.
Guyverthree 18-Jul-11 8:57am    
LPVOID does not have a size on Linux so + 1 will move it a single byte. Windows for instance it is 4 bytes.
and ++ will not compile.
CPallini 18-Jul-11 11:26am    
That's true just for the very special case of (void *) pointers.
Try to find a difference :)

// 1.
{
  int a[2] = {0};
  int i = 0;

  do {
    a[i++] = 1; // the index 0 is touched as well (0 and 1 are touched)
  } while (i < 2)
}

// 2.
{
  int a[2] = {0};
  int i = 2;

  do {
    a[--i] = 1; // the index 2 is not touched (0 and 1 are touched)
  } while (i)
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900