Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why was wchar_t invented?

Tags:

Why is wchar_t needed? How is it superior to short (or __int16 or whatever)?

(If it matters: I live in Windows world. I don't know what Linux does to support Unicode.)

like image 976
CannibalSmith Avatar asked Oct 23 '09 13:10

CannibalSmith


People also ask

What is the use of wchar_t?

The wchar_t type is an implementation-defined wide character type. In the Microsoft compiler, it represents a 16-bit wide character used to store Unicode encoded as UTF-16LE, the native character type on Windows operating systems.

What does Wchar stand for?

This document sets out the walking, cycling and horse-riding assessment and review (WCHAR) process for highway schemes on motorways and all-purpose trunk roads.

What is the difference between Wchar and char?

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.


2 Answers

See Wikipedia.

Basically, it's a portable type for "text" in the current locale (with umlauts). It predates Unicode and doesn't solve many problems, so today, it mostly exists for backward compatibility. Don't use it unless you have to.

like image 86
Aaron Digulla Avatar answered Oct 15 '22 18:10

Aaron Digulla


Why is wchar_t needed? How is it superior to short (or __int16 or whatever)?

In the C++ world, wchar_t is its own type (I think it's a typedef in C), so you can overload functions based on this. For example, this makes it possible to output wide characters and not to output their numerical value. In VC6, where wchar_t was just a typedef for unsigned short, this code

wchar_t wch = L'A' std::wcout << wch; 

would output 65 because

std::ostream<wchar_t>::operator<<(unsigned short) 

was invoked. In newer VC versions wchar_t is a distinct type, so

std::ostream<wchar_t>::operator<<(wchar_t) 

is called, and that outputs A.

like image 31
sbi Avatar answered Oct 15 '22 20:10

sbi