The ctype Library
isdigit, isalpha, toupper.
The ctype Library is a free C Academy lesson on CoddyKit. This is lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 4 lessons in total.
What Is ctype.h
The <ctype.h> header provides functions to classify and convert characters.
It saves you from memorizing ASCII ranges and writing manual comparisons.
#include <stdio.h>
#include <ctype.h>
int main(void) {
printf("Is 'A' alpha? %d\n", isalpha('A'));
return 0;
}isdigit
isdigit(c) returns a nonzero value if c is a digit character '0'-'9', and 0 otherwise.
#include <stdio.h>
#include <ctype.h>
int main(void) {
printf("isdigit('7') = %d\n", isdigit('7'));
printf("isdigit('x') = %d\n", isdigit('x'));
return 0;
}isalpha
isalpha(c) returns nonzero if c is a letter (either uppercase or lowercase).
Digits and symbols return 0.
#include <stdio.h>
#include <ctype.h>
int main(void) {
printf("isalpha('G') = %d\n", isalpha('G'));
printf("isalpha('3') = %d\n", isalpha('3'));
return 0;
}isalnum and isspace
isalnum(c) is true for letters or digits. isspace(c) is true for whitespace such as space, tab, and newline.
#include <stdio.h>
#include <ctype.h>
int main(void) {
printf("isalnum('9') = %d\n", isalnum('9'));
printf("isspace(' ') = %d\n", isspace(' '));
printf("isspace('a') = %d\n", isspace('a'));
return 0;
}isupper and islower
isupper(c) tests for uppercase letters, islower(c) for lowercase.
These are handy before deciding whether to convert case.
#include <stdio.h>
#include <ctype.h>
int main(void) {
printf("isupper('A') = %d\n", isupper('A'));
printf("islower('A') = %d\n", islower('A'));
return 0;
}ispunct
ispunct(c) returns nonzero for printable punctuation characters like !, ?, and , that are not letters, digits, or spaces.
#include <stdio.h>
#include <ctype.h>
int main(void) {
printf("ispunct('!') = %d\n", ispunct('!'));
printf("ispunct('a') = %d\n", ispunct('a'));
return 0;
}toupper
toupper(c) returns the uppercase version of a letter. If c is not a lowercase letter, it returns c unchanged.
#include <stdio.h>
#include <ctype.h>
int main(void) {
printf("%c\n", toupper('h'));
printf("%c\n", toupper('5'));
return 0;
}tolower
tolower(c) returns the lowercase version of an uppercase letter, or the character unchanged if it is not uppercase.
#include <stdio.h>
#include <ctype.h>
int main(void) {
printf("%c\n", tolower('H'));
printf("%c\n", tolower('z'));
return 0;
}Classifying a String
Loop over a string and use ctype functions to count digits and letters.
#include <stdio.h>
#include <ctype.h>
int main(void) {
char *s = "abc123";
int letters = 0, digits = 0;
for (int i = 0; s[i]; i++) {
if (isalpha(s[i])) letters++;
if (isdigit(s[i])) digits++;
}
printf("letters=%d digits=%d\n", letters, digits);
return 0;
}Uppercasing a Whole String
Combine toupper with a loop to convert an entire string to uppercase, character by character.
#include <stdio.h>
#include <ctype.h>
int main(void) {
char s[] = "Hello";
for (int i = 0; s[i]; i++) {
s[i] = toupper(s[i]);
}
printf("%s\n", s);
return 0;
}Pass int, Not Negative char
ctype functions take an int that must be representable as unsigned char or be EOF.
Passing a negative char directly is undefined behavior. Cast to unsigned char when in doubt.
#include <stdio.h>
#include <ctype.h>
int main(void) {
char c = 'Q';
int result = isalpha((unsigned char)c);
printf("safe isalpha = %d\n", result);
return 0;
}Quick Check
Test your knowledge of the ctype library.
Recap
The <ctype.h> library lets you:
- Classify with
isdigit,isalpha,isalnum,isspace,ispunct,isupper,islower. - Convert with
toupperandtolower. - Always pass values cast to
unsigned charto avoid undefined behavior.
Learn C with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 39
- Lessons
- 144
Frequently Asked Questions
Is the “The ctype Library” lesson free?
Yes — the full text of “The ctype Library” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO. The C Academy course includes 4 lessons in total.
What will I learn in “The ctype Library”?
isdigit, isalpha, toupper. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C Academy?
No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
How long does the “The ctype Library” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C Academy lesson?
Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Characters as Integers
- The ctype Library
- Reading Characters
- Building Char Utilities