std::isgraph(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

✨ NAME ✨

std::isgraph β€” bestie, the name is literally just std::isgraph again (the manpage said so and I'm not about to argue with it πŸ’€). It's the C++ standard library buddy that tells you whether a character is graphic.


πŸŽ€ Synopsis πŸŽ€

Ok so first things first: it lives in the header <cctype> πŸ“

int isgraph( int ch );

What it does: checks if the given character is graphic (has a graphical representation) as classified by the currently installed C locale. Slay.

In the default C locale, the following characters are graphic:

  • digits (0123456789)
  • uppercase letters (ABCDEFGHIJKLMNOPQRSTUVWXYZ)
  • lowercase letters (abcdefghijklmnopqrstuvwxyz)
  • punctuation characters (!"#$%&'()*+,-./:;<=>?@[\]^_{|}~`)
🚨 Behavior is undefined if the value of ch is not representable as unsigned char and is not equal to EOF. Not a vibe. Do not do that.

πŸ¦‹ Parameters πŸ¦‹

  • ch β€” the character to classify πŸ’…

🌸 Return value 🌸

Non-zero value if the character has a graphical representation character, zero otherwise. That's it, that's the whole answer ✨


πŸ’œ Notes πŸ’œ

Like all other functions from <cctype>, the behavior of std::isgraph is undefined if the argument's value is neither representable as unsigned char nor equal to EOF. To use these functions safely with plain chars (or signed chars), the argument should first be converted to unsigned char:

bool my_isgraph(char ch)
{
    return std::isgraph(static_cast<unsigned char>(ch));
}

Similarly, they should not be directly used with standard algorithms when the iterator's value type is char or signed char. Instead, convert the value to unsigned char first:

int count_graphs(const std::string& s)
{
    return std::count_if(s.begin(), s.end(),
                      // static_cast<int(*)(int)>(std::isgraph) // wrong
                      // [](int c){ return std::isgraph(c); }      // wrong
                      // [](char c){ return std::isgraph(c); }     // wrong
                         [](unsigned char c){ return std::isgraph(c); } // correct
                        );
}

Like… the static_cast<unsigned char> is the whole personality of this page, ngl πŸ™ˆ It's giving "cast it or regret it."


🧚 Example 🧚

// Run this code

#include <cctype>
#include <clocale>
#include <iostream>

int main()
{
    unsigned char c = '\xb6'; // the character ΒΆ in ISO-8859-1

    std::cout << "isgraph(\'\\xb6\', default C locale) returned "
              << std::boolalpha << (bool)std::isgraph(c) << '\n';

    std::setlocale(LC_ALL, "en_GB.iso88591");
    std::cout << "isgraph(\'\\xb6\', ISO-8859-1 locale) returned "
              << std::boolalpha << (bool)std::isgraph(c) << '\n';
}

🌈 Possible output 🌈

isgraph('\xb6', default C locale) returned false
isgraph('\xb6', ISO-8859-1 locale) returned true

Locale said "not today" then said "ok actually yes" 😭 iconic honestly.


πŸ‘‘ See also πŸ‘‘

  • isgraph(std::locale) β€” checks if a character is classified as graphical by a locale (function template)
  • iswgraph β€” checks if a wide character is a graphical character (function)
  • C documentation for isgraph

⭐ ASCII values ⭐

This is the full classification chart from the original page, with the columns ASCII values, characters, decimal / hexadecimal / octal, then iscntrl isprint isspace isblank isgraph ispunct isalnum isalpha isupper islower isdigit isxdigit, and the wide-character twins iswcntrl iswprint iswspace iswblank iswgraph iswpunct iswalnum iswalpha iswupper iswlower iswdigit iswxdigit. β‰ 0 means non-zero, 0 means zero. Yes bestie, I kept every single row πŸ’…

ASCII values  characters        decimal hexadecimal octal   description        iscntrl isprint isspace isblank isgraph ispunct isalnum isalpha isupper islower isdigit isxdigit  iswcntrl iswprint iswspace iswblank iswgraph iswpunct iswalnum iswalpha iswupper iswlower iswdigit iswxdigit
0–8           \x0–\x8           \0–\10                     control codes      β‰ 0      0       0       0       0       0       0       0       0       0       0       0          β‰ 0       0        0        0        0        0        0        0        0        0        0        0
              (NUL, etc.)
9             \x9               \11                        tab (\t)           β‰ 0      0       β‰ 0      β‰ 0      0       0       0       0       0       0       0       0          β‰ 0       0        0        0        0        0        0        0        0        0        0        0
              whitespaces
10–13         \xA–\xD           \12–\15                    (\n, \v, \f, \r)   β‰ 0      0       β‰ 0      0       0       0       0       0       0       0       0       0          β‰ 0       0        0        0        0        0        0        0        0        0        0        0
14–31         \xE–\x1F          \16–\37                    control codes      β‰ 0      0       0       0       0       0       0       0       0       0       0       0          β‰ 0       0        0        0        0        0        0        0        0        0        0        0
32            \x20              \40                        space              0       β‰ 0      β‰ 0      β‰ 0      0       0       0       0       0       0       0       0          0        β‰ 0       0        0        0        0        0        0        0        0        0        0
33–47         \x21–\x2F         \41–\57                    !"#$%&'()*+,-./   0       β‰ 0      0       0       β‰ 0      β‰ 0      0       0       0       0       0       0          0        β‰ 0       0        0        0        0        0        0        0        0        0        0
48–57         \x30–\x39         \60–\71                    0123456789         0       β‰ 0      0       0       β‰ 0      0       β‰ 0      0       0       0       β‰ 0      β‰ 0         0        β‰ 0       0        0        0        0        0        0        0        0        β‰ 0       β‰ 0
58–64         \x3A–\x40         \72–\100                   :;<=>?@           0       β‰ 0      0       0       β‰ 0      β‰ 0      0       0       0       0       0       0          0        β‰ 0       0        0        0        0        0        0        0        0        0        0
65–70         \x41–\x46         \101–\106                  ABCDEF             0       β‰ 0      0       0       β‰ 0      0       β‰ 0      β‰ 0      β‰ 0      0       0       β‰ 0         0        β‰ 0       0        0        0        0        0        0        0        0        0        0
71–90         \x47–\x5A         \107–\132                  GHIJKLMNOP         0       β‰ 0      0       0       β‰ 0      0       β‰ 0      β‰ 0      β‰ 0      0       0       0          0        β‰ 0       0        0        0        0        0        0        0        0        0        0
                                                          QRSTUVWXYZ
91–96         \x5B–\x60         \133–\140                  [\]^_`             0       β‰ 0      0       0       β‰ 0      β‰ 0      0       0       0       0       0       0          0        β‰ 0       0        0        0        0        0        0        0        0        0        0
97–102        \x61–\x66         \141–\146                  abcdef             0       β‰ 0      0       0       β‰ 0      0       β‰ 0      β‰ 0      0       β‰ 0      0       β‰ 0         0        β‰ 0       0        0        0        0        0        0        0        0        0        0
103–122       \x67–\x7A         \147–\172                  ghijklmnop         0       β‰ 0      0       0       β‰ 0      0       β‰ 0      β‰ 0      0       β‰ 0      0       0          0        β‰ 0       0        0        0        0        0        0        0        0        0        0
                                                          qrstuvwxyz
123–126       \x7B–\x7E         \172–\176                  {|}~               0       β‰ 0      0       0       β‰ 0      β‰ 0      0       0       0       0       0       0          0        β‰ 0       0        0        0        0        0        0        0        0        0        0
127           \x7F              \177                       backspace          β‰ 0      0       0       0       0       0       0       0       0       0       0       0          β‰ 0       0        0        0        0        0        0        0        0        0        0        0
                                                          character (DEL)

Anyway bestie, that's the whole page πŸ’– this was std::isgraph from the C++ Standard Library, straight from http://cppreference.com, dated 2024.06.10, and it's filed as std::isgraph(3). Go cast your chars to unsigned char and stay safe out there 🫢✨

πŸ“¦ where this page comes from