Ctype Functions: isalnum(), isalpha() and isdigit()


The ctype functions isalnum(), isalpha(), and isdigit() examine the alphanumeric ASCII characters. Each of these functions are related, so I cover them together in this post that continues my exploration of the ctype functions.

The relationship between these functions works because the positive results of isalpha() and idigit() are useful in crafting the isalnum() function: If a character is both alphabetic (isalpha()) and a digit (isdigit()), then it’s also alphanumeric (isalnum()).

The following code outputs the printable ASCII characters, from the space (code 32 or 0x20) through the DEL (code 127 or 0x7F). ASCII code 127 is “DEL” or delete, which doesn’t print but isn’t the same as a space. (More on this condition in another Lesson.) For each line, text is output to describe whether the character is alphanumeric and if so whether it’s alphabetic or a digit.

2026_05_23-Lesson-a.c

#include <stdio.h>
#include <ctype.h>

#define DEL 127

int main()
{
    int ch;

    for( ch=' '; ch<=DEL; ch++ )
    {
        printf("%3d %2X '%c' is",ch,ch,ch);
        if( isalnum(ch) )
        {
            printf(" alphanumeric");
            if( isalpha(ch) )
                printf(" (alphabetic)");
            if( isdigit(ch) )
                printf(" (digit)");
        }
        else
        {
            printf(" non-alphanumeric");
        }
        putchar('\n');
    }

    return 0;
}

The code uses the three ctype functions: isalnum(), isalpha(), and isdigit(). A for loop plows through characters ' ' through '~'. Various printf() statements output the qualities of these characters, specifically tagging alphanumeric and non-alphanumeric, which alphabetic and digit characters called out separately.

I’m unhappy with the nested if statements, but given the condition that the character is either alphabetic or digit, it works. And, yes, it could be an if-else statement but that would remove the isdigit() test.

Here’s abbreviated output:

 32 20 ' ' is non-alphanumeric
 33 21 '!' is non-alphanumeric
 34 22 '"' is non-alphanumeric
 35 23 '#' is non-alphanumeric
...
 46 2E '.' is non-alphanumeric
 47 2F '/' is non-alphanumeric
 48 30 '0' is alphanumeric (digit)
 49 31 '1' is alphanumeric (digit)
 50 32 '2' is alphanumeric (digit)
 51 33 '3' is alphanumeric (digit)
 52 34 '4' is alphanumeric (digit)
 53 35 '5' is alphanumeric (digit)
 54 36 '6' is alphanumeric (digit)
 55 37 '7' is alphanumeric (digit)
 56 38 '8' is alphanumeric (digit)
 57 39 '9' is alphanumeric (digit)
 58 3A ':' is non-alphanumeric
 59 3B ';' is non-alphanumeric
...
 63 3F '?' is non-alphanumeric
 64 40 '@' is non-alphanumeric
 65 41 'A' is alphanumeric (alphabetic)
 66 42 'B' is alphanumeric (alphabetic)
 67 43 'C' is alphanumeric (alphabetic)
...
 89 59 'Y' is alphanumeric (alphabetic)
 90 5A 'Z' is alphanumeric (alphabetic)
 91 5B '[' is non-alphanumeric
 92 5C '\' is non-alphanumeric
 93 5D ']' is non-alphanumeric
 94 5E '^' is non-alphanumeric
 95 5F '_' is non-alphanumeric
 96 60 '`' is non-alphanumeric
 97 61 'a' is alphanumeric (alphabetic)
 98 62 'b' is alphanumeric (alphabetic)
...
121 79 'y' is alphanumeric (alphabetic)
122 7A 'z' is alphanumeric (alphabetic)
123 7B '{' is non-alphanumeric
124 7C '|' is non-alphanumeric
125 7D '}' is non-alphanumeric
126 7E '~' is non-alphanumeric
127 7F '' is non-alphanumeric

Coding your own version of these ctype functions requires some basic comparisons, as shown in the following code. Note that I use the results of both isalpha() and isdigit() to craft my isalnum() function:

2026_05_23-Lesson-b.c

#include <stdio.h>

#define TRUE 1
#define FALSE 0
#define DEL 127

int isalpha(int c)
{
    if( (c>='A' && c<='Z') || (c>='a' && c<='z') )
            return TRUE;
    return FALSE;
}

int isdigit(int c)
{
    if( c>='0' && c<='9' )
        return TRUE;
    return FALSE;
}

int isalnum(int c)
{
    if( isalpha(c) || isdigit(c) )
        return TRUE;
    return FALSE;
}

int main()
{
    int ch;

    for( ch=' '; ch<=DEL; ch++ )
    {
        printf("%3d %2X '%c' is",ch,ch,ch);
        if( isalnum(ch) )
        {
            printf(" alphanumeric");
            if( isalpha(ch) )
                printf(" (alphabetic)");
            if( isdigit(ch) )
                printf(" (digit)");
        }
        else
        {
            printf(" non-alphanumeric");
        }
        putchar('\n');
    }

    return 0;
}

The isalpha() function perform a complex comparison to ensure that the character is in the range 'A' through 'Z' or 'a' through 'z'. When either condition is true, the character is alphabetic, TRUE is returned.

The isdigit() function returns TRUE when the character is in the range of ‘0’ through ‘9’.

Finally, the isalnum() function determines whether the character is alphabetic (isalpha()) or numeric (isdigit()) and returns TRUE is so.

The program’s output is the same.

In next week’s Lesson I cover the is isgraph() and isprint() ctype functions.

Leave a Reply