c - c中for循环的一个例子

标签 c for-loop

我遇到了以下问题:

下面的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/

相关文章:

有人可以解释这段代码中的这些行吗?

c - 如何通过从字符串常量到 'char' 的不推荐转换来解决此错误?

c - 将结构体中的字符串传递给函数并返回它

c - 从文件中读取行而不知道行长度

c++ - 重印选项列表

C语言: passing info through a File . csv到使用链表的结构

python - 在 python 中使用 for 循环遍历文本文件 - 为什么这有效?

javascript - 如何在函数参数中使用循环并将结果记录到控制台

for-loop - 使用 ForEach 转换为 lambda 表达式以中断 for 循环

c++ - 在迭代过程中是否会访问在std::unordered_set(或unordered_map)中添加的元素?