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 7.3 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 6.1 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 7. Locales and Internationalization, for more information on locales.
These functions are declared in the header file `ctype.h'.
Function: int islower(int c)
Returns true if c is a lower-case letter. The letter need not be
from the Latin alphabet, any alphabet representable is valid.
Function: int isupper(int c)
Returns true if c is an upper-case letter. The letter need not be
from the Latin alphabet, any alphabet representable is valid.
Function: int isalpha(int c)
Returns true if c is an alphabetic character (a letter). If
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.
Function: int isdigit(int c)
Returns true if c is a decimal digit (`0' through `9').
Function: int isalnum(int c)
Returns true if c is an alphanumeric character (a letter or
number); in other words, if either isalpha or isdigit is
true of a character, then isalnum is also true.
Function: int isxdigit(int c)
Returns true if c is a hexadecimal digit.
Hexadecimal digits include the normal decimal digits `0' through
`9' and the letters `A' through `F' and
`a' through `f'.
Function: int ispunct(int c)
Returns true if c is a punctuation character.
This means any printing character that is not alphanumeric or a space
character.
Function: int isspace(int c)
Returns true if c is a whitespace character. In the standard
"C" locale, isspace returns true for only the standard
whitespace characters:
' '
space
'\f'
formfeed
'\n'
newline
'\r'
carriage return
'\t'
horizontal tab
'\v'
vertical tab
Function: int isblank(int c)
Returns true if c is a blank character; that is, a space or a tab.
This function is a GNU extension.
Function: int isgraph(int c)
Returns true if c is a graphic character; that is, a character
that has a glyph associated with it. The whitespace characters are not
considered graphic.
Function: int isprint(int c)
Returns true if c is a printing character. Printing characters
include all the graphic characters, plus the space (`') character.
Function: int iscntrl(int c)
Returns true if c is a control character (that is, a character that
is not a printing character).
Function: int isascii(int c)
Returns true if c is a 7-bit 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'.
Function: int tolower(int c)
If c is an upper-case letter, tolower returns the corresponding
lower-case letter. If c is not an upper-case letter,
c is returned unchanged.
Function: int toupper(int c)
If c is a lower-case letter, toupper returns the corresponding
upper-case letter. Otherwise c is returned unchanged.
Function: int toascii(int c)
This function converts c to a 7-bit 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.
4.3 Character class determination for wide characters
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.
Data type:wctype_t
The 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.
This type is defined in `wctype.h'.
Function: wctype_t wctype(const char *property)
The 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:
"alnum"
"alpha"
"cntrl"
"digit"
"graph"
"lower"
"print"
"punct"
"space"
"upper"
"xdigit"
This function is declared in `wctype.h'.
To test the membership of a character to one of the non-standard classes
the ISO C standard defines a completely new function.
Function: int iswctype(wint_t wc, wctype_t desc)
This function returns a nonzero value if wc is in the character
class specified by desc. desc must previously be returned
by a successful call to wctype.
This function is declared in `wctype.h'.
To make it easier to use the commonly-used classification functions,
they are defined in the C library. There is no need to use
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.
Function: int iswalnum(wint_t wc)
This function returns a nonzero value if wc is an alphanumeric
character (a letter or number); in other words, if either 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'.
Function: int iswalpha(wint_t wc)
Returns true if wc is an alphabetic character (a letter). If
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'.
Function: int iswcntrl(wint_t wc)
Returns true if wc is a control character (that is, a character that
is not a printing character).
This function can be implemented using
iswctype (wc, wctype ("cntrl"))
It is declared in `wctype.h'.
Function: int iswdigit(wint_t wc)
Returns true if wc is a digit (e.g., `0' through `9').
Please note that this function does not only return a nonzero value for
decimal digits, but for all kinds of digits. A consequence is
that code like the following will not work unconditionally for
wide characters:
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'.
Function: int iswgraph(wint_t wc)
Returns true if wc is a graphic character; that is, a character
that has a glyph associated with it. The whitespace characters are not
considered graphic.
This function can be implemented using
iswctype (wc, wctype ("graph"))
It is declared in `wctype.h'.
Function: int iswlower(wint_t wc)
Returns true if wc is a lower-case letter. The letter need not be
from the Latin alphabet, any alphabet representable is valid.
This function can be implemented using
iswctype (wc, wctype ("lower"))
It is declared in `wctype.h'.
Function: int iswprint(wint_t wc)
Returns true if wc is a printing character. Printing characters
include all the graphic characters, plus the space (`') character.
This function can be implemented using
iswctype (wc, wctype ("print"))
It is declared in `wctype.h'.
Function: int iswpunct(wint_t wc)
Returns true if wc is a punctuation character.
This means any printing character that is not alphanumeric or a space
character.
This function can be implemented using
iswctype (wc, wctype ("punct"))
It is declared in `wctype.h'.
Function: int iswspace(wint_t wc)
Returns true if wc is a whitespace character. In the standard
"C" locale, iswspace returns true for only the standard
whitespace characters:
L' '
space
L'\f'
formfeed
L'\n'
newline
L'\r'
carriage return
L'\t'
horizontal tab
L'\v'
vertical tab
This function can be implemented using
iswctype (wc, wctype ("space"))
It is declared in `wctype.h'.
Function: int iswupper(wint_t wc)
Returns true if wc is an upper-case letter. The letter need not be
from the Latin alphabet, any alphabet representable is valid.
This function can be implemented using
iswctype (wc, wctype ("upper"))
It is declared in `wctype.h'.
Function: int iswxdigit(wint_t wc)
Returns true if wc is a hexadecimal digit.
Hexadecimal digits include the normal decimal digits `0' through
`9' and the letters `A' through `F' and
`a' through `f'.
This function can be implemented using
iswctype (wc, wctype ("xdigit"))
It is declared in `wctype.h'.
The GNU C library also provides a function which is not defined in the
ISO C standard but which is available as a version for single byte
characters as well.
Function: int iswblank(wint_t wc)
Returns true if wc is a blank character; that is, a space or a tab.
This function is a GNU extension. It is declared in `wchar.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:
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:
See section 6.3.3 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.
Data Type:wctrans_t
This data type is defined as a scalar type which can hold a value
representing the locale-dependent character mapping. There is no way to
construct such a value apart from using the return value of the
wctrans function.
This type is defined in `wctype.h'.
Function: wctrans_t wctrans(const char *property)
The 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:
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'.
For the generally available mappings, the ISO C standard defines
convenient shortcuts so that it is not necessary to call wctrans
for them.
Function: wint_t towlower(wint_t wc)
If wc is an upper-case letter, 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'.
Function: wint_t towupper(wint_t wc)
If wc is a lower-case letter, 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'.
The same warnings given in the last section for the use of the wide
character classification functions apply here. It is not possible to
simply cast a char type value to a wint_t and use it as an
argument to towctrans calls.