C语言中判断字符是否为数字时为什么要用单引号括起来?

 #include <stdio.h>

main()
{
    int c, i, nwhite = 0, nother = 0;
    int ndigit[10];

        for (i = 0; i < 10; i++)
        ndigit[i] = 0;

    while( (c = getchar()) != EOF)
        if (c >= '0' && c <= '9')
            ndigit[c - '0']++;
        else if (c == ' ' || c == '\n' || c == '\t')
            nwhite++;
        else
            nother++;

    printf("digits =");
    for (i = 0; i < 10; i++)
        printf(" %d", ndigit[i]);
    printf(", white space = %d, other = %d\n", nwhite, nother);
}

这段代码里,判断输入的字符是否为数字时,为什么不能去掉‘0’和‘9’的单引号?以及下面c既然已经是数字了,为什么不可以直接写ndigit[c],而要写成ndigit[c - '0'] ?试了一下好像确实不可以,但不到是为什么。萌新求教。

因为判断的不是是否为数字,而是是否为表示数字的字符。你从键盘输入,输入的是字符,不是数字。

输入的是字符
字符和字符比较的是ascll码
去掉单引号的话,0-9表示的实际是ascll表上的前十个字符。

学习一下ascll码表
0 和'0' 是两种值

从键盘接收到的输入为字符,字符'0'-'9'对应的Ascii码为48-57。
如果直接写成ndigit[c],对应即为ndigit[48]-ndigit[57],显然不对。

字符的0不等于数值的0

数字与字符的差别。。。请查看ASCII值。。。

可以学习下ascii码是什么意思!!