bha*_*438 1 c for-loop decrement postfix-operator
我刚刚遇到这个 C 问题,代码如下:
int fun();
int main()
{
for (fun(); fun(); fun())
printf("%d\n", fun());
return 0;
}
int fun()
{
int static n = 10;
return n--;
}
Run Code Online (Sandbox Code Playgroud)
我期望for循环计算为for(10;9;8)并printf打印 7 (因为我们将使用fun()?进行调用n = 7),但它打印了8。为什么会出现这种情况?
期待for(10;9;8)是错误的。
循环的操作顺序for是:
for (<0>; <1>; <3>) {
<2>;
}
Run Code Online (Sandbox Code Playgroud)
假设<1>为真,<2>不包含结束循环的语句,并且在<3>之后构造从<1>重复。
这样你就有了for (10; 9; 7) { 8; }。
然后for (_; 6; 4) { 5; },等等。
| 归档时间: |
|
| 查看次数: |
112 次 |
| 最近记录: |