GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Main page
  • Contents
  • Current events
  • Random article
  • About Wikipedia
  • Contact us
  • Help
  • Learn to edit
  • Community portal
  • Recent changes
  • Upload file
  • Special pages
  • Search
  • Donate

This view

  • Original page
  • New address

GeezerVison is showing en.wikipedia.org — C character classification - Wikipedia

(Redirected from Ctype.h)
icon
This article needs more citations. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed.
Find sources: "C character classification" – news · newspapers · books · scholar · JSTOR
(October 2011) (Learn how and when to remove this message)

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.[1]

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 – 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[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
character
Wide
character
Description
isalnum iswalnum checks whether the operand is alphanumeric
isalpha iswalpha checks whether the operand is alphabetic
islower iswlower checks whether the operand is lowercase
isupper iswupper checks whether the operand is an uppercase
isdigit iswdigit checks whether the operand is a digit
isxdigit iswxdigit checks whether the operand is hexadecimal
iscntrl iswcntrl checks whether the operand is a control character
isgraph iswgraph checks whether the operand is a graphical character
isspace iswspace checks whether the operand is space
isblank iswblank checks whether the operand is a blank space character
isprint iswprint checks whether the operand is a printable character
ispunct iswpunct checks whether the operand is punctuation
tolower towlower converts the operand to lowercase
toupper towupper converts the operand to uppercase
—N/a iswctype checks whether the operand falls into specific class
—N/a towctrans converts the operand using a specific mapping
—N/a wctype returns a wide character class to be used with iswctype
—N/a wctrans returns a transformation mapping to be used with towctrans

References

  1. ↑ ISO/IEC 9899:1999 specification (PDF). p. 193, § 7.4.

External links

Wikibooks logo
The Wikibook A Little C Primer has a page on the topic of: C Character Class Test Library
Wikibooks logo
The Wikibook C Programming has a page on the topic of: C Programming/C Reference

Retrieved from "https://en.wikipedia.org/w/index.php?title=C_character_classification&oldid=1328454953"
Category:
  • C standard library
Hidden categories:
  • Articles with short description
  • Short description is different from Wikidata
  • Articles needing additional references from October 2011
  • All articles needing additional references
  • Use dmy dates from October 2017
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.