Thursday, January 5, 2012

Wide character - wchar_t

According to Wikipedia:

A wide character is a computer character datatype that generally has a size greater than the traditional 8-bit character. The increased datatype size allows for the use of larger coded character sets.

in C/C++:
The standard library of the C programming language includes a lot of facilities for dealing with wide characters and strings composed of them. The wide characters are defined using datatype wchar_t, which in the original C90 standard was defined as 16-bit value due to historical compatibility reasons. C and C++ compilers that comply with the 10646-1:2000 Unicode standard generally assume 32-bit values.

However, the ISO/IEC 10646:2003 Unicode standard 4.0 says that:

"ANSI/ISO C leaves the semantics of the wide character set to the specific implementation but requires that the characters from the portable C execution set correspond to their wide character equivalents by zero extension."

and that

"The width of wchar_t is compiler-specific and can be as small as 8 bits. Consequently, programs that need to be portable across any C or C++ compiler should not use wchar_t for storing Unicode text. The wchar_t type is intended for storing compiler-defined wide characters, which may be Unicode characters in some compilers."


Example:
Wide character - wchar_t
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 
 cout << "_WCHAR_T_SIZE: " << _WCHAR_T_SIZE << endl;
 cout << "WCHAR_MIN= " << WCHAR_MIN << endl;
 cout << "WCHAR_MAX= " << WCHAR_MAX << endl << endl;

 wchar_t wideChar_heart = L'?';
 cout << wideChar_heart << endl;
 wprintf(L"wideChar_heart: %lc \n", wideChar_heart);

 system("PAUSE");
 return 0;
}



No comments:

Post a Comment