What you need to reset the arrays after the declaration and the constant.
#include <stdio.h> main() { int c, i, nwhite, nother; int ndigit[10]; nwhite = nother = 0; for (i = 0; i < 10; ++i) ndigit[i] = 0; while ((c = getchar()) != EOF) if (c >= '0' && c <= '9') ++ndigit[c - '0']; else if (c == ' ' || c == 'n' || c == 't') ++nwhite; else ++nother; printf("digits="); for (i = 0; i < 10; ++i) printf(" %d", ndigit[i]); printf("white space = %d, other =%dn", nwhite, nother); }
The fact is that I cannot explain this moment for myself
for (i = 0; i < 10; ++i) ndigit[i] = 0;
Why do we recalculate the cells of the array from 1 to 10, and then reset them further? And didn't it have to declare an array of 11 variables, since one cell is reserved for zero? The program itself is taken from Kernigan and Richie and is engaged in counting numbers.