C Standard Library -
Introduction
The C standard library's ctype.h The header file provides functions that can be used to test and convert characters. These functions are mainly used to check the type of a character (such as letters, digits, whitespace characters, etc.) and to perform case conversion.
Provides a convenient set of functions for character classification and conversion, and is an important tool in the C standard library for handling character operations.
The following is a simple example demonstrating how to use the functions provided:
Example
#include
#include
int
main
(
)
{
char
ch
;
// Example characters
char
chars
[
]
=
"a1 B? \n"
;
// Check the type of each character
for
(
int
i
=
0
;
chars
[
i
]
!
=
'\0'
;
i
++
)
{
ch
=
chars
[
i
]
;
printf
(
"Character: '%c'\n"
, ch
)
;
if
(
isalpha
(
ch
)
)
{
printf
(
" - isalpha: Yes\n"
)
;
}
else
{
printf
(
" - isalpha: No\n"
)
;
}
if
(
isdigit
(
ch
)
)
{
printf
(
" - isdigit: Yes\n"
)
;
}
else
{
printf
(
" - isdigit: No\n"
)
;
}
if
(
isspace
(
ch
)
)
{
printf
(
" - isspace: Yes\n"
)
;
}
else
{
printf
(
" - isspace: No\n"
)
;
}
if
(
isprint
(
ch
)
)
{
printf
(
" - isprint: Yes\n"
)
;
}
else
{
printf
(
" - isprint: No\n"
)
;
}
if
(
ispunct
(
ch
)
)
{
printf
(
" - ispunct: Yes\n"
)
;
}
else
{
printf
(
" - ispunct: No\n"
)
;
}
}
// Character case conversion example
char
lower
=
'a'
;
char
upper
=
'A'
;
printf
(
"tolower('%c') = '%c'\n"
, upper,
tolower
(
upper
)
)
;
printf
(
"toupper('%c') = '%c'\n"
, lower,
toupper
(
lower
)
)
;
return
0
;
}
The output is:
Character: 'a'
- isalpha: Yes
- isdigit: No
- isspace: No
- isprint: Yes
- ispunct: No
Character: '1'
- isalpha: No
- isdigit: Yes
- isspace: No
- isprint: Yes
- ispunct: No
Character: ' '
- isalpha: No
- isdigit: No
- isspace: Yes
- isprint: Yes
- ispunct: No
Character: 'B'
- isalpha: Yes
- isdigit: No
- isspace: No
- isprint: Yes
- ispunct: No
Character: '?'
- isalpha: No
- isdigit: No
- isspace: No
- isprint: Yes
- ispunct: Yes
Character: '
'
- isalpha: No
- isdigit: No
- isspace: Yes
- isprint: No
- ispunct: No
tolower('A') = 'a'
toupper('a') = 'A'
Library functions
The functions defined in the header file ctype.h are listed below.
These functions are used to test whether a character belongs to a certain type. They accept int as a parameter, whose value must be EOF or representable as an unsigned char.
If the argument c satisfies the described condition, these functions return nonzero (true). If the argument c does not satisfy the described condition, these functions return zero.
| No. | Function & Description |
|---|---|
| 1 | int isalnum(int c) |
| This function checks whether the passed character is alphanumeric. | |
| 2 | int isalpha(int c) |
| This function checks whether the passed character is a letter. | |
| 3 | int iscntrl(int c) |
| This function checks whether the passed character is a control character. | |
| 4 | int isdigit(int c) |
| This function checks whether the passed character is a decimal digit. | |
| 5 | int isgraph(int c) |
| This function checks whether the passed character has a graphical representation. | |
| 6 | int islower(int c) |
| This function checks whether the passed character is a lowercase letter. | |
| 7 | int isprint(int c) |
| This function checks whether the passed character is printable. | |
| 8 | int ispunct(int c) |
| This function checks whether the passed character is a punctuation character. | |
| 9 | int isspace(int c) |
| This function checks whether the passed character is a whitespace character. | |
| 10 | int isupper(int c) |
| This function checks whether the passed character is an uppercase letter. | |
| 11 | int isxdigit(int c) |
| This function checks whether the passed character is a hexadecimal digit. |
The standard library also includes two conversion functions, which accept and return an "int"
| No. | Function & Description |
|---|---|
| 1 | int tolower(int c) |
| This function converts an uppercase letter to a lowercase letter. | |
| 2 | int toupper(int c) |
| This function converts a lowercase letter to an uppercase letter. |
Character class
| No. | Character class & description |
|---|---|
| 1 | Digit |
| The complete set of digits { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } | |
| 2 | Hexadecimal digits |
| The set { 0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f } | |
| 3 | Lowercase letters |
| The set { 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 } | |
| 4 | Uppercase letters |
| The set {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 } | |
| 5 | Common Format Patterns |
| The set of lowercase and uppercase letters | |
| 6 | Alphanumeric characters |
| The set of digits, lowercase letters, and uppercase letters | |
| 7 | Punctuation characters |
| The set ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~ | |
| 8 | Graphical characters |
| The set of alphanumeric and punctuation characters | |
| 9 | Space characters |
| The set of tab, newline, vertical tab, form feed, carriage return, and space characters. | |
| 10 | Printable characters |
| The set of alphanumeric, punctuation, and space characters. | |
| 11 | Control characters |
| In ASCII encoding, the octal codes of these characters are from 000 to 037, and 177 (DEL). | |
| 12 | Whitespace character |
| Includes the space character and the tab character. | |
| 13 | Letter characters |
| The set of lowercase and uppercase letters. |