What's the real use of using n[c-'0']?

前端 未结 12 708
旧时难觅i
旧时难觅i 2020-12-09 17:54

I\'m a novice in C and I came across the code like this :

int n[10];
if(c>=\'0\' && c<=\'9\')
++n[c-\'0\']

In if

12条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-09 18:46

    First, if is not a loop, it is a statement. There's only going to be one pass through the code.

    That means the first line can read if c is a digit and the second line combines conversion of the ascii digit to an integer digit (and increments the element of the n array to count that digit).

提交回复
热议问题