Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
C character classification
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{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 (computing)|character]] for membership in a particular class of characters; such as alphabetic, control, etc. Both single-byte, and wide characters are supported.<ref>{{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}}</ref> == History == Early [[c (programming language)|C]] [[programmer]]s working on the [[Unix]] operating system developed [[programming idiom]]s for classifying characters. For example, the following [[source code|code]] evaluates as ''true'' for an [[ASCII]] letter character <code>c</code>: <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''. == Implementation == For performance, the standard character classification functions are usually implemented as macros instead of [[function (programming)|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"> #define isdigit(c) ((c) >= '0' && (c) <= '9') </syntaxhighlight> This can lead to an error when the macro parameter <code>x</code> is expanded to an expression with a [[Side effect (computer science)|side effect]]; for example: <code>isdigit(x++)</code>. 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 <code>isdigit</code> macro could be written as: <syntaxhighlight lang="c"> #define isdigit(c) (TABLE[c] & 1) </syntaxhighlight> The macro argument, <code>c</code>, 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<br>character ! Wide<br>character ! Description |- | {{anchor|isalnum}}<code>[http://en.cppreference.com/w/c/string/byte/isalnum isalnum]</code> | {{anchor|iswalnum}}<code>[http://en.cppreference.com/w/c/string/wide/iswalnum iswalnum]</code> | checks whether the operand is alphanumeric |- | {{anchor|isalpha}}<code>[http://en.cppreference.com/w/c/string/byte/isalpha isalpha]</code> | {{anchor|iswalpha}}<code>[http://en.cppreference.com/w/c/string/wide/iswalpha iswalpha]</code> | checks whether the operand is alphabetic |- | {{anchor|islower}}<code>[http://en.cppreference.com/w/c/string/byte/islower islower]</code> | {{anchor|iswlower}}<code>[http://en.cppreference.com/w/c/string/wide/iswlower iswlower]</code> | checks whether the operand is lowercase |- | {{anchor|isupper}}<code>[http://en.cppreference.com/w/c/string/byte/isupper isupper]</code> | {{anchor|iswupper}}<code>[http://en.cppreference.com/w/c/string/wide/iswupper iswupper]</code> | checks whether the operand is an uppercase |- | {{anchor|isdigit}}<code>[http://en.cppreference.com/w/c/string/byte/isdigit isdigit]</code> | {{anchor|iswdigit}}<code>[http://en.cppreference.com/w/c/string/wide/iswdigit iswdigit]</code> | checks whether the operand is a digit |- | {{anchor|isxdigit}}<code>[http://en.cppreference.com/w/c/string/byte/isxdigit isxdigit]</code> | {{anchor|iswxdigit}}<code>[http://en.cppreference.com/w/c/string/wide/iswxdigit iswxdigit]</code> | checks whether the operand is hexadecimal |- | {{anchor|iscntrl}}<code>[http://en.cppreference.com/w/c/string/byte/iscntrl iscntrl]</code> | {{anchor|iswcntrl}}<code>[http://en.cppreference.com/w/c/string/wide/iswcntrl iswcntrl]</code> | checks whether the operand is a control character |- | {{anchor|isgraph}}<code>[http://en.cppreference.com/w/c/string/byte/isgraph isgraph]</code> | {{anchor|iswgraph}}<code>[http://en.cppreference.com/w/c/string/wide/iswgraph iswgraph]</code> | checks whether the operand is a graphical character |- | {{anchor|isspace}}<code>[http://en.cppreference.com/w/c/string/byte/isspace isspace]</code> | {{anchor|iswspace}}<code>[http://en.cppreference.com/w/c/string/wide/iswspace iswspace]</code> | checks whether the operand is [[Whitespace character|space]] |- | {{anchor|isblank}}<code>[http://en.cppreference.com/w/c/string/byte/isblank isblank]</code> | {{anchor|iswblank}}<code>[http://en.cppreference.com/w/c/string/wide/iswblank iswblank]</code> | checks whether the operand is a blank space character |- | {{anchor|isprint}}<code>[http://en.cppreference.com/w/c/string/byte/isprint isprint]</code> | {{anchor|iswprint}}<code>[http://en.cppreference.com/w/c/string/wide/iswprint iswprint]</code> | checks whether the operand is a printable character |- | {{anchor|ispunct}}<code>[http://en.cppreference.com/w/c/string/byte/ispunct ispunct]</code> | {{anchor|iswpunct}}<code>[http://en.cppreference.com/w/c/string/wide/iswpunct iswpunct]</code> | checks whether the operand is punctuation |- | {{anchor|tolower}}<code>[http://en.cppreference.com/w/c/string/byte/tolower tolower]</code> | {{anchor|towlower}}<code>[http://en.cppreference.com/w/c/string/wide/towlower towlower]</code> | converts the operand to lowercase |- | {{anchor|toupper}}<code>[http://en.cppreference.com/w/c/string/byte/toupper toupper]</code> | {{anchor|towupper}}<code>[http://en.cppreference.com/w/c/string/wide/towupper towupper]</code> | converts the operand to uppercase |- | {{n/a}} | {{anchor|iswctype}}<code>[http://en.cppreference.com/w/c/string/wide/iswctype iswctype]</code> | checks whether the operand falls into specific class |- | {{n/a}} | {{anchor|towctrans}}<code>[http://en.cppreference.com/w/c/string/wide/towctrans towctrans]</code> | converts the operand using a specific mapping |- | {{n/a}} | {{anchor|wctype}}<code>[http://en.cppreference.com/w/c/string/wide/wctype wctype]</code> | returns a wide character class to be used with <code>iswctype</code> |- | {{n/a}} | {{anchor|wctrans}}<code>[http://en.cppreference.com/w/c/string/wide/wctrans wctrans]</code> | returns a transformation mapping to be used with <code>towctrans</code> |} ==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}} [[Category:C standard library]] {{Use dmy dates|date=October 2017}}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Anchor
(
edit
)
Template:CProLang
(
edit
)
Template:C Standard Library
(
edit
)
Template:Cite book
(
edit
)
Template:Clear
(
edit
)
Template:Endash
(
edit
)
Template:N/a
(
edit
)
Template:Refimprove
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)
Template:Sister project
(
edit
)
Template:Use dmy dates
(
edit
)
Template:Wikibooks
(
edit
)