什么是真正的用途使用N [C-'0']的? [英] What's the real use of using n[c-'0']?
问题描述
我在C新手,我整个code来是这样的:
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']
在如果
循环为什么我们要使用左右 0
单引号,有什么用呢,我们为什么不能定义 0
为一个整数直线距离?而在code的第二行 ++ N [C-'0']
,有什么用使用数组像这样,在某种意义上我们为什么需要减去 0
(再次为什么在这种情况下使用单引号的?)在数组索引从 C
?
In if
loop why we have to use single quotes around 0
, whats the use of it, why we can't define 0
as an integer straight away? And in the second line of code ++n[c-'0']
, whats the use of using array like this, in sense why we need to subtract 0
(once again why the use of single quotes in this context?) from c
in the array index?
如果我做这样的 N [C-'0']
,索引操作(结果 C-'0
)将是一个字符或整数?
If i do like this n[c-'0']
, the result of index operation(c-'0'
) will be an character or integer?
由于任何人都可以说我,什么真正使用这种阵列和哪些缺点呢?
Given that can anyone say me, whats the real use of such array and what are the disadvantages well?
先谢谢了。
推荐答案
在C, 0
是一个整数,其价值重估presents数字为零一个字符,而不是0值,这将是空字符。其他答案省略了这一点,但要注意,重要的是C语言任务的十进制数字已经连续值,因此,如果 C
是一个数字, C-'0
是位的数值,即
In C, '0'
is an integer whose value represents the digit zero as a character, not the value 0, which would be the null character. Other answers have omitted this, but it's important to note that the C language mandates that the decimal digits have consecutive values, so that if c
is a digit, c-'0'
is the numeric value of that digit, i.e.
'0'-'0' = 0
'1'-'0' = 1
'2'-'0' = 2
.
.
.
'9'-'0' = 9
这篇关于什么是真正的用途使用N [C-'0']的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!