I put down teach yourself C in 21 days and have started over w/ the
K&R white book.
I'm trying to make sense of some code:
I don't understand why the -'0' is there.
int main (void)
int c, i, nwhite, nother;
nwhite = nother = 0;
for ( i = 0; i < 10; ++i )
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0']; //THIS LINE RIGHT HERE
else if ( c == ' ' || c == '\n' || c == '\t')
printf("digits = ");
for( i = 0; i < 10; ++i)
printf(" %d", ndigit[i]);
printf(", whitespace = %d, other = %d\n",
I understand that a character written between single quotes represents an integer value equal to the numerical value of the character in the machine's character set.
so '0' tranlates to 48
'1' translates to 49
if c = '1' then
c-'0' translates to 49-48 would equal 1
Why can't we use a 1 and a 9 in the code w/out the ' and having to subtract the value of '0' from it?
we define c as in int in the first line!
I hope i'm making sense.