liveBook · Manning

Character class

As explained in Modern C

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

A character class groups characters by a property such as alphabetic, numeric, whitespace, punctuation, or control status. C exposes common classes through <ctype.h>, avoiding assumptions about the execution character encoding. Its classifiers include isalpha, isdigit, isspace, isalnum, isupper, islower, isprint, isgraph, ispunct, and isxdigit; toupper and tolower convert alphabetic case while leaving other characters unchanged. section 8.4

Classifying digits and letters

A base-conversion helper can test a character with isdigit(a), subtract '0' for decimal digits, then apply toupper(a) and isupper(a) before mapping Latin letters to values. The surrounding code receives and returns int, because these historical interfaces use int rather than char. Decimal digits are consecutive and isdigit is not locale dependent; broader alphabetic classes may be extended by the implementation. section 8.4

Common classifications include:

  • isspace recognizes space, form feed, newline, carriage return, tab, and vertical tab; isblank recognizes space and tab. section 8.4
  • isdigit recognizes decimal digits, while isxdigit recognizes decimal and hexadecimal digits; these classes are not extended by the implementation. section 8.4
  • isalnum combines alphabetic and decimal-digit membership; isalpha combines lowercase and uppercase membership. section 8.4
  • scanf uses bracket expressions as input classes: %[aeiouAEIOU] accepts vowels, while %[^\n]%*[\n] reads a nonempty line and discards its newline. section 14.2
Table 8.11. 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. (view table figure)

Name

Meaning

C locale

Extended

islower Lowercase 'a' ... 'z' Yes
isupper Uppercase 'A' ... 'Z' Yes
isblank Blank ' ', '\t' Yes
isspace Space ' ', '\f', '\n', '\r', '\t', '\v' Yes
isdigit Decimal '0' ... '9' No
isxdigit Hexadecimal '0' ... '9', 'a' ... 'f', 'A' ... 'F' No
iscntrl Control '\a', '\b', '\f', '\n', '\r', '\t', '\v' 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

Compare each classifier's meaning, C-locale members, and whether implementations may extend the class with platform-specific characters. section 8.4

How Modern C covers it

How deep it goes
Defines standard classifiers, relationships, locale extensions, and scan-set syntax, but defers wider multibyte-character treatment.
Examples
Uses hexatridecimal conversion, vowel scanning, and whole-line scanning with a negated newline class.
What it assumes
Assumes basic C character constants, string literals, and formatted input while establishing encoding and locale distinctions.
What you can do after
Apply ctype.h classifiers and scanf scan sets to recognize, convert, and selectively read characters.
How it connects
Connects character classes with ctype.h, C locale, scanf, strspn, strcspn, toupper, and tolower.

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. The section links point to the parts of the book it draws on.