liveBook · Manning

Ctype.h

As explained in Modern C, Third Edition

ctype.h is the C standard header that declares character classification and character-case conversion facilities.

The C standard header <ctype.h> provides classifiers for common character classes and the case-conversion functions toupper and tolower. These interfaces work with character classes without depending directly on a particular encoding. For historical reasons, the classifiers and conversions take int arguments and return int. section 8.5

Converting a digit or alphabetic digit

unsigned hexatridecimal(int a) { if (isdigit(a)) return a - '0'; a = toupper(a); return isupper(a) ? 10 + (a - 'A') : -1; } Here isdigit recognizes decimal digits, whose character codes are guaranteed to be consecutive. toupper normalizes alphabetic input before isupper tests it. section 8.5

The classifiers cover these commonly used character classes:

  • isalnum, isalpha, islower, and isupper classify alphanumeric, alphabetic, lowercase, and uppercase characters. section 8.5
  • isdigit and isxdigit recognize decimal and hexadecimal digits; their C-locale classes are not extended with platform-specific characters. section 8.5
  • isblank, isspace, iscntrl, isgraph, isprint, and ispunct classify whitespace, control, graphical, printable, and punctuation characters. section 8.5
  • toupper and tolower convert alphabetic characters and leave other characters unchanged. 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

Use the classifier table to compare each predicate's meaning, C-locale members, and possible platform-specific extensions. section 8.5

Character literals such as 'a' and '\n' have type int, and numeric octal or hexadecimal character escapes require care because hexadecimal escapes consume every following hexadecimal digit that belongs to the code. section 8.5

How Modern C, Third Edition covers it

How deep it goes
Defines classifier categories, argument types, conversion behavior, locale extensions, and a worked hexadecimal-digit conversion.
Examples
Demonstrates hexadecimal-digit conversion using isdigit, toupper, and isupper, alongside character and string escape examples.
What it assumes
Assumes familiarity with C character and string literals, while defining character classes and their relationships.
What you can do after
Apply it via <ctype.h> classifiers and conversions to recognize character classes and normalize alphabetic input.
How it connects
Connects character classification with string processing, character literals, locales, and numerical conversion through stdlib.

Also explained in

Compare how 2 books explain ctype.h →

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.