温馨提示×

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

c++
小亿
98
2024-03-15 18:15:57
栏目: 编程语言

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.

0