这是一个丑陋的代码,仅用于在面试期间恐吓求职者...... 但我无法理解其背后的逻辑。 有人能解释一下为什么带有“b”的表达式不等于带有“a”的表达式吗?
#include <stdio.h>
void main(){
int a = 1, b = 1, c = 1;
printf("%d %d %d", ++a + a++ + 1, 1 + ++b + b++, ++c + c++); // displays 6 5 5
}
非常感谢。
最佳答案
阅读Undefined behavior and sequence points .
这是一个略有不同但相似的示例(感谢 Zan):
2)此外,仅应访问先前值以确定要存储的值。
C++ 示例:
std::printf("%d %d", i,++i); // invokes Undefined Behaviour because of Rule no 2
关于C 中的组合前增量和后增量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23163887/