CKTIS / C++
std::isalpha — checking whether a character is a letter
std::isalpha from <cctype> checks whether a value represents a letter under the current C locale. It returns int: zero or a nonzero value. To classify a char value, convert it to unsigned char and pass the result only if it fits in int.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
#include <cctype>
#include <climits>
// Signature: int std::isalpha(int c);
const auto byte = static_cast<unsigned char>(ch);
if (byte <= INT_MAX) {
const bool letter = std::isalpha(static_cast<int>(byte)) != 0;
// Use letter here.
} else {
// This byte cannot be passed to std::isalpha as its original value.
}std::isalpha from <cctype> classifies one character value using the current C locale's LC_CTYPE category. It returns int: a nonzero value for a letter and 0 otherwise. Test != 0, not == 1 — a true result is not required to equal 1.
The value passed to the int parameter must equal EOF or be representable as unsigned char; other values cause undefined behavior. For a variable char ch, converting only to int does not repair a negative char value. First compute static_cast<unsigned char>(ch). The call std::isalpha(static_cast<unsigned char>(ch)) is safe for any ch if UCHAR_MAX <= INT_MAX. If UCHAR_MAX > INT_MAX, check that the converted value is at most INT_MAX before converting it to int (both limits are defined in <climits>). A value above INT_MAX cannot be passed unchanged: the conversion can yield EOF or another negative value that causes undefined behavior. Do not call the function for such a byte. Keep the result of an input function returning int as int, and check for EOF before converting the byte.
In the "C" locale, letters are A–Z and a–z; other locales may recognize additional letters. The function does not decode UTF-8: classifying individual bytes does not determine whether a multibyte Unicode character is a letter.
Do not confuse this function with the two-argument std::isalpha(ch, loc) from <locale>, which returns bool and uses an explicit locale object. The example below assumes that its values fit in int after conversion to unsigned char. It sets LC_CTYPE to "C" and prints the result after normalization to bool, so the expected output does not depend on the library's particular nonzero return value.
Examples
#include <cctype>
#include <clocale>
#include <initializer_list>
#include <iostream>
int main() {
if (std::setlocale(LC_CTYPE, "C") == nullptr) {
return 1;
}
for (char ch : {'A', 'z', '7', '_'}) {
const bool letter =
std::isalpha(static_cast<unsigned char>(ch)) != 0;
std::cout << ch << ": " << letter << '\n';
}
}
Result
A: 1 z: 1 7: 0 _: 0
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.