【发布时间】:2015-06-06 05:20:14
【问题描述】:
虽然我对C有基本的了解,但我遇到了以下代码。我仍然了解以下代码的逻辑。
循环如何终止以及循环何时终止——在什么条件下?
最后
counter变为零,但是为什么循环没有继续执行-1,-2而是终止了?
代码:
#include <stdio.h>
void func(void);
static int count = 5;
int main()
{
while (count--)
{
func();
}
return 0;
}
void func(void)
{
static int i = 5;
i++;
printf("i is %d and count is %d\n", i, count);
}
输出:
i is 6 and count is 4
i is 7 and count is 3
i is 8 and count is 2
i is 9 and count is 1
i is 10 and count is 0
【问题讨论】:
-
while(0) 将评估为假。这将打破 while 循环
标签: c termination