Menu
Home Explore People Places Arts History Plants & Animals Science Life & Culture Technology
On this page
C character classification
C standard library header

C character classification is a group of operations in the C standard library that test a character for membership in a particular class of characters; such as alphabetic, control, etc. Both single-byte, and wide characters are supported.

We don't have any images related to C character classification yet.
We don't have any YouTube videos related to C character classification yet.
We don't have any PDF documents related to C character classification yet.
We don't have any Books related to C character classification yet.
We don't have any archived web articles related to C character classification yet.

History

Early C programmers working on the Unix operating system developed programming idioms for classifying characters. For example, the following code evaluates as true for an ASCII letter character c:

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

Eventually, the interface to common character classification functionality was codified in the C standard library file ctype.h.

Implementation

For performance, the standard character classification functions are usually implemented as macros instead of functions. But, due to limitations of macro evaluation, they are generally not implemented today as they were in early versions of Linux like:

#define isdigit(c) ((c) >= '0' && (c) <= '9')

This can lead to an error when the macro parameter x is expanded to an expression with a side effect; for example: isdigit(x++). If the implementation was a function, then x would be incremented only once. But for this macro definition it is incremented twice.

To eliminate this problem, a common implementation is for the macro to use table lookup. For example, the standard library provides an array of 256 integers – one for each character value – that each contain a bit-field for each supported classification. A macro references an integer by character value index and accesses the associated bit-field. For example, if the low bit indicates whether the character is a digit, then the isdigit macro could be written as:

#define isdigit(c) (TABLE[c] & 1)

The macro argument, c, is referenced only once, so is evaluated only once.

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.

BytecharacterWidecharacterDescription
isalnumiswalnumchecks whether the operand is alphanumeric
isalphaiswalphachecks whether the operand is alphabetic
isloweriswlowerchecks whether the operand is lowercase
isupperiswupperchecks whether the operand is an uppercase
isdigitiswdigitchecks whether the operand is a digit
isxdigitiswxdigitchecks whether the operand is hexadecimal
iscntrliswcntrlchecks whether the operand is a control character
isgraphiswgraphchecks whether the operand is a graphical character
isspaceiswspacechecks whether the operand is space
isblankiswblankchecks whether the operand is a blank space character
isprintiswprintchecks whether the operand is a printable character
ispunctiswpunctchecks whether the operand is punctuation
tolowertowlowerconverts the operand to lowercase
touppertowupperconverts the operand to uppercase
iswctypechecks whether the operand falls into specific class
towctransconverts the operand using a specific mapping
wctypereturns a wide character class to be used with iswctype
wctransreturns a transformation mapping to be used with towctrans
The Wikibook A Little C Primer has a page on the topic of: C Character Class Test Library The Wikibook C Programming has a page on the topic of: C Programming/C Reference

References

  1. ISO/IEC 9899:1999 specification (PDF). p. 193, § 7.4. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf