AlgoInsight

Q&A

What is the isalpha() function in C?

By AlgoInsight Team2026-06-18machine-learningc

Overview

When processing strings character by character, you often need to check what type a character is - letter, digit, whitespace, etc. <ctype.h> provides a suite of classification functions for this. isalpha is the one for alphabetic letters. Understanding its input requirement (unsigned char or EOF) and return type (int, not bool) is important for correct usage.

graph TD
    A["isalpha(c)"] --> B{"c is A-Z or a-z?"}
    B -->|yes| C["returns non-zero (true)"]
    B -->|no| D["returns 0 (false)"]
    A --> E["<ctype.h> must be included"]
    A --> F["Argument must be unsigned char or EOF"]
    style C fill:#1e1b4b,color:#fff

Signature and Include

#include <ctype.h>
int isalpha(int c);

Despite the name, the parameter is typed as int - the function accepts an unsigned char value or the macro EOF.

Basic Usage

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

int main() {
    printf("%d\n", isalpha('A'));     // non-zero (true)
    printf("%d\n", isalpha('z'));     // non-zero (true)
    printf("%d\n", isalpha('5'));     // 0 (false - digit)
    printf("%d\n", isalpha(' '));     // 0 (false - space)
    printf("%d\n", isalpha('!'));     // 0 (false - punctuation)
    return 0;
}

The exact non-zero value returned is implementation-defined; use it as a boolean (if (isalpha(c))), don't rely on a specific value.

Counting Letters in a String

int count_alpha(const char *s) {
    int count = 0;
    while (*s) {
        if (isalpha((unsigned char)*s))
            count++;
        s++;
    }
    return count;
}

The Crucial Cast: (unsigned char)

char is signed on many platforms, so characters above 127 have negative values. isalpha requires an unsigned char or EOF - passing a negative char is undefined behaviour. The safe pattern is:

if (isalpha((unsigned char)c)) { ... }

This is the most commonly missed detail about all <ctype.h> functions.

The ctype.h Family

isalpha is one of a family:

Function True when
isalpha(c) Letter (A–Z, a–z)
isdigit(c) Digit (0–9)
isalnum(c) Letter or digit
isspace(c) Whitespace (space, tab, newline…)
isupper(c) / islower(c) Uppercase / lowercase
ispunct(c) Punctuation
isprint(c) Printable character
toupper(c) / tolower(c) Convert case

Locale Sensitivity

In the default "C" locale, isalpha recognizes only A–Z and a–z. In other locales (set via setlocale), locale-specific letters (accented characters, etc.) may also return non-zero. For portable, locale-independent code, use the "C" locale explicitly or be explicit about the locale your code targets.

Why This Matters in Interviews

C string-processing interviews use isalpha to test <ctype.h> familiarity and the (unsigned char) cast rule. Key points: returns non-zero (not necessarily 1) for true; the argument must be cast to unsigned char to avoid UB on signed char platforms; know the family (isdigit, isalnum, isspace, toupper/tolower); and be aware of locale behavior. These functions replace raw ASCII comparisons with portable, readable code.

Common Pitfalls

References

More answers