<ctype.h> classifies narrow char values. <wctype.h> does the same for wide characters: iswalpha asks “is this letter?”, towlower changes case. Pair it with <wchar.h> when you process international text with wchar_t strings.
01
iswalpha
Letter?
02
iswdigit
Digit?
03
iswspace
Whitespace?
04
towlower
Lowercase.
05
towupper
Uppercase.
06
wint_t
Arg type.
Fundamentals
Definition and Usage
Wide-character classification functions take a wint_t value and return non-zero (true) or zero (false). Conversion functions return the new wide character, or the original if no mapping exists.
Think of wctype.h as the wide mirror of ctype.h: isalpha → iswalpha, tolower → towlower. Use them when iterating over wchar_t buffers from wchar.h, not on plain char bytes.
💡
Beginner Tip
For everyday ASCII programs, ctype.h on char is enough. Learn wctype.h when you already use wide strings (L"...", wprintf) or Windows wide APIs.
Foundation
📝 Syntax
Include the header (usually with wchar.h for wide I/O):
Compile with gcc file.c -std=c11 -o out. Examples use portable ASCII wide letters; extended characters need locale and Unicode terminal support.
📚 Getting Started
Classify and convert a single wide character (fixed from reference).
Example 1 — iswalpha, towlower, towupper
The reference used L'Ä' which often prints ? without locale—this version uses ASCII L'A' for reliable output.
C
#include <stdio.h>
#include <wctype.h>
#include <wchar.h>
#include <locale.h>
int main(void) {
wint_t wc = L'A';
wint_t lower;
wint_t upper;
setlocale(LC_ALL, "");
if (iswalpha(wc)) {
wprintf(L"%lc is an alphabetic character\n", (wchar_t)wc);
} else {
wprintf(L"%lc is not alphabetic\n", (wchar_t)wc);
}
lower = towlower(wc);
upper = towupper(lower);
wprintf(L"Lowercase of %lc is %lc\n", (wchar_t)wc, (wchar_t)lower);
wprintf(L"Uppercase of %lc is %lc\n", (wchar_t)lower, (wchar_t)upper);
return 0;
}
📤 Output:
A is an alphabetic character
Lowercase of A is a
Uppercase of a is A
How It Works
iswalpha returns non-zero for letters. towlower(L'A') yields L'a'. Arguments are wint_t so EOF from wide input can be distinguished from characters when needed.
Example 2 — iswdigit (Validate a Wide Digit)
Check whether a wide character is 0–9.
C
#include <stdio.h>
#include <wctype.h>
#include <wchar.h>
int main(void) {
wchar_t tests[] = { L'0', L'9', L'A', L'-' };
size_t i;
for (i = 0; i < 4; i++) {
wprintf(L"'%lc' -> %s\n", tests[i],
iswdigit(tests[i]) ? L"digit" : L"not a digit");
}
return 0;
}
📤 Output:
'0' -> digit
'9' -> digit
'A' -> not a digit
'-' -> not a digit
How It Works
iswdigit is the wide version of isdigit. Use iswalnum when you want letters or digits together.
📈 Practical Patterns
Whitespace, scanning, and case-insensitive compare.
Example 3 — iswspace (Skip Leading Spaces)
Find the first non-whitespace wide character in a string.
POSIX provides wcscasecmp for this; manual towlower loops teach how classification fits with wchar.h string walking. Locale affects non-ASCII letters.
Applications
🚀 Common Use Cases
Input validation — reject non-digit wide characters in forms.
Tokenizers — split wide text on iswspace boundaries.
Password rules — require upper, lower, digit with isw* tests.
Case-normalization — towlower before compare or hash.
Lexers — classify wide source code characters.
Windows UI strings — sanitize wide user input.
🧠 How wctype.h Works
1
Read wchar_t
From a wide string or fgetwc input.
Input
2
Classify or convert
isw* tests category; tow* maps case.
wctype
3
Branch logic
Accept, reject, transform, or count characters.
App code
=
💬
Safe wide parsing
Programs handle international input with explicit rules.
Important
📝 Notes
Do not pass char to iswalpha—cast to wchar_t only when the value is a valid wide character.
Return value is non-zero for true; compare with if (iswalpha(wc)), not == 1.
towlower may return unchanged character if no lowercase exists.
Extended Unicode letters need correct locale and wide encoding support.
For UTF-8 char text, many apps use ctype.h on bytes only for ASCII, or a Unicode library.
Advanced: wctype, iswctype, towctrans for custom locale categories.
Performance
⚡ Optimization
Per-character isw* calls in tight loops are usually fine. For ASCII-only data, converting to narrow char and using ctype.h can be faster but only when you know the text is ASCII. Avoid calling setlocale repeatedly inside loops.
Wrap Up
Conclusion
<wctype.h> completes the wide-character story started in <wchar.h>. Use iswalpha, iswdigit, and iswspace to inspect characters; use towlower and towupper to normalize case.
You have now covered every standard library header in this tutorial series. Continue with C interview programs or revisit the library index.
Mix narrow and wide classification in one loop blindly
Compare iswalpha result to exactly 1
Forget that rules change when locale changes
List iswalpha under wchar.h in docs
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about wctype.h
Wide ctype—mirror of ctype.h.
5
Core concepts
💬01
iswalpha
Wide letter.
Test
📚02
iswdigit
0–9.
Test
📈03
iswspace
Skip ws.
Parse
📄04
towlower
Downcase.
Map
🌐05
ctype.h
Narrow twin.
Pair
❓ Frequently Asked Questions
wctype.h is the C standard header for classifying and converting wide characters. It provides iswalpha, iswdigit, iswspace, towlower, towupper, and related functions—the wide-character equivalents of ctype.h functions like isalpha and tolower.
ctype.h works on narrow characters (char/int) from byte strings. wctype.h works on wide characters (wint_t/wchar_t) from wide strings. Use ctype.h with printf and char*; use wctype.h with wprintf and wchar_t*.
wchar.h handles wide strings and I/O: wcslen, wcscpy, wprintf, mbstowcs. wctype.h tests and transforms single wide characters: iswalpha, towlower. Include wchar.h for string work and wctype.h when you need per-character wide classification.
wint_t is an integer type that can hold any value of wchar_t plus the special value WEOF. Classification functions like iswalpha take wint_t so they can accept wide characters and end-of-file indicators from wide input functions.
Yes. Which characters count as letters, digits, or whitespace depends on the current locale. Call setlocale from locale.h when you need locale-aware behavior—for example handling accented letters in European languages.
The reference used L'Ä' without setlocale and possibly a console that could not display the character. For portable tutorials, start with ASCII wide letters like L'A'. For extended characters, set locale (e.g. en_US.UTF-8) and ensure your terminal supports Unicode.
Did you know?
The C standard names every wide classification function with a leading isw and every wide case conversion with tow (think “to wide”). That naming mirror makes it easy to guess the wide version once you know ctype.h.