Skip to content
Surf Wiki
Save to docs
general/c-standard-library

From Surf Wiki (app.surf) — the open knowledge base

C character classification

Operations in the C standard library that classify characters


Summary

Operations in the C standard library that classify characters

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 characters or control characters. Both single-byte and wide characters are supported.

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:

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:

C
#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:

C
#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.

Byte
characterWide
characterDescription
isalnum`isalnum`iswalnum`iswalnum`checks whether the operand is alphanumeric
isalpha`isalpha`iswalpha`iswalpha`checks whether the operand is alphabetic
islower`islower`iswlower`iswlower`checks whether the operand is lowercase
isupper`isupper`iswupper`iswupper`checks whether the operand is an uppercase
isdigit`isdigit`iswdigit`iswdigit`checks whether the operand is a digit
isxdigit`isxdigit`iswxdigit`iswxdigit`checks whether the operand is hexadecimal
iscntrl`iscntrl`iswcntrl`iswcntrl`checks whether the operand is a control character
isgraph`isgraph`iswgraph`iswgraph`checks whether the operand is a graphical character
isspace`isspace`iswspace`iswspace`checks whether the operand is space
isblank`isblank`iswblank`iswblank`checks whether the operand is a blank space character
isprint`isprint`iswprint`iswprint`checks whether the operand is a printable character
ispunct`ispunct`iswpunct`iswpunct`checks whether the operand is punctuation
tolower`tolower`towlower`towlower`converts the operand to lowercase
toupper`toupper`towupper`towupper`converts the operand to uppercase
iswctype`iswctype`checks whether the operand falls into specific class
towctrans`towctrans`converts the operand using a specific mapping
wctype`wctype`returns a wide character class to be used with `iswctype`
wctrans`wctrans`returns a transformation mapping to be used with `towctrans`

References

References

  1. "ISO/IEC 9899:1999 specification".
Wikipedia Source

This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page.

Want to explore this topic further?

Ask Mako anything about C character classification — get instant answers, deeper analysis, and related topics.

Research with Mako

Free with your Surf account

Content sourced from Wikipedia, available under CC BY-SA 4.0.

This content may have been generated or modified by AI. CloudSurf Software LLC is not responsible for the accuracy, completeness, or reliability of AI-generated content. Always verify important information from primary sources.

Report