简体   繁体   English

为什么可以将不匹配参数传递给 isdigit 函数?

[英]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?这里会发生从charint隐式转换吗?

c is a char , 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 .charunsigned 时,除了在罕见的机器上,也没有问题,因为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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM