C ctype.h Functions
In this page:
Checking for Alphabets
isalpha() checks whether a character falls in the ranges A-Z or a-z and returns a non-zero value if it does, which is the standard way to validate that user input contains only letters before processing it as a name or word rather than a number or symbol.
Example: Checking for Alphabets
#include <stdio.h>
#include <ctype.h>
int main() {
printf("%d", isalpha('A'));
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Checking for Digits
isdigit() checks whether a character is one of the decimal digits 0 through 9, which is useful for validating that a piece of input meant to represent a number doesn't actually contain letters or punctuation before you try to convert it with something like atoi().
Example: Checking for Digits
#include <stdio.h>
#include <ctype.h>
int main() {
printf("%d", isdigit('7'));
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Checking for Alphanumeric
isalnum() checks whether a character is either a letter or a digit, combining what isalpha() and isdigit() each check individually, which is convenient when validating things like usernames or identifiers that should allow both letters and numbers but reject punctuation.
Example: Checking for Alphanumeric
#include <stdio.h>
#include <ctype.h>
int main() {
printf("%d %d", isalnum('A'), isalnum('!'));
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Case Conversions
tolower() converts an uppercase letter to its lowercase equivalent and leaves any character that isn't an uppercase letter unchanged, while toupper() does the reverse conversion — both are the standard building blocks for case-insensitive string comparisons in C.
Example: Case Conversions
#include <stdio.h>
#include <ctype.h>
int main() {
printf("%c %c", tolower('A'), toupper('b'));
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Checking for Whitespace
isspace() checks whether a character is one of the recognized whitespace characters (space, tab, newline, carriage return, and a couple of others), which is commonly used while parsing text to skip over or trim leading and trailing blank characters.
Example: Checking for Whitespace
#include <stdio.h>
#include <ctype.h>
int main() {
printf("%d %d", isspace(' '), isspace('a'));
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- C typedef
- C Type Casting
- C Bit Fields
- C Variable Length Arrays
- C Command Line Arguments
- C Function Pointers
- C Callback Functions
- C Multidimensional Pointer
- C string.h Functions
- C stdlib.h Functions
- C math.h Functions
- C time.h Functions
- C ctype.h Functions
- C errno.h
- C assert.h
- C Error Handling
- C Debugging Techniques
- C Code Style & Best Practices
- C Common Mistakes
- C Interview Questions