C
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Arrays and Strings
-
Section 5: Pointers
-
Section 6: Memory Management
-
Section 7: Structures and Unions
-
Section 8: The Preprocessor and Build Process
-
Section 9: Standard Library: stdio.h
-
Section 10: Standard Library: stdlib.h
-
Section 11: Standard Library: string.h
-
Section 12: Standard Library: ctype.h and wctype.h
-
Section 13: Standard Library: math.h, complex.h, fenv.h, tgmath.h
-
Section 14: Standard Library: Type and Limit Headers
-
Section 15: Standard Library: Error Handling and Debugging
-
Section 16: Standard Library: Localization and Encoding
-
Section 17: Standard Library: time.h
-
Section 18: Standard Library: Concurrency (C11)
-
Section 19: POSIX and System Programming (unistd.h)
-
Section 20: More Data Structures
-
Section 21: Algorithms in C
-
Section 22: Bitwise Operations
-
Section 23: Command-Line Programs
-
Section 24: Debugging and Best Practices
-
Section 25: Compiler and Language Internals
-
Section 26: Embedded and Cross-Platform Considerations
-
Section 27: Networking Basics
-
Section 28: Practical Projects
-
Section 29: Interview Practice
-
Section 30: C23 Modern Features
-
Section 31: More Practice and Review
129: Wide Character Strings with wchar.h
Why can't I just use a regular char array for everything?
If you're only dealing with standard English text, char is great because it's exactly one byte. But the world is bigger than ASCII. Once you need to support characters from other languages—like the Japanese "世界" or even a simple Euro symbol (€)—a single byte isn't enough to hold the unique identifier for that character.
That's where wchar_t (wide character) comes in. Depending on your system, it's usually 2 or 4 bytes. It gives the program enough "room" to store a much larger range of characters. I've seen plenty of beginners try to cram UTF-8 into a char array and then get confused when strlen() returns a number higher than the actual number of visible characters. wchar_t helps avoid that confusion by treating one "wide" character as one unit of memory.
How do I actually write and print wide strings?
You can't just drop a wide string into a standard printf call. First, you have to tell the compiler that the string literal is wide by prefixing it with an L. Then, you use the wide-character equivalents of the standard I/O functions, like wprintf.
One thing that trips everyone up: you must set the locale. If you don't, your program will likely fail to print any non-ASCII wide characters because it doesn't know which character set the terminal is using. Here is how I usually set it up:
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main() {
// This tells the program to use the system's default locale settings
setlocale(LC_ALL, "");
wchar_t* greeting = L"Hello, 世界";
wprintf(L"The message is: %ls\n", greeting);
return 0;
}
Notice the %ls specifier? That's specifically for wide strings. If you use %s, you're going to get garbage output or a crash.
Are there different functions for manipulating these strings?
Yes. You can't use strlen, strcpy, or strcat on wchar_t arrays. The wchar.h header provides a mirror set of functions that look almost identical but start with wcs (which stands for "wide character string").
It's a bit of a naming chore, but the pattern is consistent. I've listed the most common swaps below:
strlen()becomeswcslen()strcpy()becomeswcscpy()strcmp()becomeswcscmp()strcat()becomeswcscat()
For example, if you wanted to find the length of a wide string, you'd do this:
wchar_t* city = L"München";
size_t len = wcslen(city); // Returns the number of wide characters, not bytes
I highly recommend sticking to the wcs versions strictly. Trying to cast a wchar_t* to a char* to use the old functions is a recipe for a memory nightmare.
📋 Practical Task
Build a Multi-lingual Name Formatter
Your task is to create a program that takes a wide-character string containing a person's name and a city (in any language), and prints a formatted greeting. Your program must:
- Call
setlocale(LC_ALL, "")to ensure wide characters render correctly. - Declare two
wchar_tarrays: one for a name (e.g.,L"André"orL"佐藤") and one for a city. - Use
wcslen()to calculate the total number of wide characters in both strings combined. - Use
wprintfto print a message like:"Greetings [Name] from [City]! (Total characters: [Length])".
Make sure you use the L prefix for all your wide string literals!
There are no comments for now.