liveBook · Manning

Character class

As explained in Modern C, Third Edition

A character class is a set of characters sharing a property such as being alphabetic, numeric, whitespace, or punctuation.

A character class is a set of characters sharing a property, such as alphabetic, numeric, whitespace, or punctuation. C exposes common classes through <ctype.h> classifier functions, while character and string literals remain usable across platform encodings. section 8.5

Table 8.14 Character classifiers. The third column indicates whether C implementations may extend these classes with platform-specific characters, such as ’ ä’ as a lowercase character or ’ €’ as punctuation.
Name Meaning C locale Extended
islower Lowercase ’a’ ⋯ ’z’ Yes
isupper Uppercase ’A’ ⋯ ’Z’ Yes
isblank Blank ’ ’, ’'͡ Yes
isspace Space ”, ’'̑, ’’, ’'̊, ’'͡, ’'̌ Yes
isdigit Decimal ’0’ ⋯ ’9’ No
isxdigit Hexadecimal ’0’ ⋯ ’9’, ’a’ ⋯ ’f’, ’A’ ⋯ ’F’ No
iscntrl Control ’’, ’'̱, ’'̑, ’’, ’'̊, ’'͡, ’'̌ Yes
isalnum Alphanumeric isalpha(x)||isdigit(x) Yes
isalpha Alphabet islower(x)||isupper(x) Yes
isgraph Graphical (!iscntrl(x)) && (x != ”) Yes
isprint Printable !iscntrl(x) Yes
ispunct Punctuation isprint(x)&&!(isalnum(x)||isspace(x)) Yes

The classifier table connects each predicate to its C-locale meaning and shows which classes implementations may extend with platform-specific characters. section 8.5

Use the classifiers and conversions according to these distinctions:

  • Use isalpha, isdigit, isalnum, islower, and isupper for alphabetic and numeric categories; use isspace and isblank for whitespace-related tests. section 8.5
  • Use isxdigit for hexadecimal digits; unlike several alphabetic classes, decimal and hexadecimal digit classes are not locale-extended. section 8.5
  • Use isgraph, isprint, ispunct, and iscntrl to distinguish graphical, printable, punctuation, and control characters. section 8.5
  • Remember that the classifiers and toupper/tolower take and return int for historical reasons; the case conversions leave nonalphabetic characters unchanged. section 8.5

Character classes in formatted input

A scanf scan set selects a class explicitly: "%[aeiouAEIOU]" matches vowels. Beginning the set with ^ negates it, so "%[^\n]%*[\n]" reads a nonempty line and discards its final newline. section 14.2

Character classes also support conversion logic. A hexadecimal-style converter can test isdigit, subtract '0' for consecutive decimal digits, convert with toupper, and then test isupper for alphabetic digits.

How Modern C, Third Edition covers it

How deep it goes
Defines common predicates, locale extensions, integer interfaces, and scan-set syntax, then demonstrates classifier-based conversion.
Examples
Uses ctype predicates, a hexadecimal-style converter, and scanf sets for vowels and newline-delimited input.
What it assumes
Assumes familiarity with C character and string literals, integer character constants, and scanf format strings.
What you can do after
Apply character classes via <ctype.h> classifiers, case conversions, conversion routines, and scanf scan sets.
How it connects
Connects character classes with character literals, string literals, locales, case conversion, numeric conversion, and formatted input.

Also explained in

Compare how 9 books explain character class →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.