The <ctype.h> header file defines a set of prototypes for character handling functions.

Character Testing Functions

These functions are used to determine whether a character belongs to a specific type:

  • isalnum(): Checks if the character is alphanumeric.
  • isalpha(): Checks if the character is a letter.
  • isdigit(): Checks if the character is a digit.
  • isxdigit(): Checks if the character is a hexadecimal digit.
  • islower(): Checks if the character is a lowercase letter.
  • isupper(): Checks if the character is an uppercase letter.
  • isblank(): Checks if the character is a standard whitespace character (including space, horizontal tab, or newline).
  • isspace(): Checks if the character is any whitespace character (such as space, newline, form feed, carriage return, vertical tab, or horizontal tab).
  • iscntrl(): Checks if the character is a control character (e.g., Ctrl + B).
  • isprint(): Checks if the character is a printable character.
  • isgraph(): Checks if the character is any printable character except space.
  • ispunct(): Checks if the character is a punctuation character (any printable character except space, letter, or digit).

These functions take an int parameter (not a char) because they also accept EOF as a valid argument.

If the character belongs to the specified type, the function returns a non-zero integer (typically 1, indicating true); otherwise, it returns 0 (indicating false).

Here’s an example: the user inputs a character, and the program checks if it is an English letter.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <ctype.h>

int main(void) {
char ch = getchar();

if (isalpha(ch))
printf("it is an alpha character.\n");
else
printf("it is not an alpha character.\n");

return 0;
}

Character Mapping Functions

These functions return a corresponding form of a character, primarily consisting of two functions:

  • tolower(): If the argument is an uppercase character, it returns the lowercase equivalent; otherwise, it returns the original character.
  • toupper(): If the argument is a lowercase character, it returns the uppercase equivalent; otherwise, it returns the original character.
1
2
// Convert character to uppercase
ch = toupper(ch);

Note that these functions do not modify the original character.