Template:Short description Template:Refimprove Template:C Standard Library

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.<ref>Template:Cite book</ref>

HistoryEdit

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:

<syntaxhighlight lang="c"> ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') </syntaxhighlight>

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

ImplementationEdit

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:

<syntaxhighlight lang="c">

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

</syntaxhighlight>

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 Template:Endash one for each character value Template:Endash 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:

<syntaxhighlight lang="c">

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

</syntaxhighlight>

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

Overview of functionsEdit

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
Template:Anchorisalnum Template:Anchoriswalnum checks whether the operand is alphanumeric
Template:Anchorisalpha Template:Anchoriswalpha checks whether the operand is alphabetic
Template:Anchorislower Template:Anchoriswlower checks whether the operand is lowercase
Template:Anchorisupper Template:Anchoriswupper checks whether the operand is an uppercase
Template:Anchorisdigit Template:Anchoriswdigit checks whether the operand is a digit
Template:Anchorisxdigit Template:Anchoriswxdigit checks whether the operand is hexadecimal
Template:Anchoriscntrl Template:Anchoriswcntrl checks whether the operand is a control character
Template:Anchorisgraph Template:Anchoriswgraph checks whether the operand is a graphical character
Template:Anchorisspace Template:Anchoriswspace checks whether the operand is space
Template:Anchorisblank Template:Anchoriswblank checks whether the operand is a blank space character
Template:Anchorisprint Template:Anchoriswprint checks whether the operand is a printable character
Template:Anchorispunct Template:Anchoriswpunct checks whether the operand is punctuation
Template:Anchortolower Template:Anchortowlower converts the operand to lowercase
Template:Anchortoupper Template:Anchortowupper converts the operand to uppercase
Template:N/a Template:Anchoriswctype checks whether the operand falls into specific class
Template:N/a Template:Anchortowctrans converts the operand using a specific mapping
Template:N/a Template:Anchorwctype returns a wide character class to be used with iswctype
Template:N/a Template:Anchorwctrans returns a transformation mapping to be used with towctrans

ReferencesEdit

Template:Reflist

External linksEdit

Template:Sister project Template:Sister project

Template:CProLang Template:Use dmy dates