c-'0'
is technique to give int value == to char number e.g. 1
for '1'
, 5
for '5'
.
char symbols '0', '1', '2' ..... '9'
are assigned continue encoding values so difference of a numeric char constant with '0'
gives decimal number. (in your compiler for example in ASCII char they are assigned continues acsii values).
So for example in variable c
is '7'
, then c - '0'
== 7
;
In your code array declared as:
int ndigit[10]= {0};
// default initialized with `0`
So index can be from 0
to 9
. So in you code:
++ndigit[c-'0']; // ndigit[c-'0'] = ndigit[c-'0'] + 1;
increments frequency of a number by 1
when at corresponding digit of a number char.