← GCC/C Index

🔡 <ctype.h> — Character Classification & Conversion

Fast single-character tests and conversions. All functions take an unsigned char value cast to int. Returns non-zero (true) or 0 (false).

Include: #include <ctype.h> | Compile: gcc -Wall -o prog prog.c

📋 Functions & Macros

NamePrototype / ValueDescription
isalphaint isalpha(int c)Non-zero if c is a letter (a-z, A-Z)
isdigitint isdigit(int c)Non-zero if c is a decimal digit (0-9)
isalnumint isalnum(int c)Non-zero if c is a letter or digit
isspaceint isspace(int c)Non-zero if c is whitespace
isupperint isupper(int c)Non-zero if c is uppercase letter
islowerint islower(int c)Non-zero if c is lowercase letter
ispunctint ispunct(int c)Non-zero if c is punctuation
isprintint isprint(int c)Non-zero if c is printable (includes space)
isgraphint isgraph(int c)Non-zero if c is printable and not space
iscntrlint iscntrl(int c)Non-zero if c is a control character
isxdigitint isxdigit(int c)Non-zero if c is a hex digit (0-9, a-f, A-F)
toupperint toupper(int c)Convert lowercase letter to uppercase
tolowerint tolower(int c)Convert uppercase letter to lowercase

💡 Example Program

Copy, save as demo.c, and compile with the command shown at the bottom.

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

void str_upper(char *s) {
    for (; *s; s++) *s = (char)toupper((unsigned char)*s);
}

void analyze(const char *s) {
    int letters=0, digits=0, spaces=0, other=0;
    for (; *s; s++) {
        unsigned char c = (unsigned char)*s;
        if      (isalpha(c)) letters++;
        else if (isdigit(c)) digits++;
        else if (isspace(c)) spaces++;
        else                 other++;
    }
    printf("Letters=%d Digits=%d Spaces=%d Other=%d\n",
           letters, digits, spaces, other);
}

int main(void) {
    char text[] = "Hello, World! 123";
    analyze(text);
    str_upper(text);
    printf("Upper: %s\n", text);
    return 0;
}
/* Compile: gcc -Wall -o ctype_demo ctype_demo.c */

← <stdlib.h>  |  🏠 Index  |  <time.h> →