Programs that work with characters and strings often need to classify a character--is it alphabetic, is it a digit, is it whitespace, and so on--and perform case conversion operations on characters. The functions in the header file `ctype.h' are provided for this purpose.
Since the choice of locale and character set can alter the
classifications of particular character codes, all of these functions
are affected by the current locale. (More precisely, they are affected
by the locale currently selected for character classification--the
LC_CTYPE
category; see section Categories of Activities that Locales Affect.)
The ISO C standard specifies two different sets of functions. The
one set works on char
type characters, the other one on
wchar_t
wide characters (see section Introduction to Extended Characters).
This section explains the library functions for classifying characters.
For example, isalpha
is the function to test for an alphabetic
character. It takes one argument, the character to test, and returns a
nonzero integer if the character is alphabetic, and zero otherwise. You
would use it like this:
if (isalpha (c)) printf ("The character `%c' is alphabetic.\n", c);
Each of the functions in this section tests for membership in a
particular class of characters; each has a name starting with `is'.
Each of them takes one argument, which is a character to test, and
returns an int
which is treated as a boolean value. The
character argument is passed as an int
, and it may be the
constant value EOF
instead of a real character.
The attributes of any given character can vary between locales. See section Locales and Internationalization, for more information on locales.
These functions are declared in the header file `ctype.h'.
islower
or isupper
is true of a character, then
isalpha
is also true.
In some locales, there may be additional characters for which
isalpha
is true--letters which are neither upper case nor lower
case. But in the standard "C"
locale, there are no such
additional characters.
isalpha
or isdigit
is
true of a character, then isalnum
is also true.
"C"
locale, isspace
returns true for only the standard
whitespace characters:
' '
'\f'
'\n'
'\r'
'\t'
'\v'
unsigned char
value that fits
into the US/UK ASCII character set. This function is a BSD extension
and is also an SVID extension.
This section explains the library functions for performing conversions
such as case mappings on characters. For example, toupper
converts any character to upper case if possible. If the character
can't be converted, toupper
returns it unchanged.
These functions take one argument of type int
, which is the
character to convert, and return the converted character as an
int
. If the conversion is not applicable to the argument given,
the argument is returned unchanged.
Compatibility Note: In pre-ISO C dialects, instead of
returning the argument unchanged, these functions may fail when the
argument is not suitable for the conversion. Thus for portability, you
may need to write islower(c) ? toupper(c) : c
rather than just
toupper(c)
.
These functions are declared in the header file `ctype.h'.
tolower
returns the corresponding
lower-case letter. If c is not an upper-case letter,
c is returned unchanged.
toupper
returns the corresponding
upper-case letter. Otherwise c is returned unchanged.
unsigned char
value
that fits into the US/UK ASCII character set, by clearing the high-order
bits. This function is a BSD extension and is also an SVID extension.
tolower
, and is provided for compatibility
with the SVID. See section SVID (The System V Interface Description).
toupper
, and is provided for compatibility
with the SVID.
Amendment 1 to ISO C90 defines functions to classify wide
characters. Although the original ISO C90 standard already defined
the type wchar_t
, no functions operating on them were defined.
The general design of the classification functions for wide characters
is more general. It allows extensions to the set of available
classifications, beyond those which are always available. The POSIX
standard specifies how extensions can be made, and this is already
implemented in the GNU C library implementation of the localedef
program.
The character class functions are normally implemented with bitsets, with a bitset per character. For a given character, the appropriate bitset is read from a table and a test is performed as to whether a certain bit is set. Which bit is tested for is determined by the class.
For the wide character classification functions this is made visible.
There is a type classification type defined, a function to retrieve this
value for a given class, and a function to test whether a given
character is in this class, using the classification value. On top of
this the normal character classification functions as used for
char
objects can be defined.
wctype_t
can hold a value which represents a character class.
The only defined way to generate such a value is by using the
wctype
function.
wctype
returns a value representing a class of wide
characters which is identified by the string property. Beside
some standard properties each locale can define its own ones. In case
no property with the given name is known for the current locale
selected for the LC_CTYPE
category, the function returns zero.
The properties known in every locale are:
@multitable @columnfractions .25 .25 .25 .25
"alnum"
@tab "alpha"
@tab "cntrl"
@tab "digit"
"graph"
@tab "lower"
@tab "print"
@tab "punct"
"space"
@tab "upper"
@tab "xdigit"
This function is declared in `wctype.h'.
wctype
.
This function is declared in `wctype.h'.
wctype
if the property string is one of the known character
classes. In some situations it is desirable to construct the property
strings, and then it is important that wctype
can also handle the
standard classes.
iswalpha
or iswdigit
is true of a character, then iswalnum
is also
true.
This function can be implemented using
iswctype (wc, wctype ("alnum"))It is declared in `wctype.h'.
iswlower
or iswupper
is true of a character, then
iswalpha
is also true.
In some locales, there may be additional characters for which
iswalpha
is true--letters which are neither upper case nor lower
case. But in the standard "C"
locale, there are no such
additional characters.
This function can be implemented using
iswctype (wc, wctype ("alpha"))It is declared in `wctype.h'.
iswctype (wc, wctype ("cntrl"))It is declared in `wctype.h'.
n = 0; while (iswdigit (*wc)) { n *= 10; n += *wc++ - L'0'; }This function can be implemented using
iswctype (wc, wctype ("digit"))It is declared in `wctype.h'.
iswctype (wc, wctype ("graph"))It is declared in `wctype.h'.
iswctype (wc, wctype ("lower"))It is declared in `wctype.h'.
iswctype (wc, wctype ("print"))It is declared in `wctype.h'.
iswctype (wc, wctype ("punct"))It is declared in `wctype.h'.
"C"
locale, iswspace
returns true for only the standard
whitespace characters:
L' '
L'\f'
L'\n'
L'\r'
L'\t'
L'\v'
iswctype (wc, wctype ("space"))It is declared in `wctype.h'.
iswctype (wc, wctype ("upper"))It is declared in `wctype.h'.
iswctype (wc, wctype ("xdigit"))It is declared in `wctype.h'.
The first note is probably not astonishing but still occasionally a
cause of problems. The iswXXX
functions can be implemented
using macros and in fact, the GNU C library does this. They are still
available as real functions but when the `wctype.h' header is
included the macros will be used. This is the same as the
char
type versions of these functions.
The second note covers something new. It can be best illustrated by a (real-world) example. The first piece of code is an excerpt from the original code. It is truncated a bit but the intention should be clear.
int is_in_class (int c, const char *class) { if (strcmp (class, "alnum") == 0) return isalnum (c); if (strcmp (class, "alpha") == 0) return isalpha (c); if (strcmp (class, "cntrl") == 0) return iscntrl (c); ... return 0; }
Now, with the wctype
and iswctype
you can avoid the
if
cascades, but rewriting the code as follows is wrong:
int is_in_class (int c, const char *class) { wctype_t desc = wctype (class); return desc ? iswctype ((wint_t) c, desc) : 0; }
The problem is that it is not guaranteed that the wide character representation of a single-byte character can be found using casting. In fact, usually this fails miserably. The correct solution to this problem is to write the code as follows:
int is_in_class (int c, const char *class) { wctype_t desc = wctype (class); return desc ? iswctype (btowc (c), desc) : 0; }
See section Converting Single Characters, for more information on btowc
.
Note that this change probably does not improve the performance
of the program a lot since the wctype
function still has to make
the string comparisons. It gets really interesting if the
is_in_class
function is called more than once for the
same class name. In this case the variable desc could be computed
once and reused for all the calls. Therefore the above form of the
function is probably not the final one.
The classification functions are also generalized by the ISO C
standard. Instead of just allowing the two standard mappings, a
locale can contain others. Again, the localedef
program
already supports generating such locale data files.
wctrans
function.
wctrans
function has to be used to find out whether a named
mapping is defined in the current locale selected for the
LC_CTYPE
category. If the returned value is non-zero, you can use
it afterwards in calls to towctrans
. If the return value is
zero no such mapping is known in the current locale.
Beside locale-specific mappings there are two mappings which are guaranteed to be available in every locale:
@multitable @columnfractions .5 .5
"tolower"
@tab "toupper"
These functions are declared in `wctype.h'.
towctrans
maps the input character wc
according to the rules of the mapping for which desc is a
descriptor, and returns the value it finds. desc must be
obtained by a successful call to wctrans
.
This function is declared in `wctype.h'.
wctrans
for them.
towlower
returns the corresponding
lower-case letter. If wc is not an upper-case letter,
wc is returned unchanged.
towlower
can be implemented using
towctrans (wc, wctrans ("tolower"))This function is declared in `wctype.h'.
towupper
returns the corresponding
upper-case letter. Otherwise wc is returned unchanged.
towupper
can be implemented using
towctrans (wc, wctrans ("toupper"))This function is declared in `wctype.h'.
char
type value to a wint_t
and use it as an
argument to towctrans
calls.
Go to the first, previous, next, last section, table of contents.