![](/img/trans.png)
[英]Why is the return type of isdigit() int instead of boolean?
[英]Why does isdigit() return 2048 if true?
任何人都可以解释为什么isdigit
返回2048
如果是真的? 我是ctype.h
库的新手。
#include <stdio.h>
#include <ctype.h>
int main() {
char c = '9';
printf ("%d", isdigit(c));
return 0;
}
因为它被允许。 C99标准仅对isdigit
, isalpha
等说:
当且仅当参数
c
的值符合函数描述中的值时,此子句中的函数才返回非零(true) 。
至于为什么在实践中发生这种情况,我不确定。 在猜测中,它使用与所有is*
函数共享的查找表,并屏蔽除特定位位置之外的所有位置。 例如:
static const int table[256] = { ... };
// ... etc ...
int isalpha(char c) { return table[c] & 1024; }
int isdigit(char c) { return table[c] & 2048; }
// ... etc ...
因为没有标准文档来定义如何用指定的数字表示bool,而对于C语言,非零是真,零是假。 所以这取决于实际的实施。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.