我知道这是一个愚蠢的问题,但我只是出于好奇才问这个问题。 我刚刚在某处读过这段代码:
#include<stdio.h>
int main() {
for ( ; 0 ; )
printf("This code will be executed one time.");
return 0;
}
输出:
This code will be executed one time.
这个循环在Turbo C编译器中执行一次,而在gcc中不工作,但是这个循环怎么可能执行一次?
能否请您指导一下 Turbo C 编译器中此代码的异常行为(如果有)?
最佳答案
这是编译器中的一个错误。 C99 标准对 for 循环的描述如下:
The statement
for ( clause-1 ; expression-2 ; expression-3 ) statement
behaves as follows: The expression expression-2 is the controlling expression
that is evaluated before each execution of the loop body.
The expression expression-3 is evaluated as a void expression after each
execution of the loop body. [...]
鉴于 expression-2 的计算结果为 false,代码不应打印任何输出。
关于C:Turbo C 编译器中 for 循环的异常行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18934587/