int ++和++ int有什么区别? [英] What is the difference between int++ and ++int?
问题描述
是的我是菜鸟,但我完全忘记了他们都做了什么。
Yes I'm a noob, but I completely forgot what they both do.
然而,我知道int ++只是在int的值上加一。
I know, however, that int++ just adds one to the value of int.
那么,什么是++ int?
So, what is ++int?
谢谢。
推荐答案
如果您正在谈论C(或类C语言),除非您使用,否则它们完全相同值:
If you're talking about C (or C-like languages), it's exactly the same unless you use the value:
int a = 10;
int b = a++;
在这种情况下, a
变为11 b
设置为10.这是后增量 - 在使用后增加。
In that case, a
becomes 11 and b
is set to 10. That's post-increment - you increment after use.
如果您将上面的那一行更改为:
If you change that line above to:
int b = ++a;
然后 a
仍然变为11但是 b
。这是因为它是预增量 - 你在使用之前增加。
then a
still becomes 11 but so does b
. That's because it's pre-increment - you increment before use.
注意它对于C ++类来说并不完全相同,有效率可以通过优先选择一个而不是另一个。但是既然你在谈论整数,那么C ++就像C一样。
Note that it's not quite the same thing for C++ classes, there are efficiencies that can be had by preferring one over the other. But since you're talking about integers, C++ acts the same as C.
这篇关于int ++和++ int有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!