Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between ++i and i++?

In C, what is the difference between using ++i and i++, and which should be used in the incrementation block of a for loop?

like image 826
The.Anti.9 Avatar asked Aug 24 '08 05:08

The.Anti.9


People also ask

What is the difference between I and Ï?

Initially in French and also in Afrikaans, Catalan, Dutch, Galician, Southern Sami, Welsh, and occasionally English, ⟨ï⟩ is used when ⟨i⟩ follows another vowel and indicates hiatus in the pronunciation of such a word.

What is the difference between I and me?

“I” is a first person singular pronoun that is used as the subject of a sentence or clause. (Example: I write the songs.) “Me” is used as an object. (Ex: The songs are written by me.)

What does ː mean in IPA?

IPA. In the International Phonetic Alphabet the sign ː (not a colon, but two triangles facing each other in an hourglass shape; Unicode U+02D0 ) is used for both vowel and consonant length. This may be doubled for an extra-long sound, or the top half (ˑ) may be used to indicate that a sound is "half long".

What is me and what is I?

Both me and I are 1st person singular pronouns, which means that they are used by one person to refer to himself or herself. I is the subject pronoun, which means that it is used as the subject of a verb, the one "doing" the verb, as in these examples: I am studying for a Russian test.


2 Answers

  • ++i will increment the value of i, and then return the incremented value.

     i = 1;  j = ++i;  (i is 2, j is 2) 
  • i++ will increment the value of i, but return the original value that i held before being incremented.

     i = 1;  j = i++;  (i is 2, j is 1) 

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 over i++" and you won't go wrong.

There's a couple of comments regarding the efficiency of ++i and i++. 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.

like image 139
Mark Harrison Avatar answered Oct 04 '22 00:10

Mark Harrison


i++ is known as Post Increment whereas ++i is called Pre Increment.

i++

i++ is post increment because it increments i's value by 1 after the operation is over.

Lets see the following example:

int i = 1, j; j = i++; 

Here value of j = 1 but i = 2. Here value of i will be assigned to j first then i will be incremented.

++i

++i is pre increment because it increments i's value by 1 before the operation. It means j = i; will execute after i++.

Lets see the following example:

int i = 1, j; j = ++i; 

Here value of j = 2 but i = 2. Here value of i will be assigned to j after the i incremention of i. Similarly ++i will be executed before j=i;.

For your question which should be used in the incrementation block of a for loop? the answer is, you can use any one.. doesn't matter. It will execute your for loop same no. of times.

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

And

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

Both the loops will produce same output. ie 0 1 2 3 4.

It only matters where you are using it.

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

In this case output will be 1 2 3 4 5.

like image 42
Parag Avatar answered Oct 04 '22 01:10

Parag