c++中isdigit函数的用法是什么

c++
930
2024/4/7 18:15:57
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

isdigit函数是C++中的一个字符处理函数,用于判断一个字符是否是数字字符。

isdigit函数的原型为:

int isdigit(int c);

参数c是一个单个字符,可以是任何字符类型。如果参数c是一个数字字符(‘0’-‘9’),则isdigit函数返回非零值(通常为1),否则返回0。

示例代码:

#include <iostream>
#include <cctype>

int main() {
    char c = '5';
    if (isdigit(c)) {
        std::cout << c << " is a digit character." << std::endl;
    } else {
        std::cout << c << " is not a digit character." << std::endl;
    }
    
    return 0;
}

输出结果:

5 is a digit character.

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: 如何优化islower函数的性能