C ctype.h Library

C Tutorial

C ctype.h Library

In C ctype.h Library provides functions to test and manipulate characters. It’s very useful for validating input, parsing text, and formatting strings.

All functions in ctype.h work with int values representing characters (usually char).


📌 Common Functions in ctype.h

1️⃣ Character Testing Functions

FunctionDescriptionExample
isalnum(int c)Checks if character is alphanumeric (a-z, A-Z, 0-9)isalnum('A') → true
isalpha(int c)Checks if character is alphabeticisalpha('1') → false
isdigit(int c)Checks if character is a digitisdigit('5') → true
islower(int c)Checks if character is lowercaseislower('a') → true
isupper(int c)Checks if character is uppercaseisupper('B') → true
isspace(int c)Checks for whitespace (' ', '\t', '\n')isspace(' ') → true
ispunct(int c)Checks if character is punctuationispunct('!') → true
isxdigit(int c)Checks if character is hexadecimal digitisxdigit('F') → true
isprint(int c)Checks if character is printableisprint('\n') → false
isgraph(int c)Checks if character has graphic representationisgraph(' ') → false

2️⃣ Character Conversion Functions

FunctionDescriptionExample
tolower(int c)Converts uppercase to lowercasetolower('A')'a'
toupper(int c)Converts lowercase to uppercasetoupper('b')'B'

3️⃣ Example: Character Validation and Conversion


 

Sample Output:

Enter a character: a
a is a letter.
Uppercase: A
Lowercase: a

4️⃣ Example: Count Character Types in a String


 

Output:

Letters: 5
Digits: 3
Spaces: 2
Punctuation: 1

📌 Summary Table

FunctionPurpose
isalnumChecks alphanumeric
isalphaChecks alphabetic
isdigitChecks digits
islowerChecks lowercase letters
isupperChecks uppercase letters
isspaceChecks whitespace characters
ispunctChecks punctuation
isxdigitChecks hexadecimal digit
isprintChecks printable characters
isgraphChecks graphic characters
tolowerConverts to lowercase
toupperConverts to uppercase

✅ Notes

  1. Functions expect int values, usually casted from char.

  2. Always include #include <ctype.h>.

  3. Useful for input validation, parsing, and formatting strings.

  4. Can be combined with string.h for advanced string processing.

You may also like...