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

C ctype.h Functions

The ctype.h library has helpers that check what kind of character you have, such as a letter, digit, or space, and can change its case. It is like a sorter for characters.
Syntax
c
#include <ctype.h>

isalpha(character);
isdigit(character);
toupper(character);
tolower(character);

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 standard input/output (printf, scanf)
#include <stdio.h>
// Include character-test functions (isdigit, toupper, ...)
#include <ctype.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", isalpha('A'));
	// Return 0 to signal that the program finished successfully
	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 standard input/output (printf, scanf)
#include <stdio.h>
// Include character-test functions (isdigit, toupper, ...)
#include <ctype.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", isdigit('7'));
	// Return 0 to signal that the program finished successfully
	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 standard input/output (printf, scanf)
#include <stdio.h>
// Include character-test functions (isdigit, toupper, ...)
#include <ctype.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d %d", isalnum('A'), isalnum('!'));
	// Return 0 to signal that the program finished successfully
	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 standard input/output (printf, scanf)
#include <stdio.h>
// Include character-test functions (isdigit, toupper, ...)
#include <ctype.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%c %c", tolower('A'), toupper('b'));
	// Return 0 to signal that the program finished successfully
	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 standard input/output (printf, scanf)
#include <stdio.h>
// Include character-test functions (isdigit, toupper, ...)
#include <ctype.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d %d", isspace(' '), isspace('a'));
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting #include <ctype.h> before using isalpha or toupper.
  2. Passing a negative char value to these functions, which is undefined unless cast to unsigned char.
  3. Expecting toupper to change a whole string in place, when it converts one character.

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.