在下面的C代碼(即由一個例子以K &ř啓發),可變nw
被視爲未初始化:變量被視爲未初始化
#include <stdio.h>
#define IN 1
#define OUT 0
main(){
int c,nl,nw,nc = 0;
int state = OUT;
while((c=getchar())!=EOF){
++nc;
if(c=='\n')
++nl;
if(c==' ' || c=='\n' || c=='\t')
state = OUT;
else if(state == OUT){
state = IN;
++nw;
}
}
printf("%d %d %d\n" , nl, nw, nc);
}
例如:
input: one two three
output: 1 796411731 14
中間的數字應該是3(nw
=單詞數量)
我不明白爲什麼會發生這種情況,因爲我明確地暗示了在開始時給它一個值。