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.
#include <ctype.h> int isalpha(int c); int isdigit(int c); int isalnum(int c); int isxdigit(int c);
Try it
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.
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
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. Writeisalpha((unsigned char)ch)wheneverchis achar. Without the cast, a byte like0xE9becomes a negativeintand the call is undefined. Functions such asgetcharandfgetcalready return the right range, so no cast is needed there. - ASCII in the
"C"locale.isalphais true only forA-Zanda-z. Aftersetlocale()selects another locale,isalphamay 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. isdigitis locale-independent. It is true only for0to9in every locale, and the standard requires these to be consecutive, soc - '0'always works. Letters have no such guarantee in theory (EBCDIC has gaps), so do not writec - 'a'loops if portability to such systems matters.isalnum(c)equalsisalpha(c) || isdigit(c).isxdigit: accepts both cases. Convert the digit with a lookup or withstrtolon 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.
isdigittests one character. To check that a whole string is a number, usestrtoland inspect the end pointer. - Standard version. All four are in C89. The locale-aware behavior of
isalphais implementation-defined outside the"C"locale.