C character classification
{{Short description|Operations in the C standard library that classify characters}}
{{refimprove|date=October 2011}}
{{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.{{cite book | url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf | title=ISO/IEC 9899:1999 specification | at=p. 193, § 7.4}}
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 {{endash}} one for each character value {{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:
- 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.
class="wikitable" style="font-size:0.85em;"
! Byte ! Wide ! Description |
{{anchor|isalnum}}[http://en.cppreference.com/w/c/string/byte/isalnum isalnum]
| {{anchor|iswalnum}} | checks whether the operand is alphanumeric |
{{anchor|isalpha}}[http://en.cppreference.com/w/c/string/byte/isalpha isalpha]
| {{anchor|iswalpha}} | checks whether the operand is alphabetic |
{{anchor|islower}}[http://en.cppreference.com/w/c/string/byte/islower islower]
| {{anchor|iswlower}} | checks whether the operand is lowercase |
{{anchor|isupper}}[http://en.cppreference.com/w/c/string/byte/isupper isupper]
| {{anchor|iswupper}} | checks whether the operand is an uppercase |
{{anchor|isdigit}}[http://en.cppreference.com/w/c/string/byte/isdigit isdigit]
| {{anchor|iswdigit}} | checks whether the operand is a digit |
{{anchor|isxdigit}}[http://en.cppreference.com/w/c/string/byte/isxdigit isxdigit]
| {{anchor|iswxdigit}} | checks whether the operand is hexadecimal |
{{anchor|iscntrl}}[http://en.cppreference.com/w/c/string/byte/iscntrl iscntrl]
| {{anchor|iswcntrl}} | checks whether the operand is a control character |
{{anchor|isgraph}}[http://en.cppreference.com/w/c/string/byte/isgraph isgraph]
| {{anchor|iswgraph}} | checks whether the operand is a graphical character |
{{anchor|isspace}}[http://en.cppreference.com/w/c/string/byte/isspace isspace]
| {{anchor|iswspace}} | checks whether the operand is space |
{{anchor|isblank}}[http://en.cppreference.com/w/c/string/byte/isblank isblank]
| {{anchor|iswblank}} | checks whether the operand is a blank space character |
{{anchor|isprint}}[http://en.cppreference.com/w/c/string/byte/isprint isprint]
| {{anchor|iswprint}} | checks whether the operand is a printable character |
{{anchor|ispunct}}[http://en.cppreference.com/w/c/string/byte/ispunct ispunct]
| {{anchor|iswpunct}} | checks whether the operand is punctuation |
{{anchor|tolower}}[http://en.cppreference.com/w/c/string/byte/tolower tolower]
| {{anchor|towlower}} | converts the operand to lowercase |
{{anchor|toupper}}[http://en.cppreference.com/w/c/string/byte/toupper toupper]
| {{anchor|towupper}} | converts the operand to uppercase |
{{n/a}}
| {{anchor|iswctype}} | checks whether the operand falls into specific class |
{{n/a}}
| {{anchor|towctrans}} | converts the operand using a specific mapping |
{{n/a}}
| {{anchor|wctype}} | returns a wide character class to be used with |
{{n/a}}
| {{anchor|wctrans}} | returns a transformation mapping to be used with |
References
{{reflist}}
External links
{{wikibooks|A Little C Primer|C Character Class Test Library}}
{{wikibooks|C Programming|C character classification|C Programming/C Reference}}
{{clear}}
{{CProLang|state=expanded}}
{{Use dmy dates|date=October 2017}}