Skip to Content
Course content

129: Wide Character Strings with wchar.h

Click on the "Edit" button in the top corner of the screen to edit your slide content.

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() becomes wcslen()
  • strcpy() becomes wcscpy()
  • strcmp() becomes wcscmp()
  • strcat() becomes wcscat()

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_t arrays: one for a name (e.g., L"André" or L"佐藤") and one for a city.
  • Use wcslen() to calculate the total number of wide characters in both strings combined.
  • Use wprintf to print a message like: "Greetings [Name] from [City]! (Total characters: [Length])".

Make sure you use the L prefix for all your wide string literals!

Rating
0 0

There are no comments for now.

to be the first to leave a comment.