CKTIS / C++
std::isdigit — checking for digits
std::isdigit from <cctype> checks whether a character is a decimal digit. It returns int, not bool. After converting a character to unsigned char, the resulting value must fit in int.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
#include <cctype>
// Signature: int std::isdigit(int c)std::isdigit from <cctype> recognizes digits '0'–'9'. It returns zero for other characters and a nonzero value for digits — not necessarily 1. Test the result with != 0, not == 1.
The value passed to the int parameter must be representable as unsigned char or equal to EOF; otherwise, behavior is undefined. For a variable char c, std::isdigit(static_cast<unsigned char>(c)) is safe for every value of c if UCHAR_MAX <= INT_MAX (both macros are available in <climits>). If UCHAR_MAX > INT_MAX, store the result of static_cast<unsigned char>(c) as u and call std::isdigit(static_cast<int>(u)) only if u <= INT_MAX. Otherwise, do not pass u to this function: conversion to int can yield a negative value other than EOF. Check an input function's int result for EOF before converting it.
This classifies one character; it neither parses a number nor decodes UTF-8. The separate overload std::isdigit(c, loc) from <locale> uses a locale facet; do not confuse it with the one-argument function described here.
Examples
#include <cctype>
#include <climits>
#include <initializer_list>
#include <iostream>
int main() {
for (char c : {'7', 'A', '0'}) {
const auto u = static_cast<unsigned char>(c);
const bool digit = u <= INT_MAX
? std::isdigit(static_cast<int>(u)) != 0
: (c >= '0' && c <= '9');
std::cout << c << ": " << digit << '\n';
}
}
Result
7: 1 A: 0 0: 1
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.