我遇到了以下问题:
下面的for循环会运行多少次-
for(;0;)
printf("hello");
我执行了它运行了 1 次。我无法理解怎么办?
最佳答案
即使执行 1 次也不会执行。我猜你的编译器不好?
好的。我认为您正在使用 Turbo C ;-)
编辑:
来自 C99 标准:
6.8.5.3 The for statement 1 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. If clause-1 is a declaration, the scope of any variables it declares is the remainder of the declaration and the entire loop, including the other two expressions; it is reached in the order of execution before the first evaluation of the controlling expression. If clause-1 is an expression, it is evaluated as a void expression before the first evaluation of the controlling expression.134)
它明确指出在执行循环之前首先评估条件。任何符合标准的编译器都不应该执行循环 for(;0;) {}
,哪怕是一次。
关于c - c中for循环的一个例子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11048465/