current = current * 10 + (c - '0');

后端 未结 4 1645
鱼传尺愫
鱼传尺愫 2021-01-21 11:58

I\'m trying to read unknown number of integers by this piece of code:

while (1) {
        int c = getchar ();
        if (c == EOF)
            break;
        el         


        
4条回答
  •  旧巷少年郎
    2021-01-21 12:56

    c - '0' is a rudimentary way of turning a single ASCII digit into an integer.

    For example, if c is equal to '9', then its integer value is 0x39. The ASCII value for '0' is 0x30, so, 0x39-0x30 == 0x09 which is equal to the integer value 9.

    Here is the ASCII table for digits:

    chr  hex   dec
    '0'  0x30  48
    '1'  0x31  49
    '2'  0x32  50
    '3'  0x33  51
    '4'  0x34  52
    '5'  0x35  53
    '6'  0x36  54
    '7'  0x37  55
    '8'  0x38  56
    '9'  0x39  57
    

提交回复
热议问题