Wide char and library functions in C++

13 May, 2022
Wide char is similar to char data type, except that wide char take up twice the space and can take on much larger values as a result. char can take 256 values which corresponds to entries in the ASCII table. On the other hand, wide char can take on 65536 values which corresponds to UNICODE values which is a recent international standard which allows for the encoding of characters for virtually all languages and commonly used symbols.
Just like the type for character constants is char, the type for wide character is wchar_t.This data type occupies 2 or 4 bytes depending on the compiler being used.Mostly the wchar_t datatype is used when international languages like Japanese are used.
Just like the type for character constants is char, the type for wide character is wchar_t.
This data type occupies 2 or 4 bytes depending on the compiler being used.
Mostly the wchar_t datatype is used when international languages like Japanese are used.
Below is a simple C++ implementation to show how wchar_t is used : 
CPP
// An example C++ program to demonstrate use of wchar_t#include <iostream>using namespace std; int main(){    wchar_t w  = L'A';    cout << "Wide character value:: " << w << endl ;    cout << "Size of the wide char is:: " << sizeof(w);    return 0;}
Output:
Wide character value:: 65
Size of the wide char is:: 4
L is the prefix for wide character literals and wide-character string literals which tells the compiler that  the char or string is of type wide-char.
w is prefixed in operations like scanning (wcin) or printing (wcout) while operating wide-char type.
Wide char type array or string: Just like char type array string, there can be a wide-char type array string as well. Below is the C++ implementation to show wide-char type array string: 
CPP
// An example C++ program to demonstrate use// of wchar_t in array#include <iostream>using namespace std; int main(){    // char type array string    char caname[] = "geeksforgeeks" ;    cout << caname << endl ;     // wide-char type array string    wchar_t waname[] = L"geeksforgeeks" ;    wcout << waname << endl;     return 0;}
Output:
geeksforgeeks
geeksforgeeks
The output is the same, but the only difference is that the wide character array uses twice as much memory to encode each character. Functions for wide character array strings : Most of the functions for wide character array strings are defined in the header file cwchar. wcslen() : syntax: size_t wcslen (const wchar_t* wcs); It returns the length of the wide string. This is the wide character equivalent of strlen. Below is a simple C++ implementation to show how to get the length of a wide character array string. 
CPP
// An example C++ program to demonstrate use// of wcslen()#include <iostream>#include<cwchar>using namespace std; int main(){    // wide-char type array string    wchar_t waname[] = L"geeksforgeeks" ;     wcout << L"The length of '" << waname          << L"' is " << wcslen(waname) << endl;     return 0;}
Output:
The length of 'geeksforgeeks' is 13
wcscpy() : syntax: wchar_t *wcscpy(wchar_t *strDestination, const wchar_t *strSource); wcscpy() stands for Wide-Character String Copy. It copies a wide-character string pointed by strSource into the wide character array pointed by strDestination. This is the wide character equivalent of strcpy. Below is a simple C++ implementation to show the use of wcscpy: 
CPP
// An example C++ program to demonstrate use// of wcscpy()#include <iostream>#include<cwchar>using namespace std; int main(){    wchar_t waname[] = L"geeksforgeeks" ;    wchar_t wacopy[14];    wcscpy(wacopy, waname);    wcout << L"Original = " << waname          << L"\nCopy =  " << wacopy << endl;     return 0;}
Output:
Original = geeksforgeeks
Copy =  geeksforgeeks
wcscat() : syntax: wchar_t *wcscat(wchar_t *strDestination, const wchar_t *strSource); wcscat() stands for Wide-Character String Concatenation. Appends a copy of the strSource wide string to the strDestination wide string. This is the wide character equivalent of strcat. Below is a simple C++ implementation to show the use of wcscat: 
CPP
// An example C++ program to demonstrate use// of wcscat()#include <iostream>#include<cwchar>using namespace std; int main(){    wchar_t string1[] = L"geeksforgeeks" ;    wchar_t string2[] = L" is for Geeks" ;     wcscat(string1, string2);     wcout << L"Concatenated wide string is = "          << string1 << endl;     return 0;}
Output:
Concatenated wide string is = geeksforgeeks is for Geeks
wcscmp() : syntax: int wcscmp(const wchar_t* wcs1, const wchar_t* wcs2); wcscmp() stands for Wide-Character String Comparison. It returns 0 if wcs1 and wcs2 are equal, returns a value greater than zero if the first wide character that does not match has a greater value in wcs1 than in wcs2. And returns a value less than zero if the first wide character that does not match has a lesser value in wcs1 than in wcs2. This is the wide character equivalent of strcmp. Below is a simple C++ implementation to show the use of wcscmp: 
CPP
// An example C++ program to demonstrate use// of wcscmp()#include <iostream>#include<cwchar>using namespace std; int main(){    wchar_t string1[] = L"geeksforgeeks" ;    wchar_t string2[] = L"GEEKS" ;    wcout << L"Comparison1 = "          << wcscmp(string1, string2) << endl;    wcout << L"Comparison2 = "          << wcscmp(string1, string1) << endl;    wcout << L"Comparison3 = "          << wcscmp(string2, string1) << endl;    return 0;}
Output:
Comparison1 = 1
Comparison2 = 0
Comparison3 = -1
wcstok() : syntax: wchar_t* wcstok( wchar_t* str, const wchar_t* delim, wchar_t ** ptr); wcstok() stands for Wide-Character String Tokenize. Finds the next token in a null-terminated wide string pointed to by str. The separator characters are identified by null-terminated wide string pointed to by delim. str – pointer to the null-terminated wide string to tokenize. delim – pointer to the null-terminated wide string identifying delimiters. ptr – pointer to an object of type wchar_t*, which is used by wcstok to store its internal state. This is the wide character equivalent of strtok(). Below is a simple C++ implementation to show the use of wcstok: 
CPP
// An example C++ program to demonstrate use// of wcstok()#include <iostream>#include<cwchar>using namespace std; int main(){    wchar_t string[] = L"geeksforgeeks,is,for,GEEKS" ;     wchar_t* internal_state;     wchar_t delim[] = L"," ;    wchar_t* token  = wcstok(string, delim, &internal_state);     while (token)    {        wcout << token << endl;        token = wcstok(NULL, delim, &internal_state);    }     return 0;}
Output:
geeksforgeeks
is
for
GEEKS
wcsncpy() : syntax: wchar_t* wcsncpy(wchar_t* destination, const wchar_t* source, size_t n); Copies the first n wide-characters of source to destination. If the end of the source wide string is found before n characters have been copied, destination is padded with additional null wide characters until a total of n characters.This is the wide character equivalent of strncpy(). Below is a simple C++ implementation to show the use of wcsncpy: 
CPP
// An example C++ program to demonstrate use// of wcsncpy()#include <iostream>#include<cwchar>using namespace std; int main(){    wchar_t string1[] = L"Geeks For Geeks";    wchar_t string2[20];    wchar_t string3[20];     wcsncpy(string2, string1, 20);     // partial copy    wcsncpy(string3, string2, 5);     string3[5] = L'&#092;&#048;';   // null character manually added     wcout << string1 << endl << string2          << endl << string3 ;     return 0;}
Output:
Geeks For Geeks
Geeks For Geeks
Geeks
wcsstr() : syntax: const wchar_t* wcsstr (const wchar_t* wcs1, const wchar_t* wcs2); Returns a pointer to the first occurrence of wcs2 in wcs1. It returns a null pointer if wcs2 is not part of wcs1. Here, wcs1 is the wide character string to be scanned and wcs2 contains the sequence to match. This is the wide character equivalent of strstr(). Below is a simple C++ implementation to show the use of wcsstr: 
CPP
// An example C++ program to demonstrate use// of wcsstr()#include <iostream>#include<cwchar>using namespace std; int main(){    wchar_t string1[] = L"Geeks Are Geeks";    wchar_t* string2 = wcsstr(string1, L"Are");    wcsncpy(string2, L"For", 3);    wcout << string1 << endl;    return 0;}
Output:
Geeks For Geeks
Reference: http://www.cplusplus.com/reference/cwchar/ This article is contributed by MAZHAR IMAM KHAN. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
anindo_7
sagartomar9927
cpp-data-types
CPP-Library
cpp-string
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.