c Programming/C Reference/wctype.h

From Wikibooks, open books for an open world
Jump to: navigation, search

Template:C Standard library

In the programming language C, wctype.h is a header file in the standard library, containing various functions and macros for classifying and mapping wide characters. The corresponding header for dealing with (normal, non-wide) characters is <ctype.h>.

This header declares (from <wchar.h>) the wint_t and wctype_t types, and WEOF macro.[1] (wint_t is capable of storing any wide character or the value WEOF.) The type wctrans_t is also declared, which represents a mapping between characters.[1]

Character classification functions[edit]

These functions return non-zero/zero depending on whether or not the argument is in a certain category according to the current locale. POSIX:2008 specifies alternate versions of the functions suffixed with _l which take a locale (locale_t) as an additional argument.

Declaration Return non-zero if ch is …
int iswalnum(wint_t wc); alphanumeric
int iswalpha(wint_t wc); alphabetic
int iswblank(wint_t wc); blank
int iswcntrl(wint_t wc); a control character
int iswctype(wint_t wc, wctype_t charclass); in charclass
int iswdigit(wint_t wc); a decimal digit
int iswgraph(wint_t wc); a visible character
int iswlower(wint_t wc); a lowercase letter
int iswprint(wint_t wc); printable
int iswpunct(wint_t wc); punctuation
int iswspace(wint_t wc); white-space
int iswupper(wint_t wc); an uppercase letter
int iswxdigit(wint_t wc); a hexadecimal digit

Character mapping functions[edit]

These functions take a wide character, and apply some mapping to it.

Declaration Description
wint_t towctrans(wint_t ch, wctrans_t desc); Map ch according to the mapping desc
wint_t towlower(wint_t ch); If ch is an uppercase letter, map it to lowercase, otherwise return it unchanged
wint_t towupper(wint_t ch); If ch is a lowercase letter, map it to uppercase, otherwise return it unchanged

Other functions[edit]

These functions create values for use with other functions, from a string. Note that iswdigit(c) is the same as iswctype(c, wctype("digit")), and toupper(c) is the same as towctrans(c, wctrans("toupper")).[2]

Declaration Description
wctrans_t wctrans(const char *charclass); Returns a character mapping which can be used with towctrans
wctype_t wctype(const char *property); Returns a character class which can be used with iswctype

References[edit]

  1. a b wctype.h: wide-character classification and mapping utilities – Base Definitions Reference, The Single UNIX® Specification, Issue 7 from The Open Group
  2. The current Standard (C99 with Technical corrigenda TC1, TC2, and TC3 included)PDF (3.61 MB). Pages 397, 398 and 400.