简体   繁体   English

为什么我们需要“ -'0'”来修改数组?

[英]why we need “-'0'” to modify array?

This is code from C by Dennis Ritchie, chapter "Array": 这是Dennis Ritchie的C语言代码,“数组”一章:

#include <stdio.h>
/* count digits, white space, others */
main()
{
    int c, i, nwhite, nother;
    int ndigit[10];
    nwhite = nother = 0;
    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);
}

Why do we need -'0' in this line? 为什么我们在这一行中需要-'0'

++ndigit[c-'0'];

If I change it to ++ndigit[c] , the program doesn't work properly. 如果我将其更改为++ndigit[c] ,则该程序无法正常运行。 Why can't we just write ++ndigit[c] ? 为什么我们不能只写++ndigit[c]

I already read the explanation of the book, but I don't understand it. 我已经读过这本书的解释,但我听不懂。

This works only if '0', '1', ..., '9' have consecutive increasing values. 仅在“ 0”,“ 1”,...,“ 9”具有连续递增的值时才有效。 Fortunately, this is true for all character sets. 幸运的是,所有字符集都是如此。 By definition, chars are just small integers, so char variables and constants are identical to ints in arithmetic expressions. 根据定义,char只是小整数,因此char变量和常量与算术表达式中的int相同。 This is natural and convenient; 这是自然而方便的; for example c-'0' is an integer expression with a value between 0 and 9 corresponding to the character '0' to '9' stored in c, and thus a valid subscript for the array ndigit 例如c-'0'是一个整数表达式,其值在0到9之间,与c中存储的字符'0'到'9'相对应,因此是数组ndigit的有效下标

to understand why we need "-'0'" you first need to understand ASCII table - http://www.asciitable.com/ 要了解为什么我们需要“ -'0”,您首先需要了解ASCII表-http: //www.asciitable.com/

now you need to understand that every character in C is represented by a number between 0 and 127 ( 255 for extended ). 现在您需要了解C中的每个字符都由0到127之间的数字表示(其中extended为255)。

for example if you'll print the character '0' for his numeric value: 例如,如果您要为数字值打印字符“ 0”:

printf( "%d", '0' );

output: 48 输出:48

now you've declared an array of size 10 - ndigit[ 10 ] , where the n cell represent the number of times the number n was given as input. 现在,您已经声明了一个大小为10- ndigit[ 10 ]的数组,其中n单元格表示将数字n作为输入给出的次数。

so if you receive '0' as input you'd want to do ndigit[ 0 ]++ so you need to convert from char to integer. 因此,如果您收到“ 0”作为输入,则需要执行ndigit[ 0 ]++因此您需要从char转换为integer。 and you can do that by subtracting 48 ( = '0' ) 你可以减去48(='0')

thats why we use the line ++ndigit[c-'0']; 那就是为什么我们使用++ndigit[c-'0'];

if c = '5', we will get 如果c ='5',我们将得到

++ndigit['5' - '0']

++ndigit[ 53 - 48 ]

++ndigit[ 5 ]

exactly like we wanted it to be 就像我们想要的那样

c = getchar() will store the character code read to c , and it is differ from the integer that the character stands for. c = getchar()将存储读取到c的字符代码,它与字符代表的整数不同。

Quote from N1256 5.2.1 Character sets 引用N1256 5.2.1字符集

. In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous. 在源基本字符集和执行基本字符集中,上述十进制数字列表中的每个0后面的字符的值应比前一个的值大1。

As this shows, the character codes for decimal digits are continuous, so you can convert the character code of decimal digits to the integer that the characters stand for by subtracting '0' , which is 0 's character code, from the character code. 如此所示,十进制数字的字符代码是连续的,因此您可以通过从字符代码中减去'0' (即0的字符代码)将十进制数字的字符代码转换为字符所代表的整数。

In conclusion, c-'0' yields the integer that the character in c stands for. 总之, c-'0'产生c字符代表的整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM