C character classification explained

C character classification is an operation provided by a group of functions in the ANSI C Standard Library for the C programming language. These functions are used to test characters for membership in a particular class of characters, such as alphabetic characters, control characters, etc. Both single-byte, and wide characters are supported.[1]

History

Early C-language programmers working on the Unix operating system developed programming idioms for classifying characters into different types. For example, for the ASCII character set, the following expression identifies a letter, when its value is true:

('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z')

As this may be expressed in multiple formulations, it became desirable to introduce short, standardized forms of such tests that were placed in the system-wide header file ctype.h.

Implementation

Unlike the above example, the character classification routines are not written as comparison tests. In most C libraries, they are written as static table lookups instead of macros or functions.

For example, an array of 256 eight-bit integers, arranged as bitfields, is created, where each bit corresponds to a particular property of the character, e.g., isdigit, isalpha. If the lowest-order bit of the integers corresponds to the isdigit property, the code could be written as

  1. define isdigit(x) (TABLE[x] & 1)

Early versions of Linux used a potentially faulty method similar to the first code sample:

  1. define isdigit(x) ((x) >= '0' && (x) <= '9')

This can cause problems if when the macro expands, the expression substituted for x has a side effect. For example, if one calls isdigit(x++) or isdigit(run_some_program). It is not immediately evident that the argument to isdigit is evaluated twice. For this reason, the table-based approach is generally used.

Overview of functions

The functions that operate on single-byte characters are defined in ctype.h header file (cctype in C++). The functions that operate on wide characters are defined in wctype.h header file (cwctype in C++).

The classification is evaluated according to the effective locale.

Byte
character
Wide
character
Description
[http://en.cppreference.com/w/c/string/byte/isalnum isalnum][http://en.cppreference.com/w/c/string/wide/iswalnum iswalnum]checks whether the operand is alphanumeric
[http://en.cppreference.com/w/c/string/byte/isalpha isalpha][http://en.cppreference.com/w/c/string/wide/iswalpha iswalpha]checks whether the operand is alphabetic
[http://en.cppreference.com/w/c/string/byte/islower islower][http://en.cppreference.com/w/c/string/wide/iswlower iswlower]checks whether the operand is lowercase
[http://en.cppreference.com/w/c/string/byte/isupper isupper][http://en.cppreference.com/w/c/string/wide/iswupper iswupper]checks whether the operand is an uppercase
[http://en.cppreference.com/w/c/string/byte/isdigit isdigit][http://en.cppreference.com/w/c/string/wide/iswdigit iswdigit]checks whether the operand is a digit
[http://en.cppreference.com/w/c/string/byte/isxdigit isxdigit][http://en.cppreference.com/w/c/string/wide/iswxdigit iswxdigit]checks whether the operand is hexadecimal
[http://en.cppreference.com/w/c/string/byte/iscntrl iscntrl][http://en.cppreference.com/w/c/string/wide/iswcntrl iswcntrl]checks whether the operand is a control character
[http://en.cppreference.com/w/c/string/byte/isgraph isgraph][http://en.cppreference.com/w/c/string/wide/iswgraph iswgraph]checks whether the operand is a graphical character
[http://en.cppreference.com/w/c/string/byte/isspace isspace][http://en.cppreference.com/w/c/string/wide/iswspace iswspace]checks whether the operand is space
[http://en.cppreference.com/w/c/string/byte/isblank isblank][http://en.cppreference.com/w/c/string/wide/iswblank iswblank]checks whether the operand is a blank space character
[http://en.cppreference.com/w/c/string/byte/isprint isprint][http://en.cppreference.com/w/c/string/wide/iswprint iswprint]checks whether the operand is a printable character
[http://en.cppreference.com/w/c/string/byte/ispunct ispunct][http://en.cppreference.com/w/c/string/wide/iswpunct iswpunct]checks whether the operand is punctuation
[http://en.cppreference.com/w/c/string/byte/tolower tolower][http://en.cppreference.com/w/c/string/wide/towlower towlower]converts the operand to lowercase
[http://en.cppreference.com/w/c/string/byte/toupper toupper][http://en.cppreference.com/w/c/string/wide/towupper towupper]converts the operand to uppercase
[http://en.cppreference.com/w/c/string/wide/iswctype iswctype]checks whether the operand falls into specific class
[http://en.cppreference.com/w/c/string/wide/towctrans towctrans]converts the operand using a specific mapping
[http://en.cppreference.com/w/c/string/wide/wctype wctype]returns a wide character class to be used with iswctype
[http://en.cppreference.com/w/c/string/wide/wctrans wctrans]returns a transformation mapping to be used with towctrans

External links

Notes and References

  1. Book: ISO/IEC 9899:1999 specification . p. 193, ยง 7.4.