HOME

TheInfoList



OR:

C character classification is a group of operations in the
C standard library The C standard library, sometimes referred to as libc, is the standard library for the C (programming language), C programming language, as specified in the ISO C standard.International Organization for Standardization, ISO/International Electrote ...
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.


History

Early C
programmer A programmer, computer programmer or coder is an author of computer source code someone with skill in computer programming. The professional titles Software development, ''software developer'' and Software engineering, ''software engineer' ...
s working on the
Unix Unix (, ; trademarked as UNIX) is a family of multitasking, multi-user computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...
operating system developed programming idioms for classifying characters. For example, the following
code In communications and information processing, code is a system of rules to convert information—such as a letter, word, sound, image, or gesture—into another form, sometimes shortened or secret, for communication through a communicati ...
evaluates as ''true'' for an
ASCII ASCII ( ), an acronym for American Standard Code for Information Interchange, is a character encoding standard for representing a particular set of 95 (English language focused) printable character, printable and 33 control character, control c ...
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 Linux ( ) is a family of open source Unix-like operating systems based on the Linux kernel, an kernel (operating system), operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically package manager, pac ...
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 In medicine, a side effect is an effect of the use of a medicinal drug or other treatment, usually adverse but sometimes beneficial, that is unintended. Herbal and traditional medicines also have side effects. A drug or procedure usually use ...
; 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 & 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.


References


External links

C standard library {{Use dmy dates, date=October 2017