Cコード奇妙な行動
#include <stdio.h>
int main()
{
int c , nother , new , ndigits [10] , white, tabs ;
for (int i = 0 ; i< 10 ; i++)
ndigits[i] = 0 ;
while ((c = getchar())!= EOF)
{
switch (c)
{
case '0' : case '1' : case '2' :
case '3' : case '4' : case '5' :
case '6' : case '7' : case '8' :
case '9' :
ndigits[c- '0' ]++ ;
break ;
case ' ' :
printf("w"); /*to see how many spaces */
white++ ;
case '\t' :
printf("t");
tabs++;
case '\n' :
printf("n");
new++ ;
break ;
default :
nother++ ;
break ;
}
}
printf ("digits = ") ;
for (int i = 0 ; i < 10 ; i++)
printf ("%d" , ndigits[i]) ;
printf (",tabs = %d , new line = %d, spaces = %d , other = %d ",
tabs, white , new , nother);
return 0 ;
}
私はそれが
GCC
を使用して、ちょうどそれが印刷さCtrl + z
を押してコンパイル桁= 00000、タブ= 4200912、新しい行= 4194432、スペース= 2293540 その他=これらの数から来る2147307520
?
私は再びそれをコンパイルして
HELLO HELLO HELLO
を入力して を入力してクリックして、それがwtnwtnwnn
- を出力する理由(それは3つのタブをカウントする理由予想より3 nは、そこにある)ということでしょうか?
を有効に警告してコンパイル: 'gccの-Wallコードを。c ' –
' ndigits [i] 'をゼロに初期化しました。 「白」、「タブ」などはどうですか? – dasblinkenlight
はい私は他の変数を初期化することを忘れています:) しかし、 私は再びコンパイルし、HELLO HELLO HELLOと入力して、それを入力して、wtnwtnwnnを印刷します。 なぜですか(3 nは予想よりも3つ多いです)。 –