-
very beginner's question
Hello all,
I am not new to programming on high level but very new to low level programming and having very tough time understand K&R book example at very begining.
Can someone please clarify why below has to be subtracting 0 from the digits in the array?
I tried just c as subscripts and does not work. It just prints 0..........
When I look at that array, I see no benefit by subtracting 0 from digit I find in file..(at least to my naked untrained eyes)
Please let me know.
thank you!!!
Code:
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']; ****/
++ndigit[c];
else if ( c == ' ' || c == '\n' || c == '\t' )
++nwhite;
else
++nother;
printf("digits =");
for ( i = 0; i < 10; ++i )
/* printf("%d =====> ",i); */
printf("%d\n", ndigit[i] );
printf(", white space = %d, other = %d\n",
nwhite, nother);
}
-
The idea is to translate the value of the character, e.g., in ASCII, to its value as an integer, i.e., a value from 0 to 9, and thus the result can be used as an index of the array of 10 integers, since the valid indices for that array is from 0 to 9.
-
It's not subtracting 0, it's subtracting '0'--that is, the character zero whose value is decidedly not zero. Note that in C, you use single quotes to create character constants, such as '\n' in your example code.
Characters in C are nothing but numbers. The character 'A', for example, is commonly equivalent to the number 65. You can test this with something like printf("%c\n", 65), although 65 is not absolutely guaranteed to be 'A'.
It's just convenient for us to be able to type in 'A' instead of e.g. 65, but they mean the same thing.
So you also have the characters '0', '1', '2', ... '9'. '0' thus has a number associated with it; commonly 48. If you interpret the number 48 as a character, it prints out a zero. Again, printf("%c\n", 48). If you do: char x = '0' then you're giving x the value of 48. There is no difference between char x = 48 and char x = '0'. The second form is just more convenient when we're dealing with characters (again, 48 isn't necessarily '0', but the idea is the same no matter what value '0' has on a particular system).
Anyway, there is a specific property of digits in C: '0' + 1 gives you '1'. That is, if '0' is 48, then '0' + 1 is 49; 49 and '1' are the same thing. So '1' + 1 is '2', or 50. And so on. What this means is that if you subtract '0' from any digit (that is, from '0' to '9'), you get the value of that digit. '2' - '0' is 2, for example. '5' - '0' is 5. The code in K&R uses this to convert digits to array subscripts.
Their array has only 10 elements, so they can use ndigit[0] to ndigit[9]. If the user types in 5, the program stores the value '5', not 5! That is, it stores the character five, not the actual value, or number five. If '5' is 53, then ndigit['5'] is clearly out of the bounds of the array. However, '5' - '0' is 5, and ndigit[5] is fine.
A simple program might help illustrate this:
Code:
#include <stdio.h>
int main(void)
{
int i;
char c = '0';
for(i = 0; i < 10; i++)
{
printf("Value: %d -- Character: %c\n", c, c);
c = c + 1;
}
return 0;
}
-
Also note how the book has conveniently removed the return type from main, while it really shouldn't: http://cpwiki.sourceforge.net/Implicit_main
-
oh wow... very easy and very very just CLEAR.
thank you so much..
-
i dont think u should start with K&R but it might help u to move into next level fast but if u wanna understand good try a simple book then move to K&R.
-
Hi, can you recommend me a book for such case?
I do find K&R very tough to comprehend... but at the sametime, finding perfect book for C is very hard.
I probably am not trying very hard and I really should.
But I have to admit the learning curve for C is very steep.
I have been programming Perl for quite a while now and I can high level program fairly well. It's the low level stuff I want to get into now and I have tough time going through c book.
thank you in advance!!