Several functions are provided to test whether
characters belong to certain sets or classifications,
such as upper-case, letter, digit, etc.… These classifications
depend on the LC_
locale
category.
#include <ctype.h>
int isalpha(int c);
int islower(int c);
int isupper(int c);
int isdigit(int c);
int isxdigit(int c);
int isalnum(int c);
int isblank(int c);
int isspace(int c);
int iscntrl(int c);
int isgraph(int c);
int isprint(int c);
int ispunct(int c);
#include <wctype.h>
int iswalpha(wint_t c);
int iswlower(wint_t c);
int iswupper(wint_t c);
int iswdigit(wint_t c);
int iswxdigit(wint_t c);
int iswalnum(wint_t c);
int iswblank(wint_t c);
int iswspace(wint_t c);
int iswcntrl(wint_t c);
int iswgraph(wint_t c);
int iswprint(wint_t c);
int iswpunct(wint_t c);
Functions with names matching ^is[a-z]
might be added to
<ctype.h>
or <wctype.h>
.
Single-byte characters supplied as char
should be converted to unsigned char
before
passing to the is… functions, e.g.,
isalnum((unsigned
char)
c)
. Functions like getchar
return a character
already in this form. The wide-character functions need
no such conversion.
Each function returns non-zero if the supplied
character c
belongs to the set
implied by the function's name:
Name | Description | Definition | Examples in the C locale |
---|---|---|---|
upper | upper-case letters | U+0041 through U+005A, plus locale-specific && !isdigit(c)
&& !ispunct(c) && !isspace(c) &&
!iscntrl(c) |
"ABCDEFGHIJKLM"
"NOPQRSTUVWXYZ" |
lower | lower-case letters | U+0061 through U+007A, plus locale-specific && !isdigit(c)
&& !ispunct(c) && !isspace(c) &&
!iscntrl(c) |
"abcdefghijklm"
"nopqrstuvwxyz" |
alpha | letters | islower(c) || isupper(c) ||
(locale-specific && !isdigit(c) &&
!ispunct(c) && !isspace(c) &&
!iscntrl(c)) |
|
digit | base-10 digits | U+0030 through U+0039 | "0123456789" |
xdigit | base-16 digits | U+0030 through U+0039, U+0041 through U+0046, U+0031 through U+0036 | "0123456789" "abcdef" "ABCDEF" |
alnum | letters and digits | isalpha(c) ||
isdigit(c) |
"0123456789" "abcdefghijklm" "nopqrstuvwxyz" "ABCDEFGHIJKLM" "NOPQRSTUVWXYZ" |
punct | punctuation characters | (locale-specific
&& !isspace(c) && !isalnum(c)) |
"!\"#%&'()*+,-./:;"
"<=>?[\]^_{|}~" |
printing characters | |||
cntrl | control characters | ||
graph | graphic characters | isprint(c) && c != '
' |
|
space | whitespace characters | (locale-specific
&& !isalnum(c)) |
" \f\n\r\t\v" |
blank | blank characters | (locale-specific word
separators within a line &&
isspace(c)) |
" \t" (U+0020 and
U+0009) |
For wide characters, it is possible to represent these categories textually:
#include <wctype.h>
wctype_t wctype(const char *prop);
int iswctype(wint_t c, wctype_t prop);
For example,
iswctype(c, wctype("xdigit"))
has the same behaviour as
iswxdigit(c)
. If the string prop
is not recognized, wctype
returns zero, identifying a class that no character
belongs to.
Depending on the LC_
category of the
current locale, additional classifications may be
recognized. Also, the locale is recorded in the
wctype_t
value, so even if LC_
is changed between
the calls to wctype
and
iswctype
, the second call works as if the
old setting applies.