Wide char and library functions in C++


In this section we will see what is the wide character in C++. We will also see some functions that are used to handle wide characters.

Wide characters are similar to character datatype. The main difference is that char takes 1-byte space, but wide character takes 2-bytes (sometimes 4-byte depending on compiler) of space in memory. For 2-byte space wide character can hold 64K (65536) different characters. So the wide char can hold UNICODE characters. The UNICODE values are international standard which allows for encoding for characters virtually for any character of any language.

Example

 Live Demo

#include<iostream>
using namespace std;
int main() {
   wchar_t wide_character = L'a';
   cout << "The wide character is: " << wide_character << endl;
   cout << "Wide character size: " <<sizeof(wide_character);
}

Output

The wide character is: 97
Wide character size: 2

We can see that to make wide character we have to add ‘L’ before the character literal. But the character value is not displayed in the output using cout. So to use wide char we have to use wcout, and for taking input we have to use wcin.

We can make some wide character array, and print them as string.

Example

 Live Demo

#include<iostream>
using namespace std;
int main() {
   char str1[] = "This is character array";
   cout << str1 << endl;
   wchar_t str2 [] = L"This is wide character array";
   wcout << str2;
}

Output

This is character array
This is wide character array

Now let us see some functions that are used for wide characters.

FunctionDescription
wcslen()The syntax is size_t wcslen (const wchar_t* wcs); This function is used to get the length of the wide character string.
wcscat()The syntax is: wchar_t *wcscat(wchar_t *strDest, const wchar_t *strSrc); This function is used to concatenate the source string with the destination string.
wcscpy()The syntax is wchar_t *wcscpy(wchar_t *strDest, const wchar_t *strSrc); It helps to copy the source string to destination string.
wcsncpy()The syntax is wchar_t* wcsncpy(wchar_t* dest, const wchar_t* src, size_t n); This function is used to copy first n characters of source to destination. When the end os source is less than n, then at the destination there will be some null characters.
wcscmp()The syntax is int wcscmp(const wchar_t* wcs1, const wchar_t* wcs2); This function is used to compare two wide character strings wcs1 and wcs2. It is like strcmp() function for normal string comparison.
wcsstr()The syntax is const wchar_t* wcsstr (const wchar_t* wcs1, const wchar_t* wcs2); This function is used to find the first occurrence of wcs2 in wcs1. If it is not present, then it returns null
wcstok()The syntax is wchar_t* wcstok( wchar_t* str, const wchar_t* delim, wchar_t ** ptr); This function is like strtok(). It helps to tokenize string which are created by using wide characters. It takes delimiter to tokenize the string.

Updated on: 30-Jul-2019

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements