[英]Why is it OK to pass mismatch argument into isdigit function?
I'm new to C, sorry if my question is a little bit strange.我是 C 新手,如果我的问题有点奇怪,请见谅。
the signature of isdigital
function is: isdigital
函数的签名是:
int isdigit(int arg);
but I saw a lot of cases that a char can also be argument, for example:但是我看到很多情况下 char 也可以作为参数,例如:
char c = '5';
printf("Result when numeric character is passed: %d", isdigit(c));
c
is a char, not an interger, how can it can be used in this way? c
是char,不是interger,怎么能这样用呢? does implicit casting from char
to int
occur here?这里会发生从
char
到int
隐式转换吗?
c
is achar
, not an integer, how can it can be used in this way?c
是一个char
,而不是一个整数,怎么可以这样使用呢?
The char
is converted to an int
. char
被转换为int
。 When char
is signed , this is no problem - the value converts without issue.当
char
被签名时,这没问题 - 值转换没有问题。
When char
is unsigned , except on rare machines, again no issue as all values of unsigned char
exist in int
.当
char
是unsigned 时,除了在罕见的机器上,也没有问题,因为unsigned char
所有值都存在于int
。
Th trick is that is...(int)
functions are well defined for int
values in the unsigned char
range and EOF
(some negative value), but not all negative values.技巧是
is...(int)
函数为unsigned char
范围和EOF
(一些负值)中的int
值定义了良好,但不是所有负值。 Best for char
to be cast to the unsigned char
first.最佳
char
将被强制转换为unsigned char
第一。
char c = '5';
// printf("Result when numeric character is passed: %d", isdigit(c));
printf("Result when numeric character is passed: %d", isdigit((unsigned char) c));
Note: the return value of isdigit(int)
and friends are zero (not a digit) or some non-zero value (is a digit).注意:
isdigit(int)
和friends 的返回值是零(不是数字)或一些非零值(是数字)。 The exact non-zero value is not so important as much as it is non-zero (true).确切的非零值并不像非零(真)值那么重要。
does implicit casting from char to int occur here?
这里会发生从 char 到 int 的隐式转换吗?
No, type conversion occurs.不,会发生类型转换。
The type char
gets converted into a higher-order datatype int
implicitly during compilation.在编译期间,类型
char
被隐式转换为高阶数据类型int
。 But after the conversion, it is usually becomes the ASCII-number of the character is stored in it, eg, for 'A', it is 65, no longer a char type.但是转换后,它通常会变成存储在其中的字符的ASCII-number,例如,对于'A',它是65,不再是char类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.