They key to understanding this problem is in this statement: ++ndigit[c-'0'];
The program begins by creating an array:
int ndigit[10]; /* creates an array of size 10 */
In c
when you create an array, you just get a block of memory. What's in that memory is undefined. In more modern languages you may get an array of 10 zeros, but in c
you could have garbage data in there when you first create it.
Walking through you code you should be able to identify how the program prepares the array for use. It makes sure each value at each index in the array is set to 0
.
At this point your array and its indices looks something like this:
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] /* array */
0 1 2 3 4 5 6 7 8 9 /* indices */
So if you were to run ndigit[0]
you would get back the value at index 0
, which is 0
c = getChar()
is getting 1 character from the input (the numbers you typed). But at this point the program just sees those numbers as type char
, not int
. This is where you need to understand ASCII
. Your first char
input is '3'
. This has an ASCII
value of 51
. The program performs this math: '3'-'0'
. It is subtracting two chars
. This is possible because c
is just subtracting the ASCII
value of these chars
which is really 51-48
which equals 3
.
The ++
operator increments a value: ie. ++0
increments 0
to 1
. I won't get into pre
vs post
increment because you will learn that later.
Now that you have all of that, my suggestion is to draw a picture of what the array ndigit
with its values and indices and the value of c
(the current char
) looks like at the end of each iteration of the while
loop. Do this for each iteration of the while
loop and you will see why the output you are seeing makes sense.
ie:
c = 3
[ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 ] /* array */
0 1 2 3 4 5 6 7 8 9 /* indices */
c = 4
...
...
/* and so on */
Here's a link to a useful ASCII
table guide: http://www.asciitable.com/