有什么区别++ i和i ++的区别 [英] What is the difference between ++i and i++

查看:126
本文介绍了有什么区别++ i和i ++的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C,是两者的区别: ++我我++ ,并应在使用一个循环的增量块?

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

推荐答案


  • ++我将递增 i的值,然后返回增加后的值。

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

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


  • 我++ 将递增 i的值,但返回的原始值 I 递增之前举行。

  • 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)
    


  • 对于循环,无论是作品。 ++我似乎更常见,可​​能因为这是在 K&放大器使用; - [R

    For a for loop, either works. ++i seems more common, perhaps because that is what is used in K&R.

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

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

    有一个关于 ++我我++ 的效率夫妇的意见。在任何非学生项目的编译器,将不会有性能差异。您可以通过查看生成的code,这将是相同的验证。

    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.

    效率问题是有趣......这里是我的上述问题的回答:
    是否有性能差异之间我++和++我用C?

    The efficiency question is interesting... here's my attempt at an answer: Is there a performance difference between i++ and ++i in C?

    作为的在弗氏的笔记,它是一个C ++对象不同,因为符++()是一个函数,编译器无法知道优化掉临时对象的创建来保存中间值。

    As On Freund 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.

    这篇关于有什么区别++ i和i ++的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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