← Back to C Course | Chapter 12: Advanced Topics | Lesson 13 of 20

C ctype.h Functions

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

c
#include <stdio.h>
#include <ctype.h>
int main() {
	printf("%d", isalpha('A'));
	return 0;
}

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

c
#include <stdio.h>
#include <ctype.h>
int main() {
	printf("%d", isdigit('7'));
	return 0;
}

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

c
#include <stdio.h>
#include <ctype.h>
int main() {
	printf("%d %d", isalnum('A'), isalnum('!'));
	return 0;
}

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

c
#include <stdio.h>
#include <ctype.h>
int main() {
	printf("%c %c", tolower('A'), toupper('b'));
	return 0;
}

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

c
#include <stdio.h>
#include <ctype.h>
int main() {
	printf("%d %d", isspace(' '), isspace('a'));
	return 0;
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.