C ctype.h Functions
In this page:
#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
// 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;
}
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 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;
}
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 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;
}
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 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;
}
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 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;
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting
#include <ctype.h>before usingisalphaortoupper. - Passing a negative
charvalue to these functions, which is undefined unless cast tounsigned char. - Expecting
toupperto change a whole string in place, when it converts one character.
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