這是我從哪裏學習靜態變量的代碼。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