关于c/c++中的isdigit()函数

关于isdigit函数

isdigit函数只能判断字符型是否是数字

如下

#include <iostream>
#include <cstdio>

using namespace std;

int main() {
    char c;//此时输入的是整型数字
    while (cin>>c) {
        if (isdigit(c)) {
            cout<<c<<"是数字"<<endl;
        } else cout<<c<<"不是数字"<<endl;
    }
    return 0;
}

//程序输出如下:

/*
1
1是数字
2
2是数字
3
3是数字
a
a不是数字
b
b不是数字
c
c不是数字
*/

//识别正确
//但是如果把char c 改成int c 则无法识别数字和字符的区别
//更改后的错误程序输出如下:

/*
1
1不是数字
2
2不是数字
3
3不是数字
a

Process returned 0 (0x0)   execution time : 3.267 s
Press any key to continue.
*/


  • 38
    点赞
  • 85
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值