首页 新闻 会员 周边

下面的一段c语言代码不应该是死循环吗?循环是怎样结束的

0
[待解决问题]

include <stdio.h>

/* 函数声明 */
void func1(void);

static int count=10; /* 全局变量 - static 是默认的 */

int main()
{
while (count--) {
func1();
}
return 0;
}

void func1(void)
{
/* 'thingy' 是 'func1' 的局部变量 - 只初始化一次

  • 每次调用函数 'func1' 'thingy' 值不会被重置。
    */
    static int thingy=5;
    thingy++;
    printf(" thingy 为 %d , count 为 %d\n", thingy, count);
    }

[root@localhost c_存储类_demos]# static存储类.out
thingy 为 6,count 为 9
thingy 为 7,count 为 8
thingy 为 8,count 为 7
thingy 为 9,count 为 6
thingy 为 10,count 为 5
thingy 为 11,count 为 4
thingy 为 12,count 为 3
thingy 为 13,count 为 2
thingy 为 14,count 为 1
thingy 为 15,count 为 0

上述循环为什么是在count=0的时候结束了循环,不应该是继续循环下去吗
是因为while中的count是循环变量吗?

Hssenberg的主页 Hssenberg | 菜鸟二级 | 园豆:208
提问于:2021-06-25 16:08
< >
分享
所有回答(2)
0

while (count--) {

}

你while为false 自然出去了

人间春风意 | 园豆:2335 (老鸟四级) | 2021-06-25 16:26
0

0就是false
count=0时,while(0)就是while(false),跳出循环

路小乙 | 园豆:469 (菜鸟二级) | 2021-06-25 17:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册