“在任何情况下,遵循的方针和QUOT; preFER ++我在我++ QUOT;你会不会出问题。“什么是用C这背后的原因是什么? [英] 'In any case, follow the guideline "prefer ++i over i++" and you won't go wrong.' What is the reason behind this in C?

查看:232
本文介绍了“在任何情况下,遵循的方针和QUOT; preFER ++我在我++ QUOT;你会不会出问题。“什么是用C这背后的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经碰到过回答的这个问题。在线路之一,笔者提到的:

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

在任何情况下,按照指引preFER ++我 i ++ ,你赢了T出问题。

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

我知道 ++我略快于我++ ,但认为没有理由对他们出问题。我搜索了一会儿,最接近我能得到WAS 这个。它清楚地解释,为什么是$ pferred为使用p $ ++我,但不还是你怎么会错用我++

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.

推荐答案

在的情况下

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

VS

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

及其含义的完全一致,因为前pressions 我++ 的价值++我不被使用。 (他们只副作用的评价。),这两个产生不同的code编译器的任何病理是坏的,应在垃圾被卡住。在这里,使用任何形式你preFER,但我++ 是C程序员之间惯用的。

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++;

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

  • ++我

  • 我++ + 1

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

和同样以下是等价的:


  • (I + = 1) - 1

  • 我++

  • ++我 - 1

  • (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.

这篇关于“在任何情况下,遵循的方针和QUOT; preFER ++我在我++ QUOT;你会不会出问题。“什么是用C这背后的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆