I'm currently reading the K&R book. Page 22, Arrays 1.6
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 = %d\n", nwhite, nother); It says chars are considered integers. I tried removing the '0' in the first if satement. However, the array stopped working. If chars are considered to be integers, why is the '0' required for the program to function properly
0is not'0'c-'0'is the short-hand way to get the digit from the ASCII digit. Since '0' is the lowest ASCII value, and digits are continuous in the ASCII encoding, subtracting'0'from a digit character gives you its value.