Node:Character handling, Previous:Common library functions, Up:Common library functions
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
isalpha
) or a digit (see
isdigit
).
isalpha
A
through
Z
or a
through z
.
isascii
char
type in C is actually a kind of integer!)
iscntrl
isdigit
isgraph
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
isalpha
).
isprint
isgraph
) or a space
character.
ispunct
isspace
isupper
isalpha
).
isxdigit
isdigit
), or a letter from a
through f
or A
through F
.
toascii
isascii
.)
tolower
toupper
/********************************************************/ /* */ /* 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: ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~