Let's examine some simple library functions and see how they are used.
Some of the functions that are available on GNU systems for handling
individual characters are described below. They are all macros, so the
usual caveats about macro parameters apply. (See Macro functions.)
All of the functions below accept single variables of type char
as parameters. To use any of them, you must include the system header
file ctype.h; the library used is simply glibc,
which is linked automatically.
isalnum
Returns true if and only if the parameter is alphanumeric: that is, an
alphabetic character (see isalpha) or a digit (see
isdigit).
isalpha
Returns true if and only if the parameter is alphabetic. An
alphabetic character is any character from A through
Z or a through z.
isascii
Returns true if and only if the parameter is a valid ASCII character:
that is, it has an integer value in the range 0 through 127. (Remember,
the char type in C is actually a kind of integer!)
iscntrl
Returns true if and only if the parameter is a control character.
Control characters vary from system to system, but are usually defined
as characters in the range 0 to 31.
isdigit
Returns true if and only if the parameter is a digit in the range 0
through 9.
isgraph
Returns true if and only if the parameter is graphic: that is, if the
character is either alphanumeric (see isalnum) or punctuation
(see ispunct). All graphical characters are valid ASCII
characters, but ASCII also includes non-graphical characters such as
control characters (see iscntrl) and whitespace (see
isspace).
islower
Returns true if and only if the parameter is a lower-case alphabetic
character (see isalpha).
isprint
Returns true if and only if the parameter is a printable character: that
is, the character is either graphical (see isgraph) or a space
character.
ispunct
Returns true if and only if the parameter is a punctuation character.
isspace
Returns true if and only if the parameter is a whitespace character.
What is defined as whitespace varies from system to system, but it
usually includes space characters and tab characters, and sometimes
newline characters.
isupper
Returns true if and only if the parameter is an upper-case alphabetic
character (see isalpha).
isxdigit
Returns true if and only if the parameter is a valid hexadecimal digit:
that is, a decimal digit (see isdigit), or a letter from a
through f or A through F.
toascii
Returns the parameter stripped of its eighth bit, so that it has an
integer value from 0 through 127 and is therefore a valid ASCII
character. (See isascii.)
tolower
Converts a character into its lower-case counterpart. Does not affect
characters which are already in lower case.
toupper
Converts a character into its upper-case counterpart. Does not affect
characters which are already in upper case.
/********************************************************/
/* */
/* Demonstration of character utility functions */
/* */
/********************************************************/
#include <stdio.h>
#include <ctype.h>
#define allchars ch = 0; isascii(ch); ch++
int main () /* A criminally long main program! */
{
char ch;
printf ("\n\nVALID CHARACTERS FROM isgraph:\n\n");
for (allchars)
{
if (isgraph(ch))
{
printf ("%c ",ch);
}
}
printf ("\n\nVALID CHARACTERS FROM isalnum:\n\n");
for (allchars)
{
if (isalnum(ch))
{
printf ("%c ",ch);
}
}
printf ("\n\nVALID CHARACTERS FROM isalpha:\n\n");
for (allchars)
{
if (isalpha(ch))
{
printf ("%c ",ch);
}
}
printf ("\n\nVALID CHARACTERS FROM isupper:\n\n");
for (allchars)
{
if (isupper(ch))
{
printf ("%c ",ch);
}
}
printf ("\n\nVALID CHARACTERS FROM islower:\n\n");
for (allchars)
{
if (islower(ch))
{
printf ("%c ",ch);
}
}
printf ("\n\nVALID CHARACTERS FROM isdigit:\n\n");
for (allchars)
{
if (isdigit(ch))
{
printf ("%c ",ch);
}
}
printf ("\n\nVALID CHARACTERS FROM isxdigit:\n\n");
for (allchars)
{
if (isxdigit(ch))
{
printf ("%c ",ch);
}
}
printf ("\n\nVALID CHARACTERS FROM ispunct:\n\n");
for (allchars)
{
if (ispunct(ch))
{
printf ("%c ",ch);
}
}
printf ("\n\n");
return 0;
}
The output of the above code example is as follows:
VALID CHARACTERS FROM isgraph:
! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D
E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h
i j k l m n o p q r s t u v w x y z { | } ~
VALID CHARACTERS FROM isalnum:
0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m n o p q r s t u v w x y z
VALID CHARACTERS FROM isalpha:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j
k l m n o p q r s t u v w x y z
VALID CHARACTERS FROM isupper:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
VALID CHARACTERS FROM islower:
a b c d e f g h i j k l m n o p q r s t u v w x y z
VALID CHARACTERS FROM isdigit:
0 1 2 3 4 5 6 7 8 9
VALID CHARACTERS FROM isxdigit:
0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f
VALID CHARACTERS FROM ispunct:
! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~