std::isprint(3)
π NAME π
std::isprint β and yes bestie, that is literally the whole name, it's std::isprint, no notes, no subtitle, we move.
β¨ SYNOPSIS β¨
Okay so this one lives in the header <cctype> and the signature is giving minimalist:
int isprint( int ch );
It checks if ch is a printable character as classified by the C locale you currently have installed. In the default, "C" locale, the printable ones are:
- digits (
0123456789) - uppercase letters (
ABCDEFGHIJKLMNOPQRSTUVWXYZ) - lowercase letters (
abcdefghijklmnopqrstuvwxyz) - punctuation characters:
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
- space (
)
π¨ genuine warning, not a vibe: the behavior is undefined if the value ofchis not representable asunsigned charand is not equal toEOF. Like, undefined undefined. Respect it.
πΈ PARAMETERS πΈ
chβ the character you want classified. That's it. That's the whole job.
π₯ RETURN VALUE π₯
Non-zero value if the character can be printed, zero otherwise. Simple queen behavior.
π¦ NOTES π¦
Like all the other functions from <cctype>, the behavior of std::isprint is undefined if the argument's value is neither representable as unsigned char nor equal to EOF. So to use these functions safely with plain chars (or signed chars), convert the argument to unsigned char first:
bool my_isprint(char ch)
{
return std::isprint(static_cast<unsigned char>(ch));
}
Same energy for standard algorithms: lowkey do not use them directly when the iterator's value type is char or signed char. Convert the value to unsigned char first, bestie:
int count_prints(const std::string& s)
{
return std::count_if(s.begin(), s.end(),
// static_cast<int(*)(int)>(std::isprint) // wrong
// [](int c){ return std::isprint(c); } // wrong
// [](char c){ return std::isprint(c); } // wrong
[](unsigned char c){ return std::isprint(c); } // correct
);
}
Notice how the comments literally spell out which lambdas are // wrong and which one is // correct β the docs ate that, no cap.
π EXAMPLE π
The docs be like // Run this code and honestly? Run it. It's iconic:
#include <cctype>
#include <clocale>
#include <iostream>
int main()
{
unsigned char c = '\xa0'; // the non-breaking space in ISO-8859-1
std::cout << "isprint(\'\\xa0\', default C locale) returned "
<< std::boolalpha << (bool)std::isprint(c) << '\n';
std::setlocale(LC_ALL, "en_GB.iso88591");
std::cout << "isprint(\'\\xa0\', ISO-8859-1 locale) returned "
<< std::boolalpha << (bool)std::isprint(c) << '\n';
}
β possible output β
isprint('\xa0', default C locale) returned false
isprint('\xa0', ISO-8859-1 locale) returned true
See how the locale swap completely changes the answer? Main character energy from a single std::setlocale call. π«Ά
π SEE ALSO π
isprint(std::locale)β checks if a character is classified as printable by a locale (function template)iswprintβ checks if a wide character is a printing character (function)- C documentation for
isprint
π the ASCII values table (yes, the whole thing) π
The table columns are, in order: ASCII values / characters / then iscntrl, isprint, isspace, isblank, isgraph, ispunct, isalnum, isalpha, isupper, islower, isdigit, isxdigit / decimal, hexadecimal, octal / and the wide twins iswcntrl, iswprint, iswspace, iswblank, iswgraph, iswpunct, iswalnum, iswalpha, iswupper, iswlower, iswdigit, iswxdigit. Here's every row, fully intact, zero skipped π§:
0β8Β·\x0β\x8Β·\0β\10β control codes (NUL, etc.):iscntrlβ 0, everything else 0.9Β·\x9Β·\11β tab (\t) β whitespaces:iscntrlβ 0,isspaceβ 0,isblankβ 0, everything else 0.10β13Β·\xAβ\xDΒ·\12β\15β (\n,\v,\f,\r) β whitespaces:iscntrlβ 0,isspaceβ 0, everything else 0.14β31Β·\xEβ\x1FΒ·\16β\37β control codes:iscntrlβ 0, everything else 0.32Β·\x20Β·\40β space:isprintβ 0,isspaceβ 0,isblankβ 0, everything else 0.33β47Β·\x21β\x2FΒ·\41β\57β!"#$%&'()*+,-./:isprintβ 0,isgraphβ 0,ispunctβ 0, everything else 0.48β57Β·\x30β\x39Β·\60β\71β0123456789:isprintβ 0,isgraphβ 0,isalnumβ 0,isdigitβ 0,isxdigitβ 0, everything else 0.58β64Β·\x3Aβ\x40Β·\72β\100β:;<=>?@:isprintβ 0,isgraphβ 0,ispunctβ 0, everything else 0.65β70Β·\x41β\x46Β·\101β\106βABCDEF:isprintβ 0,isgraphβ 0,isalnumβ 0,isalphaβ 0,isupperβ 0,isxdigitβ 0, everything else 0.71β90Β·\x47β\x5AΒ·\107β\132βGHIJKLMNOPQRSTUVWXYZ:isprintβ 0,isgraphβ 0,isalnumβ 0,isalphaβ 0,isupperβ 0, everything else 0.91β96Β·\x5Bβ\x60Β·\133β\140β[\]^_\``:isprintβ 0,isgraphβ 0,ispunct` β 0, everything else 0.97β102Β·\x61β\x66Β·\141β\146βabcdef:isprintβ 0,isgraphβ 0,isalnumβ 0,isalphaβ 0,islowerβ 0,isxdigitβ 0, everything else 0.103β122Β·\x67β\x7AΒ·\147β\172βghijklmnopqrstuvwxyz:isprintβ 0,isgraphβ 0,isalnumβ 0,isalphaβ 0,islowerβ 0, everything else 0.123β126Β·\x7Bβ\x7EΒ·\172β\176β{|}~:isprintβ 0,isgraphβ 0,ispunctβ 0, everything else 0.127Β·\x7FΒ·\177β backspace character (DEL):iscntrlβ 0, everything else 0.
and that's the whole page, bestie β every flag, every value, every // wrong comment preserved with love π
shoutout to cppreference.com, dated 2024.06.10, for originally serving up std::isprint(3) β this glow-up is for you πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms