'In any case, follow the guideline "prefer ++i over i++" and you won't go wrong.' What is the reason behind this in C?
Asked Answered
G

1

-3

I had come across this answer to this question. In one of the line, the author mention's:

In any case, follow the guideline "prefer ++i over i++" and you won't go wrong.

I know that ++i is slightly faster than i++, but thought that there is no reason for them to go wrong. I searched for a while, and the closest I could get to was this. It explained clearly that why is it preferred to use ++i, but not still how could you go wrong using i++.

So can anyone tell me how can i++ go wrong?

NOTE: My question isn't a dupe, as I am not asking about the performance. I have already seen that question. I am asking how can i++ be wrong as mentioned in the answer I have mentioned above.

Giltzow answered 26/2, 2016 at 5:12 Comment(14)
It's not "slightly faster"; anyone who tells you this has no idea what they're talking about.Indonesia
@R.., but you that is even written in the question that I've linked. It is written that it is faster because, i++ has to store the old value, while ++i does not. So the time storing it is saved. See: Is there a performance difference between i++ and ++i in C?Giltzow
@AshishAhuja While historically there were minor performance differences, modern compilers are so good at spotting and eliminating dead code that I would be astonished if this actually was an issue anymore.Kast
@AshishAhuja: The claim is wrong. There is no "storing the old value" taking place.Indonesia
Even if it's not about performance, I think the (logical) difference between ++i and i++ must have been discussed a gazillion times...Samothrace
@AshishAhuja The answer you quoted also says "++i seems more common, perhaps because that is what is used in K&R." But in fact, a quick look at K&R second edition shows that the book uses both styles, with i++ being the more common. And i++ is used in section 3.5 where the for loop is formally introduced. So I think the answer you quoted is a little biased, and the author is just promoting his preferred style.Racer
@Barmar this isn't a dupe.Marxismleninism
"I am asking how can i++ be wrong as mentioned in the answer I have mentioned above." The only person that can answer your question is Mark Harrison, since he wrote the answer that you quoted. He also wrote the question and answer that Barmar cited. In that question and answer, he not only said that there's no performance difference, but also that the compiler generates identical code. So this question should have been asked as a comment under his answer.Racer
By "go wrong" the person who wrote the post obviously meant "write inefficient code". So yes, this is a duplicate, even if you might not realize.Commixture
Generally the whole "++i is faster" myth originates from C++, where it is sometimes true, since that language has operator overloading.Commixture
@Lundin, how is operator overloading related to post and pre increment?Giltzow
@AshishAhuja Because if you overload the postfix ++ operator in C++, there will be a function call and inside it, a temporary copy of the "this" object is created. Code example.Commixture
Actually array[i++] may be slightly faster than array[++i]. The reason is that on superscalar (e.g. modern Intel) CPUs the increment and address calculation can be done in parallel with the former, but not with the later. This is called instruction level parallelism.Fremd
@Eloff: That's false. array[++i] is equivalent to (array+1)[i++] and the latter is equally capable of being done in parallel.Indonesia
I
5

In the case of

for (i=start; i<end; i++)

vs

for (i=start; i<end; ++i)

their meanings are completely identical, because the value of the expressions i++ and ++i are not used. (They are evaluated for side effects only.) Any compiler which produces different code for the two is pathologically bad and should be chucked in the garbage. Here, use whichever form you prefer, but i++ is idiomatic among C programmers.

In other contexts, use the form that yields the value you want. If you want the value from before the increment, use i++. This makes sense for things like:

index = count++;

or

array[count++] = val;

where the old value of count becomes the index of a new slot you're going to store something in.

On the other hand, if you want the value after increment, use ++i.

Note that for integer types, the following are all completely equivalent:

  • i += 1
  • ++i
  • i++ + 1

and likewise the following are equivalent:

  • (i += 1) - 1
  • i++
  • ++i - 1

For floating point and pointers it's more complicated.

As far as any objective reasons are concerned, the advice you found is just nonsense. If on the other hand you find ++i easier to reason about, then feel free to take the advice, but this is going to depend entirely on the individual and what you're used to.

Indonesia answered 26/2, 2016 at 5:21 Comment(2)
You clear all my doubts. just that you mean to say that @MarkHarrison is wrong, and actually there is no difference between i++ and ++i?Giltzow
@AshishAhuja This answer is pretty much identical to this one posted by me in the linked duplicate.Commixture

© 2022 - 2024 — McMap. All rights reserved.