Foo*_*ooo 5 c++ programming-languages
我遇到了一个包含以下代码段的代码,
int main() {
int c = 100;
printf("\n %d \t %d \n", c, c++);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我预计输出为100和101,但输出为
101 100
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我知道原因吗?
无法保证是c在左侧还是c++在右侧,首先评估.
函数参数的评估顺序是Unspecifeid,因此符合标准的Undefined Behavior.
根据C++标准的1.9节:
"抽象机器的某些其他方面和操作在本国际标准中被描述为unspecified(for example, order of evaluation of arguments to a function).在可能的情况下,本国际标准定义了一组允许的行为.这些定义了抽象机器的非确定性方面."
ash*_*sh2 -3
printf 从右到左工作,因此首先执行 c++ (c= 100),然后在 C++ 执行后 C=101,因此输出 101 和 100 http://en.wikipedia.org/wiki/Printf