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

后端 未结 4 1678
鱼传尺愫
鱼传尺愫 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:47

    C specifies the 10 decimal digits, not surprisingly, as

    0 1 2 3 4 5 6 7 8 9

    C11 §5.2.1 further states "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.

    Thus when assessing a string for characters that are digits, the language guarantees that subtracting '0' from a decimal digit char will result in its integer value.

    if (isdigit (c))
      int value = c - '0';
    

    This is not dependent on char using ASCII.

提交回复
热议问题