Wide char and library functions in C++
Last Updated :
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.
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'\0'; // 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/
Similar Reads
Can We Call an Undeclared Function in C++?
Calling an undeclared function is a poor style in C (See this) and illegal in C++and so is passing arguments to a function using a declaration that doesn't list argument types.If we call an undeclared function in C and compile it, it works without any error. But, if we call an undeclared function in
2 min read
ldexp() function in C/C++
The ldexp() is a built-in function in C/C++ gives the product of any variable x and 2 raised to the power of exp by taking two arguments x and exp i.e., x * 2^exp Syntax: float ldexp (float x, int exp) double ldexp (double x, int exp) long double ldexp (long double x, int exp) double ldexp (T x, int
2 min read
Return From Void Functions in C++
Void functions are known as Non-Value Returning functions. They are "void" due to the fact that they are not supposed to return values. True, but not completely. We cannot return values but there is something we can surely return from void functions. Void functions do not have a return type, but the
2 min read
Function Overloading and float in C++
Although polymorphism is a widely useful phenomena in C++ yet it can be quite complicated at times. For instance consider the following code snippet: CPP #include<iostream> using namespace std; void test(float s,float t) { cout << "Function with float called "; } void test(int
2 min read
Bind Function and Placeholders in C++
Sometimes we need to manipulate the operation of a function according to the need, i.e changing some arguments to default, etc. Predefining a function to have default arguments restricts the versatility of a function and forces us to use the default arguments and that too with similar values each ti
4 min read
Function Pointers and Callbacks in C++
A callback is a function that is passed as an argument to another function. In C++, we cannot directly use the function name to pass them as a callback. We use function pointers to pass callbacks to other functions, store them in data structures, and invoke them later. In this article, we will discu
2 min read
Friend Class and Function in C++
In C++, friend functions and friend classes are concepts that allow certain functions or classes to access the private and protected members of another class. These are useful concepts in such situations where you need to give a function or another class access to internal data, while still keeping
5 min read
Inline Functions in C++
In C++, inline functions provide a way to optimize the performance of the program by reducing the overhead related to a function call. When a function is specified as inline the whole code of the inline function is inserted or substituted at the point of its call during the compilation instead of us
5 min read
Can Virtual Functions be Inlined in C++?
Virtual functions are member functions that are declared in the base class using the keyword virtual and can be overridden by the derived class. They are used to achieve Runtime polymorphism or say late binding or dynamic binding. Inline functions are used to replace the function calling location wi
2 min read
wctype() function in C/C++
The wctype() is a built-in function in C/C++ which returns a value of type wctype_t that is used for classifying a wide character. It is defined within the cwctype header file of C++. The following are the possible type wctype_t: Value of str Equivalent function space iswspace upper iswupper xdigit
2 min read