ani*_*001 0 c c++ pre-increment post-increment
我今天看到了一个有趣的声明,包括后增量和预增量.请考虑以下计划 -
#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;
}
Run Code Online (Sandbox Code Playgroud)
那个有趣的陈述中究竟发生了什么?后增量应该在语句完成后增加x的值.然后,对于该语句,第一个x的值保持为5.并且在预增量的情况下,第二个x的值应该是6或7(不确定).
为什么它给0到z的值?是5 - 5还是6 - 6?请解释.
Ada*_*eld 10
这是未定义的行为.编译器可以随意做任何事情 - 它可能会给0,它可能会给42,它可能会擦除你的硬盘,或者它可能会导致恶魔飞出你的鼻子.所有这些行为都是C和C++语言标准所允许的.