C++

What is Wchar_t in C++?

If you are a C++ developer looking to work with wide characters and Unicode characters, wchar_t is a data type that you should work on. In C++, wchar_t is used to represent wide characters and provides support for Unicode characters, making it a powerful tool for working with multilingual applications.

In this guide, we will learn about the wchar_t data type and how to use it in C++.

wchar_t in C++

In C++, wchar_t is a data type that shares some similarities with the char data type, but with one key distinction: the amount of storage used. While char occupies a single byte, wchar_t takes up 2 or 4 bytes of storage depending on the compiler, and can store up to 64K Unicode characters.

Wide characters are written using the wchar_t data type and are typically enclosed in L” (L stands for “wide-character literal”). For example, the wide-character string “Linux” can be written as L”Linux”.

Example 1

The following example illustrates the wide-character type in C++. In this example you can clearly see that to use wchar_t in C++, you have to add L before the character:

#include<iostream>

using namespace std;

int main() {

   wchar_t ch1 = L'a';

   cout << "The wide character is: " << ch1 << endl;

   cout << "The Size of the Wide character is: " << sizeof(ch1);

}

In the above program, the results you see may be different in your case. The compiler prints the ASCII value of ‘a‘, which is 97 in decimal because the cout treats ch1 as a char variable, not a wide character variable. As for the size of wchar_t, the compiler reported a size of 4 bytes, which is larger than the size of wchar_t in some other platforms. This could be due to the implementation of your compiler and the specific architecture of your system.

Example 2

If you want to print the actual wide-character value stored in ch1, you can use the wcout object instead of cout.

#include <iostream>

using namespace std;

int main() {

   wchar_t ch1 = L'a';

   wcout << "The wide character is: " << ch1 << endl;

   wcout << "The size of the wide character is: " << sizeof(ch1) << endl;

   return 0;

}

How wchar_t is Different From char in C++

There are some differences between these two data types of C++:

char wchar_t
It holds space of one byte It holds a space of 2 or 4 bytes depending on the compiler
Use cout to display the output Use wcout to print the output
Stores ASCII Values Store Unicode Values
Can store up to 256 values Can store up to 64K values

Bottom Line

wchar_t is the wide-character data type in C++ like other data types including int, char, and string. It has more size than the normal character data type and is used to store Unicode characters. The letter L is added before the character to specify that it is a wide character and use wcout instead of cout to print the wide-character data type. By using wchar_t and understanding its syntax, C++ programmers can effectively work with a wider range of characters and create more versatile programs.

About the author

Zainab Rehman

I'm an author by profession. My interest in the internet world motivates me to write for Linux Hint and I'm here to share my knowledge with others.