这是我从哪里学习静态变量的代码。while循环和静态变量
#include <stdio.h>
/* function declaration */
void func(void);
static int count = 5; /* global variable */
main() {
while(count--) {
func();
}
return 0;
}
/* function definition */
void func(void) {
static int i = 5; /* local static variable */
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
我查询时count
值等于0
为什么循环停止?为什么它不会走向负面的无限?
也许看看'while(0)'做些什么? – juanchopanza
http://en.cppreference.com/w/cpp/language/ while – SingerOfTheFall
寻找'while'循环的定义。直到条件为“假”或“0”。 – Gravell