isdigit('5')结果为0为啥错

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了isdigit('5')结果为0为啥错相关的知识,希望对你有一定的参考价值。

参考技术A int
isdigit(char
c)
检查参数c是否为阿拉伯数字0到9。
若参数c为阿拉伯数字,则返回TRUE,否则返回NULL(0)。
所以
isdigit('5')应该是返回TRUE,也就是1
参考技术B 你传递的参数不对哦,
int
isdigit(int
c)
传入的参数是
0~9
时,
才会返回值为
0,
但是
'5'
是字符,
根据
ASCII
码表知道你写的相当于
isdigit(53),
不在
0~9
范围内,
所以结果是非零值
参考技术C 函数
isdigit(ch)
就是问
ch
是数字吗?如果是结果为1,否则结果为0,
而'5'是一个字符,而不是数字5,所以你说结果应该为什么呢

c/c++isdigit()函数

isdigit函数

isdigit是计算机C(C++)语言中的一个函数,主要用于检查其参数是否为十进制数字字符。

函数定义:int isdigit(int c)
函数说明:检查参数是否为十进制数字字符(判断括号内是否为0~9的数字。)
函数所需头文件:#include<cctype>
返回值:若参数c为阿拉伯数字0~9,则返回非0值,否则返回0。

代码示例

	    //isdigit:判断字符是否为阿拉伯数字0~9
		#include<iostream>
		#include<cctype>
		 
		using namespace std;
		 
		int main()
		
		    string str = "123456789asdfghjkl~!@#$%";
		    for(int i = 0; str[i] != 0; ++i)
		    
		        if(isdigit(str[i]))
		            cout << str[i] << " is  digit" << endl;
		        else
		            cout << str[i] << " is not digit!" << endl;
		    
		    
		    return 0;
		

打印输出

标准输出:0 is  digit
1 is  digit
2 is  digit
3 is  digit
4 is  digit
5 is  digit
6 is  digit
7 is  digit
8 is  digit
9 is  digit
a is not digit!
s is not digit!
d is not digit!
f is not digit!
g is not digit!
h is not digit!
j is not digit!
k is not digit!
l is not digit!
~ is not digit!
! is not digit!
@ is not digit!
# is not digit!
$ is not digit!
% is not digit!


引经据典

https://baike.baidu.com/item/isdigit/9455880?fr=aladdin

以上是关于isdigit('5')结果为0为啥错的主要内容,如果未能解决你的问题,请参考以下文章

isdigit字符串测试函数应用实例

AttributeError: 'int' object has no attribute 'isdigit'(python下的isdigit函数)

将字符串s='ab34aa243dd78eww89' 处理为 '**34**243**78***89',然后对数字求和,结果为'**7**9**15***17

python怎么判断是否数字

匿名函数 递归 推导式

进制转换