Updated April 13, 2023
Definition of C++ wchar_t
In C++, wide characters are like character datatype except the fact that char data type takes space of one byte whereas wide-character takes space of two bytes. In some cases, the wide-character takes up four bytes of memory depending on the compiler. This can hold different 64K (65536) characters in those two bytes of space. That is, it can hold characters of UNICODE which an international standard that permits encoding characters any character in any language virtually. Let us see more details on wide characters in the below sections. In this article, we will discuss the functions and examples of C++ wchar_t.
Syntax:
Wide characters are written in the format as mentioned below.
wchar_t
This will be used in the programs for the implementation of wide characters.
Functions of Wide Characters
Below are some of the functions that are used in wide characters.
Function: wcslen()
Syntax:
wcslen ( const wchar_t* str ) ;
Description: Function that helps in getting the wide-character string length.
Function: wcsncpy()
Syntax:
wchar_t* wcsncpy( wchar_t* dst, const wchar_t* sr, size_t sn) ;
Description: Function that helps in copying the sn characters from the source to destination. If the source end is smaller than the size sn, then the destination will have some null characters.
Function: wcscat()
Syntax:
wchar_t* wcscat ( wchar_t* dst, const wchar_t* sr) ;
Description: Function that helps in concatenating the source string to destination string.
Function: wcscpy()
Syntax:
wchar_t* wcscpy ( wchar_t* dst, const wchar_t* sr) ;
Description: Function that helps in copying the source string to destination string.
Function: wcscmp()
Syntax:
wcscmp ( const wchar_t* str1, const wchar_t* str2) ;
Description: Function that helps in comparing the first string and second string. This function is similar to normal string comparison.
Function: wcsstr()
Syntax:
const wchar_t* wcsstr ( const wchar_t* str1, const wchar_t* str2) ;
Description: Function that helps in finding the first appearance of the second string in the first string. Null will be returned if it is not present.
Function: wcstok()
Syntax:
wchar_t* wcstok ( const wchar_t* str1, const wchar_t* delim , wchar_t ** ptr) ;
Description: Function that helps in tokenizing the string that generated with the help of wide characters. A delimiter delim is also used for string tokenization.
Examples of C++ wchar_t
Let us see some sample examples on wchar_t in this section.
Example #1
CPP program to implement wide character and get the size of it
Code:
#include <iostream>
using namespace std;
int main()
{
//declare a wide character
wchar_t c = L'S' ;
//print the character value
cout << "The wide character value 'S' is: " << c << endl ;
//print the size of wide character
cout << "Wide character size is " << sizeof(c) ;
return 0;
}
Output:
In this program, a wide character is declared first. On executing the code, the value and its size gets printed. Here, it can be seen that L is used as a prefix for wide-character literals as well as wide-character string literals that notifies the compiler that string or character is of type wide-char.
Example #2
CPP program to implement wide character and get the size of it using wcslen()
Code:
#include <iostream>
using namespace std;
int main()
{
//declare a wide character array string
wchar_t c[] = L"Hope never dies" ;
//print the character value
cout <<"The wide character length of Hope never dies " <<" is : " << wcslen(c) << endl ;
return 0 ;
}
Output:
In this program, a wide character array string is declared first. On executing the code, the size of the string gets printed.
Example #3
CPP program to copy a wide-character string to another location
Code:
#include <iostream>
#include<cwchar>
using namespace std;
int main()
{
//declare a wide character array string
wchar_t c[] = L"Hope never dies" ;
wchar_t d[15] ;
//copy the string
wcscpy(d, c);
wcout << L"Original string is : " << c << L"\n Copied string is : " << d << endl;
return 0;
}
Output:
In this program, an additional header file <cwchar> is also used along with other header files. Two string arrays are also used where one array is to store the string and the other one is to copy the string to. On executing the code, it can be seen that string has copied to another location.
Example #4
CPP program to concatenate a wide-character string with another string
Code:
#include <iostream>
#include<cwchar>
using namespace std;
int main()
{
//declare a wide character array string
wchar_t c[] = L"Hope never dies" ;
wchar_t d[] = L" and trust yourself" ;
//concatenate the string
wcscat(c, d);
wcout << L"Concatenated string is : " << c << endl;
return 0;
}
Output:
In this program, two string arrays are declared first. On executing the code, it can be seen that both strings are concatenated using the function wcscat().
Example #5
CPP program to compare a wide-character string with another string
Code:
#include <iostream>
#include<cwchar>
using namespace std;
int main()
{
//declare a wide character array string
wchar_t c[] = L"Hope never dies" ;
wchar_t d[] = L" and trust yourself" ;
//compare the strings
wcout << L"Comparison of first string with second = " << wcscmp(c, d) << endl;
wcout << L"Comparison of first string with first string = " << wcscmp(c, c) << endl;
wcout << L"Comparison of second string with first string = " << wcscmp(d, c) << endl;
return 0;
}
Output:
In this program also, two string arrays are declared first. Unlike the above program, this program is to compare two strings. On executing the code, it can be seen that 3 values are shown. When the first string is compared with the second string, 1 is returned as the value of a first string is higher than the second. In the second case, 0 is returned because the string is compared with itself. At last, in the third case, -1 is returned as the value of the first string is less than the second.
Conclusion
In this article, different aspects such as syntax, functions, and example of wchar_t C++ is explained in detail.
Recommended Articles
This is a guide to C++ wchar_t. Here we discuss the definition and Functions of wide Characters and examples of C++ wchar_t respectively. You may also have a look at the following articles to learn more –