C++ 标准库 - <cctype>

C++ std::isdigit 判断字符是否为 '0' 到 '9'。返回 int:非 0 为真。

原型

int isdigit(int ch);

参数

  • ch:以 unsigned char 的值传入,或 EOF。

返回值

非 0 表示是数字。

示例

#include <cctype>
#include <iostream>

int main() {
    std::cout << (std::isdigit('3') ? 1 : 0) << " ";
    std::cout << (std::isdigit('a') ? 1 : 0) << "\n";
    return 0;
}

输出:

1 0

注意事项

对可能为负的 char 应先转 static_cast<unsigned char>(c)。

下一页介绍isalpha。