我工作到K & R的第一章,來到你應該在哪裏創建字長的柱狀圖一些輸入練習。我開始嘗試使用while循環來創建一個只有最長單詞的數組,但輸入的單詞超過六個字符會導致程序凍結。我對解決問題的興趣不大,但我知道原因。C程序凍結的字長度大於6
#include <stdio.h>
main()
{
int c, i, l, max;
int length[max];
l = max = 0;
while((c = getchar()) != EOF){
if(c != ' ' && c != '\t' && c != '\n'){
++l;
if(l > max)
max = l;
else
;
}
else
l = 0;
}
for(i = 0; i < max; ++i)
length[i] = 0;
for(i = 0; i < max; ++i)
printf("\n%d", length[i]);
putchar('\n');
}
如何這甚至編譯?數組的大小未初始化。 – CroCo