CKTIS / C++
std::isspace — recognizing whitespace
std::isspace from <cctype> recognizes whitespace using the current C locale. When passing char values, ensure that the argument is in the permitted range.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
#include <cctype>
int std::isspace(int c);std::isspace from <cctype> returns nonzero for whitespace and 0 otherwise; do not assume it returns 1. In the "C" locale it recognizes space, \t, \n, \v, \f, and \r.
The argument must equal EOF or be representable as unsigned char; otherwise behavior is undefined. For any char value, std::isspace(static_cast<unsigned char>(ch)) is safe if UCHAR_MAX <= INT_MAX (both limits are available in <climits>). If UCHAR_MAX > INT_MAX, first store the result of static_cast<unsigned char>(ch) in u and call std::isspace(static_cast<int>(u)) only if u <= INT_MAX. Handle a larger value separately: converting it to int can yield EOF instead of the character value or a different negative value for which the call has undefined behavior. Check an input result stored as int against EOF before conversion.
Classification depends on the current C locale; the function does not decode UTF-8. The separate std::isspace(ch, loc) overload from <locale> uses an explicitly supplied std::locale object and returns bool.
Examples
#include <cctype>
#include <climits>
#include <clocale>
#include <iostream>
int main() {
static_assert(UCHAR_MAX <= INT_MAX, "This example requires UCHAR_MAX <= INT_MAX.");
std::setlocale(LC_CTYPE, "C");
const char text[] = "A B\tC\n";
int count = 0;
for (char ch : text) {
if (std::isspace(static_cast<unsigned char>(ch)) != 0) {
++count;
}
}
std::cout << count << '\n';
}
Result
3
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.