6

The questions says it all:

...
int ndigit[10];
...//fill in the array with 0s

while((c = getchar()) != EOF)
    if(c >= '0' && c <= '9')
         ++ndigit[c - '0']; //<== unable to understand this part

supposedly, the array stores incoming digit chars from the input stream...

2
  • 10
    Just as a side note to all replies that mention ASCII. ISO C doesn't guarantee that character set is ASCII; however, as far as I remember, it does guarantee that, no matter what integer values the digit characters '0' ... '9' have, they are consecutive - so if c is a char which is a digit, (c - '0') is a portable way to retrieve that digit's value. Commented Aug 17, 2009 at 22:49
  • 1
    Even EBCDIC with its alphabet broken into three spans, kept the digits at consecutive code points. So does CDC Display Code and DEC Rad-50. Apparently Baudot left almost nothing in order, preferring to have encodings that made some kind of sense if a paper tape was mounted backwards. Since Unicode adopts ASCII as the first 128 code points, it too keeps the digits consecutive. Commented Aug 18, 2009 at 6:17

9 Answers 9

13

In C, you can do arithmetic on characters using their character codes. So this makes sure that you have a digit, finds out which digit it is (by measuring its difference from zero) and then increments a count in the corresponding position in the array. When it's done, ndigit[0] will contain the number of occurrences of '0', ndigit[1] will contain the number of occurrences of '1', and so on.

Sign up to request clarification or add additional context in comments.

Comments

8

It is creating a histogram of the characters 0-9. "c- '0'" turns the value from getchar() into an integer, which acts as the index for the array. This index corresponds to the numbers 0-9. It then increments that array location. Thus, once it has completed running, the array consists of the repetitions for the characters 0-9.

So 0123456789 should result in an array of all ones. 0123333 should result in an array with the values 1114000000.

Comments

7

The character 0 is different from the number 0.

In ASCII, the character '0' is at position 48. The standard guarantees that in the character encoding, the numbers must be sequential (I do not know where in the standard this is said). That is, just like 1 comes after 0, '1' will come after '0'. Therefore, if you have entered '0', and you want to get 0, subtract '0' from it. '1' minus '0' will have a difference of 1. And so on.

Comments

2

Both POSIX and ISO C require:

The encoded values associated with the digits 0 to 9 shall be such that the value of each character after 0 shall be one greater than the value of the previous character.

Comments

1

getchar() is going to return the character code for the character as an int. Check out an ASCII chart such as: http://www.cs.utk.edu/~pham/ascii_table.jpg.

So if you enter '0' c is going to be 48. Subtracting '0' from the input value is just like subtracting 48, So you'll end up with the int values 0..9 in your integer array

Comments

1

c - '0' converts the character from its ASCII code to the value itself. That becomes the array index. The array subscript operator has a higher precedence than the preincrement, so the value in the array at the resulting index will be incremented.

Comments

1

The part [c - '0'] is creating a zero-based index for ndigit[]. It does this by taking c (which has an ASCII value in the range 48 to 57) and subtracting 48 (ASCII value of '0')

Comments

1

Here there using ASCII value subtract Which means '0' => ASCII value is 48 and '1' => ASCII value is 49.

printf("%d",'0');  // 48 value
printf("%d",'1');  // 49 value 
....
printf("%d",'9');  // 57 value 

And just subtract this value.

when c is 1

[c - '0'] =1;

[49-48] = 1;

Comments

0

This is a incremental counter of inputed symbols from '0' to '9'.

e.g.

if u put '1' twice when ndigit[1] will 2, if u put '5' once when ndigit[5] will 1,
If u put '0' 5000000 times when ndigit[0] will 5000000 =)

... etc.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.