增量是如何工作的?
可能的重复:
谁能解释一下这些未定义的行为(i = i++ + ++i , i = i++ 等...)
未定义的行为和序列点
,我们都知道 i++ 在下一行将值增加 1和 ++i 在同一行递增
(如果我错了,请纠正我)
因此对于 c 的示例语句如下:
int a=0;
printf("%d , %d",++a,a);
预期输出应该是 1 , 1
但它给出了 1 , 0
所以有人可能会猜到我在这里问的是为什么第二个链接 当值已经递增时,i
打印 0
而不是 1
。
因此,如果后增量没有在同一行中增加值,那么什么是 后增量和预增量之间的区别?
编辑:将变量名称从 i 更改为 a 以避免语法混乱。
Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
Undefined Behavior and Sequence Points
Ok we all know that i++ increments value by 1 on next line and ++i increments on same line
(please correct me if i am wrong there )
so for a sample statement of c as follows:
int a=0;
printf("%d , %d",++a,a);
the expected output should be 1 , 1
but instead it gives 1 , 0
so as one might guess what i am asking here is why does the second linking ofi
print 0
instead of 1
when the value is already incremented.
So if post increment didn't increment value in the same line then what is the
difference between post and pre increment?
edit : changed the name of variable from i to a to avoid grammatical confusion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您对增量运算符的理解是非常错误的。
i++
递增i
,并返回其旧值;++i
递增i
,并返回新值。当实际增量发生时
仅保证在前一个序列点之后和之前
下一个;在您的代码中,这意味着在调用
printf
之前。除此之外(很大程度上正因为如此),如果您修改
对象,如果没有
干预序列点,除非需要确定新值。
您违反了此规则,因此您的代码具有未定义的行为。
You are very wrong in your understanding of the increment operators.
i++
incrementsi
, and returns its old value;++i
incrementsi
,and returns the new value. When the actual incrementation takes place
is only guaranteed to be after the preceding sequence point, and before
the next; in your code, this means before the call to
printf
.Beyond that (and largely because of that), if you modify the value of an
object, you're not allowed to access it anywhere else without an
intervening sequence point, except as needed to determine the new value.
You violate this rule, so your code has undefined behavior.
这是未定义的行为。允许编译器以任何顺序计算参数。你的编译器只是从右到左计算它,所以最右边的参数是0,第二个是1。
编辑:正如Seth所说,编译器只能自由地改变计算的顺序,而不能做任何它想做的事,所以当您不关心可以自由调用函数的顺序,但您永远不应该假设一个参数是在另一个参数之前计算的。
It's undefined behavior. the compiler is allowed to calculate the parameters in any order. your compiler just calculate it from right to left, so the rightest parameter is 0, and the 2nd is 1.
edit: as Seth said, the compiler is only free to change the order of calculating, not to do whatever it wants, so when you don't care about the order you can freely call functions, but you should never assume that one parameter is been calculated before another.
我认为你的问题不是关于增量如何工作。您观察到的现象与将参数传递给函数的顺序有关。据我记得,这不是由 c++ 标准定义的,所以它是一个未定义的行为。这意味着不同的编译器可能有不同的实现。
例如
一个编译器可以从左到右传递参数,然后另一个编译器可以从右到左传递参数。
您可以在此处更好地阅读序列点:
http://en.wikipedia.org/wiki/Sequence_point
I think your question is not about how increment work. The phenomenon you have observed is about the order of passing parameter to a function. As far as I can remember, this is not defined by c++ standard, so it's a UNDEFINED BEHAVIOR. Meaning different compiler could have different implementation.
E.g.
One compiler could pass parameter from left to right, then a different could pass parameters from right to left.
You can have a better read of Sequence point here:
http://en.wikipedia.org/wiki/Sequence_point
是未定义的行为。
您违反了 C 序列点规则:
is undefined behavior.
Your are violating C sequence points rule:
有一次我在某本书上读到,当一个语句是这样的:
printf("%d , %d",++a,a);
执行从右到左开始,但输出为从左到右。因此,您看到输出为 1 0 - 首先计算 'a' = 0,然后计算 '++a' = 1,但显示为 1 0
这很奇怪,但是的,这就是它的工作原理。
希望这有帮助。
Once I read in some book that when a statement is like this:
printf("%d , %d",++a,a);
The execution starts from right to left but outputted as left to right. Therefore you see the out as 1 0 - 'a' is calculated first = 0 then '++a' = 1, but displayed as 1 0
This is strange but yeah that's how it works.
Hope this helps.