
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Wide char and library functions in C++
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 1: Size of a single wide character
This program demonstrates how to declare a single wide character using wchar_t to print its value and memory size.
#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); }
The above program generates the following 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.
Example 2: Wide Character Strings using Arrays
In this example, we create a wide character string (array), and print it using wcout alongside a regular character array.
#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; }
Following is the output to the above program:
This is character array This is wide character array
Library Functions for Wide Char
Here, we have some special functions that is used to handle the wide character. It is defined with the header file <wchar.h>:
Function | Description |
---|---|
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. |