C Standard Library -<ctype.h>

Introduction

C Standard Library'sctype.hThe header file provides some functions that can be used to test and convert characters. These functions are mainly used to check character types (such as letters, digits, whitespace characters, etc.) and to perform character case conversion.

<ctype.h>It provides a convenient set of functions for handling character classification and conversion operations, and is an important tool for character operations in the C standard library.

The following is a simple example demonstrating how to use<ctype.h>Functions provided:

Example

#include <stdio.h>
#include <ctype.h>

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 result 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 acceptintas an argument, 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.

Serial NumberFunction & Description
1int isalnum(int c)
This function checks whether the passed character is a letter or a digit.
2int isalpha(int c)
This function checks whether the passed character is a letter.
3int iscntrl(int c)
This function checks whether the passed character is a control character.
4int isdigit(int c)
This function checks whether the passed character is a decimal digit.
5int isgraph(int c)
This function checks whether the passed character has a graphical representation.
6int islower(int c)
This function checks whether the passed character is a lowercase letter.
7int isprint(int c)
This function checks whether the passed character is printable.
8int ispunct(int c)
This function checks whether the passed character is a punctuation character.
9int isspace(int c)
This function checks whether the passed character is a whitespace character.
10int isupper(int c)
This function checks whether the passed character is an uppercase letter.
11int isxdigit(int c)
This function checks whether the passed character is a hexadecimal digit.

The standard library also includes two conversion functions that accept and return an "int".

Serial NumberFunction & Description
1int tolower(int c)
This function converts an uppercase letter to a lowercase letter.
2int toupper(int c)
This function converts a lowercase letter to an uppercase letter.

character class

Serial NumberCharacter classes & description
1Numbers
Complete set of digits { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }
2Hexadecimal digits
Set { 0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f }
3Lowercase letters
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 }
4Uppercase letters
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 }
5Letter
Set of lowercase and uppercase letters
6Alphanumeric character
The set of digits, lowercase letters, and uppercase letters.
7Punctuation character
Set ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~
8Graphic character
The set of alphanumeric characters and punctuation characters.
9Space character
The set of tab, newline, vertical tab, form feed, carriage return, and space characters.
10Printable character
The set of alphanumeric characters, punctuation characters, and space characters.
11Control character
In ASCII encoding, the octal codes for these characters are from 000 to 037, as well as 177 (DEL).
12whitespace character
Includes space and tab characters.
13Alphabetic character
Set of lowercase and uppercase letters.
other extensions