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 character (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.
The second amendment to ISO C89 defines functions to classify wide
character. Although the original ISO C89 standard already defined
the type wchar_t but no functions operating on them were defined.
The general design of the classification functions for wide characters
is more general. It allows to extend the set of available
classification beyond the set which is always available. The POSIX
standard specifies a way how the extension can be done and this is
already implemented in the GNU C library implementation of the
localedef program.
The character class functions are normally implemented using bitsets. I.e., for the character in question the appropriate bitset is read from a table and a test is performed whether a certain bit is set in this bitset. Which bit is tested for is determined by the class.
For the wide character classification functions this is made visible.
There is a type representing the classification, a function to retrieve
this value for a specific class, and a function to test using the
classification value whether a given character is in this class. 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 ony 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 for the
LC_CTYPE category the function returns zero.
The properties known in every locale are:
"alpha" | "cntrl" | "digit"
| |
"lower" | "print" | "punct"
| |
"upper" | "xdigit"
|
To test the membership of a character to one of the non-standard classes the ISO C standard defines a completely new function.
wctype.
The make it easier to use the commonly used classification functions
they are defined in the C library. There is no need to use
wctype is the property string is one of the known character
classes. In some situations it is desirable to construct the property
string and then it gets 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"))
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"))
This function can be implemented using
iswctype (wc, wctype ("cntrl"))
n = 0;
while (iswctype (*wc))
{
n *= 10;
n += *wc++ - L'0';
}
This function can be implemented using
iswctype (wc, wctype ("digit"))
This function can be implemented using
iswctype (wc, wctype ("graph"))
This function can be implemented using
iswctype (wc, wctype ("lower"))
This function can be implemented using
iswctype (wc, wctype ("print"))
This function can be implemented using
iswctype (wc, wctype ("punct"))
"C" locale, iswspace returns true for only the standard
whitespace characters:
L' '
L'\f'
L'\n'
L'\r'
L'\t'
L'\v'
This function can be implemented using
iswctype (wc, wctype ("space"))
This function can be implemented using
iswctype (wc, wctype ("upper"))
This function can be implemented using
iswctype (wc, wctype ("xdigit"))
The GNu C library provides also a function which is not defined in the ISO C standard but which is available as a version for single byte characters as well.
The first note is probably nothing 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 nothing new compared to the
char type versions of these functions.
The second notes covers something which is 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 one could 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 guarateed that the wide character representation of a single-byte character can be found using casting. In fact, usually this fails miserably. The correct solution for 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.
Please 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. But it gets really interesting if the
is_in_class function would be called more than once using 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.
As for the classification functions the ISO C standard also
generalizes the mapping functions. Instead of only allowing the two
standard mappings the 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 it can
afterwards be used 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:
"toupper"
|
towctrans function maps the input character wc
according to the rules of the mapping for which desc is an
descriptor and returns the so found value. The desc value must be
obtained by a successful call to wctrans.
The ISO C standard also defines for the generally available mappings
convenient shortcuts so that it is not necesary to call 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"))
towupper returns the corresponding
upper-case letter. Otherwise wc is returned unchanged.
towupper can be implemented using
towctrans (wc, wctrans ("toupper"))
The same warnings given in the last section for the use of the wide
character classiffication function applies here. It is not possible to
simply cast a char type value to a wint_t and use it as an
argument for towctrans calls.
Go to the first, previous, next, last section, table of contents.