今天,我看到了一篇有趣的声明,内容包括后增量和前增量。请考虑以下计划-
#include <stdio.h>
int main(){
int x, z;
x = 5;
z = x++ - 5; // increase the value of x after the statement completed.
printf("%d\n", z); // So the value here is 0. Simple.
x = 5;
z = 5 - ++x; // increase the value of x before the statement completed.
printf("%d\n", z); // So the value is -1.
// But, for these lines below..
x = 5;
z = x++ - ++x; // **The interesting statement
printf("%d\n", z); // It prints 0
return 0;
}
这句有趣的话到底是怎么回事?后增量应该是在语句完成后增加x的值。那么,对于该语句,first x的值仍然是5。在预增量的情况下,秒x的值应该是6或7(不确定)。
为什么它给出的值为0到z?是5-5还是6-6?请解释一下。
发布于 2013-08-27 19:30:15
这是未定义的行为。编译器可以自由地做它想做的任何事情--它可能给0,它可能给42,它可能会擦除你的硬盘驱动器,或者它可能会让恶魔从你的鼻子里飞出来。所有这些行为都是C和C++语言标准所允许的。
https://stackoverflow.com/questions/18473886
复制相似问题