Hi again all
I'm working through The C Programming Language, and I'm getting stuck on some array stuff
There's one line in a program that I don't get. I've highlighted it in the following:
The book says
//counts digits, white space and others//
int c, i, nwhite, nother;
int ndigit; //ndigits is an array of 10 integers//
nwhite = nother = 0;
for (i=0; i<10;++i) // setting a value for each spot in the array//
ndigit[i] = 0;
if (c >= '0' && c<= '9')
else if (c == ' ' || c == '\n' || c=='\t')
for (i=0; i<10; ++i)
printf(" %d", ndigit[i]);
printf(", white space = %d, other = %d\n",
if (c>= '0' && c <= '9')
determins whether the character in c is a digit. If it is, the numeric value of that digit is
c - '0'
this only works if '0', '1' ..., '9' have consecutive increaing values. Fortunately, this is true for all character sets.
I don't get the purpose of "c - '0'" is, since '0' = 0, so you're not doing anything to c (?).
I also don't get what it means when it is talking about consecutive increasing values. Does it mean that I can't have my array be
s[3,2,5,4,1,7,6,5], and that is has to be s[0,1,2,3,4,5,6,7,8,9]?
Any advice would really help, I think this is stopping me from making progress in later sections of the book.
Thanks in advanced!