Skip to content
Tutorials Center
My learning

Topics

C Reference ยท ctype.h, math.h and others

C isalpha(), isdigit() and isalnum() functions

Reference for C isalpha(), isdigit(), isalnum() and isxdigit() from ctype.h: test character classes, the unsigned char rule and locale effects.

The functions in <ctype.h> classify single characters. isalpha() tests for letters, isdigit() for the decimal digits 0 to 9, isalnum() for either, and isxdigit() for hexadecimal digits (0-9, a-f, A-F). Each returns zero if the character is not in the class, and a non-zero value (not necessarily 1) if it is.

They are the building blocks of tokenizers, input validators and parsers: read a character, decide which kind it is, and act on it.

C
#include <ctype.h>

int isalpha(int c);
int isdigit(int c);
int isalnum(int c);
int isxdigit(int c);

Try it

C Classifying the characters of a string

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Validating an identifier and converting a digit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
int isalpha(int c);
int isdigit(int c);
int isalnum(int c);
int isxdigit(int c);
Name Description
c A value representable as unsigned char, or the macro EOF.

Returns: non-zero if c is in the class, otherwise zero. Compare against zero, not against 1.

Errors / undefined behavior: if c is neither EOF nor in the range of unsigned char, the behavior is undefined. The classic way to trigger this is passing a char that holds a negative value, such as an accented letter on a platform where char is signed.

Usage notes

  • Cast to unsigned char. Write isalpha((unsigned char)ch) whenever ch is a char. Without the cast, a byte like 0xE9 becomes a negative int and the call is undefined. Functions such as getchar and fgetc already return the right range, so no cast is needed there.
  • ASCII in the "C" locale. isalpha is true only for A-Z and a-z. After setlocale() selects another locale, isalpha may also accept single-byte letters such as รฉ. It does not understand UTF-8 multi-byte characters; for those, use the wide functions in <wctype.h> or a Unicode library.
  • isdigit is locale-independent. It is true only for 0 to 9 in every locale, and the standard requires these to be consecutive, so c - '0' always works. Letters have no such guarantee in theory (EBCDIC has gaps), so do not write c - 'a' loops if portability to such systems matters.
  • isalnum(c) equals isalpha(c) || isdigit(c).
  • isxdigit: accepts both cases. Convert the digit with a lookup or with strtol on a small buffer.
  • Macros. Implementations may define these as macros that evaluate their argument more than once, so do not pass an expression with side effects such as isalpha(*p++). Use a separate variable.
  • Cost. Usually a table lookup, so very cheap.
  • Not a number check. isdigit tests one character. To check that a whole string is a number, use strtol and inspect the end pointer.
  • Standard version. All four are in C89. The locale-aware behavior of isalpha is implementation-defined outside the "C" locale.