ctype.h in C

Overview
Every character belongs to a specific character type, such as alphabets, digits, punctuation, etc. The ctype header file in C declares various functions to check whether a character belongs to a particular character class. It also contains functions to transform the characters.
Introduction
The syntax to include ctype is:
or
What is ctype.h in C?
The ctype header file in C defines various functions to classify and transform characters. These functions take an integer(ASCII value of the character) as an input. If the input is not an integer, it is typecasted internally in the function.
Character Classes
A character class is a set of characters belonging to some type. In this section, we will see the various character sets present in C.
-
Digits: It is the set of numeric characters, i.e.,{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}.
-
Hexadecimal digits: It is the set of hexadecimal digits {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F}. The hexadecimal number system is the type of number system which has a base of 16.
-
Lowercase letters: It is the set of lowercase alphabets {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}.
-
Uppercase letters: It is the set of uppercase alphabets {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}.
-
Letters: It is the set of all alphabets, uppercase, and lowercase.
-
Alphanumeric characters: It is the set of all alphabets(uppercase and lowercase) and digits.
-
Punctuation characters: It is the set of all punctuation characters ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ | ~ {}
-
Graphical characters: It is the set of all characters with a graphical representation. This includes letters, digits, and punctuation characters.
Note: The whitespace characters (' ') are not considered graphical characters.
-
Space characters: It is the set of characters that produces a white space. These are as follows.
Character Description ' ' space '\n' newline '\t' horizontal tab '\v' vertical tab '\f' form feed '\r' Carriage return -
Printable characters: The set of alphabets(lowercase or uppercase), numbers, punctuation characters, and space characters.
-
Control characters: Control characters are those who are not printed on the screen. Some examples are backspace, tab, shift, etc.
-
Blank characters: It is the set of space character(' ') and tab character('\t').
-
Alphabetic characters: The set of all alphabets, uppercase, and lowercase.
The Functions Defined in the Header ctype.h
-
isalnum: This function returns a non-zero number if the argument is an alphabet('a' - 'z' or 'A' - 'Z') or a number('0' - '9'). It returns zero in all other cases.
Syntax:
-
isalpha: This function returns a non-zero number if the argument is an alphabet('a' - 'z' or 'A' - 'Z') and zero otherwise.
Syntax:
-
iscntrl: This function returns a non-zero number if the argument is a control character and zero otherwise.
Syntax:
-
isdigit: This function returns a non-zero number if the argument is a numeric character('0' - '9') and zero otherwise.
Syntax:
-
isgraph: This function returns a non-zero number if the argument is a graphic character and zero otherwise.
Syntax:
-
islower: This function returns a non-zero number if the argument is a lowercase alphabet character('a' - 'z') and zero otherwise.
Syntax:
-
isprint: This function returns a non-zero number if the argument is a printable character and zero otherwise.
Syntax:
-
ispunct: This function returns a non-zero number if the argument is a punctuation character and zero otherwise.
Syntax:
-
isspace: This function returns a non-zero number if the argument belongs to the space character class and zero otherwise.
Syntax:
-
isupper: This function returns a non-zero number if the argument is an uppercase alphabet character('A' - 'Z') and zero otherwise.
Syntax:
-
isxdigit: This function returns a non-zero number if the argument is a hexadecimal character and zero otherwise.
Syntax:
-
tolower: If the argument is an uppercase alphabet, the function converts it into a lowercase alphabet. Else, it returns the same character.
Syntax:
-
toupper: If the argument is a lowercase alphabet, the function converts it into an uppercase alphabet. Else, it returns the same character.
Syntax:
Conclusion
- The ctype header file in C defines functions to classify and transform characters in various character classes.
- A character class is a set of characters belonging to the same type.
- These functions take an integer(ASCII value of the character) as an input. If the input is not an integer, it is typecasted internally in the function.