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

前端 未结 12 727
旧时难觅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:47

    This allows to use a char as an index of an array. For example you could define a string "012345", probably read from an external file, and compute for each character c-'0', which will give the integers 0, 1, 2, 3, 4, and 5 respectively.

提交回复
热议问题