isalpha() / isdigit() / isspace() / isalnum()
These functions check whether a character belongs to a category such as letters, digits, or whitespace. They are defined in <ctype.h> and are commonly used for string validation and lexical analysis.
Syntax
// All functions take an unsigned char value (or EOF) as the argument. // Return value: non-zero (true) if the condition is met, 0 (false) otherwise. int isalpha(int c); // Checks whether c is an alphabetic letter (a-z, A-Z). int isdigit(int c); // Checks whether c is a decimal digit (0-9). int isalnum(int c); // Checks whether c is a letter or a digit. int isspace(int c); // Checks whether c is a whitespace character (space, tab, newline, etc.). int isupper(int c); // Checks whether c is an uppercase letter (A-Z). int islower(int c); // Checks whether c is a lowercase letter (a-z). int ispunct(int c); // Checks whether c is a punctuation or symbol character. int isprint(int c); // Checks whether c is a printable character (including space). int iscntrl(int c); // Checks whether c is a control character.
Character classification functions
| Function | Characters | Description |
|---|---|---|
| isalpha() | a-z, A-Z | Returns true if the character is alphabetic. May include locale-dependent characters such as accented letters. |
| isdigit() | 0-9 | Returns true if the character is an ASCII decimal digit. Not locale-dependent. |
| isalnum() | a-z, A-Z, 0-9 | Returns true if either isalpha() or isdigit() would return true. |
| isspace() | ' ', '\t', '\n', '\r', '\f', '\v' | Returns true if the character is a whitespace character. |
| isupper() | A-Z | Returns true if the character is an uppercase letter. |
| islower() | a-z | Returns true if the character is a lowercase letter. |
| ispunct() | !@#… etc. | Returns true if the character is printable but is neither a space nor an alphanumeric character. |
Sample code
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(void) {
const char *str = "Hello, World! 123";
int alpha = 0, digit = 0, space = 0, punct = 0;
// Classify and count each character in the string.
for (int i = 0; str[i] != '\0'; i++) {
unsigned char c = (unsigned char)str[i]; // Cast to pass safely.
if (isalpha(c)) alpha++;
else if (isdigit(c)) digit++;
else if (isspace(c)) space++;
else if (ispunct(c)) punct++;
}
printf("Letters: %d, Digits: %d, Spaces: %d, Punctuation: %d\n",
alpha, digit, space, punct);
// Outputs: "Letters: 10, Digits: 3, Spaces: 3, Punctuation: 2"
// Validate an identifier (starts with a letter or underscore, followed by letters, digits, or underscores).
const char *id = "my_var_1";
int valid = isalpha((unsigned char)id[0]) || id[0] == '_';
for (size_t i = 1; i < strlen(id) && valid; i++) {
valid = isalnum((unsigned char)id[i]) || id[i] == '_';
}
printf("'%s' is a %s identifier.\n", id, valid ? "valid" : "invalid");
// Outputs: "'my_var_1' is a valid identifier."
return 0;
}
Notes
Passing a plain char value directly can cause undefined behavior if the value is negative (as with signed char). Always cast to (unsigned char) before passing the argument.
These functions may be affected by the locale (regional settings). In particular, isalpha() may classify characters outside the ASCII range as alphabetic depending on the locale. If you are only working with ASCII characters, locale dependency is not a concern.
For converting characters between uppercase and lowercase, see tolower() / toupper().
If you find any errors or copyright issues, please contact us.