
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
#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
#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.
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. |
- Related Articles
- Convert CHAR to HEX in SAP system and functions
- Explain important functions in math.h library functions using C language
- What are stack and unstack functions in the Python Pandas library.
- What are the C library functions?
- Explain string library functions with suitable examples in C
- Difference between char s[] and char *s in C
- Difference between const char* p, char * const p, and const char * const p in C
- Golang Program to Convert Char Type Variables to Int Using Inbuilt Functions
- Finding alphabet from ASCII value without using library functions in JavaScript
- Finding square root of a number without using library functions - JavaScript
- Flattening an array with truthy/ falsy values without using library functions - JavaScript
- Golang Program to Display Prime Numbers Between Two Intervals using library functions.
- Difference between string and char[] types in C++
- Char Struct in C#
- Mathematical Functions in Python - Special Functions and Constants
