gpt4 book ai didi

c - 为什么 for (1; 2; 3) 是无限循环?

转载 作者:行者123 更新时间:2023-11-30 21:32:15 25 4
gpt4 key购买 nike

在程序中打印hello:

for (1; 2; 3) 
printf ("Hello");

为什么输出显示无限循环? for ( ; ; ) 本身不是一个无限循环吗?

最佳答案

嗯,你说得对。在C中,结构是

for ( initialization; condition; increment/decrement)

因此,在您的代码中,条件部分(即中间)中的 for (1; 2; 3) 是一个非零(即 2)这永远是真的。这就是为什么您的代码始终满足 true 条件并发生无限循环 的原因。

根据infinite Loop For 循环中的定义:

When the conditional expression is absent, it is assumed to be true. You may have an initialization and increment expression, but C programmers more commonly use the for( ; ; ) construct to signify an infinite loop.

为了更好的理解,您可以关注For Looop

关于c - 为什么 for (1; 2; 3) 是无限循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32224959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com