-4

I have the following code snippet:

int ndigit[10] = {0,0,0,0,0,0,0,0,0,0};

void count() {
    char c;
    while (cin.get(c)) {
    if (c>='0' && c<='9') {
        ndigit[c-'0']++;
    }
}

So c has to be between 0 and 9 (with 0 and 9), so why it is necessary to index the array like ndigit[c-'0'] instead of just ndigit[c] ?

If I am right, in both cases a c value can occur more then once, so a the value of ndigit[c-'0'] gets overwritten anyway...

I appreciate your wisdom!

goulashsoup
  • 2,639
  • 2
  • 34
  • 60

2 Answers2

2

when you type the character 0 as input to this program you will get a byte with the hex value x30 (see http://www.asciitable.com/)

To access the first element of the array you need x00 (binary 0). Subtracting x30 from the typed char will give you 0.

The logic works because the ASCII codes for 0-9 are x30,x31,..x39. So subracting 0x30 (or '0' same thing) will give you 0-9

pm100
  • 48,078
  • 23
  • 82
  • 145
  • 4
    Assuming ASCII encoding or similar: not necessarily true. Note that both the C++ and C standards insist that encodings have `0` to `9` in order and in a single block. – Bathsheba Nov 01 '16 at 19:07
  • of course could be a non ascii machine - but my 10c says that it is – pm100 Nov 01 '16 at 19:09
  • i know it works with any encoding but showing real likely values makes it clearer – pm100 Nov 01 '16 at 19:09
  • Subtracting `'0'` will work whatever the encoding scheme, as numerals are required to be consecutive. Please do not use magic numbers. – Weather Vane Nov 01 '16 at 19:10
  • @WeatherVane senior stack users such as yourself should let us newbs handle the small and easy questions, please :) – NeoR Nov 01 '16 at 19:32
  • @NeoR it was a comment not my answer but feel free to post an answer yourself (edit: too late now)! I am no senior (a fraction of their rep) but learning something every day here. You can think you know something, then it becomes obsolete, but I believe numerals are still consecutive, unlike alphabet which becomes increasingly complicated with globalisation ;-) – Weather Vane Nov 01 '16 at 19:45
  • @WeatherVane You are senior atleast to me and well lets leave the senior most members with greater than 100k rep as far as possible but still even users with 10k points should let us newbs handle the small talk among ourselves. – NeoR Nov 01 '16 at 19:50
  • @weathervane: you hunch on the numerals is correct. But note that EBCDIC encoding has been around for donkeys years and that has non consecutive alphas. – Bathsheba Nov 01 '16 at 19:50
  • @NeoR I don't know what you mean - you could have posted comments too. – Weather Vane Nov 01 '16 at 19:51
  • Oh well that's true about globalization – NeoR Nov 01 '16 at 19:51
  • I just meant I have seen you on other such small easy going questions also :) – NeoR Nov 01 '16 at 19:52
  • @Bathsheba it was not a hunch. And I made no statement about ASCII, although I hope and pray that EDCBIC will soon be forgotten - as if! And some of them *are* consecutive. – Weather Vane Nov 01 '16 at 19:53
  • @NeoR I repeat - if you can answer or comment yourself then do so, instead of critising other commenters. – Weather Vane Nov 01 '16 at 19:56
  • @Bathsheba EBCDIC (my first systems) have consecutive digit codes 0xF0- 0xF9 – pm100 Nov 01 '16 at 20:28
  • @NeoR just remember that many new members already have vastly superior knowledge to me, especially recent knowledge. Is that you? Some very high reps think they know more than new SO members. – Weather Vane Nov 01 '16 at 20:28
  • @WeatherVane definitely can't say that I am superior, although definitely just a smart and hard working creative thinker. – NeoR Nov 01 '16 at 20:35
1

"So c has to be between 0 and 9"

No! c has to be between '0' and '9', and it makes the difference.

Every character has appropriate numeric value (ASCII code), e. g.:

  • 'A' has 65
  • 'B' has 66
  • 'a' has 98

In C language characters are simply numbers, e. g. 'A' + 'B' is a perfectly valid expression and means 65 + 66.

If we want display or read a digit (0, 1, ..., 9), we actually use its symbolic representation, i. e. an character ('0', '1', ..., '9'). And these characters amazingly have not ASCII values 0, 1, ..., 9, but 48, 49, ... 57 - they are all shifted by 48.

So for converting a digit symbol, e. g. '7' (which has ASCII value 55 - as 7 + 48) into a number which we people see in it (i. e. 7 - without apostrophes), we need simply subtract this shifting number 48 from its ASCII value:

    (7 + 48) - 48

which is the same as to subtract '0' (= 48) from '7' (=55):

    '7' - '0'

and witch is exactly what does the expression

    c - '0'

in your code.

MarianD
  • 13,096
  • 12
  • 42
  • 54