for example, does:
wchar_t x;
translate to:
unsigned short x;
for example, does:
wchar_t x;
translate to:
unsigned short x;
Not necessarily; it could be a 4-byte quantity, or indeed any other size chosen by the implementation.
It depends on the compiler.
In short: in C may be in C++ no.
Widely. C defines wchar_t as typedef but in Unix it is generally 4 bytes (so generally not short) and in Windows 2 so it may be short.
Under C++ it is unique built-in type like char
or int
, so you can legally overload void foo(short x)
and void foo(wchar_t x)
wchar_t isn't required by the standard to be unsigned. It can also be signed. And there must be another type of the same size; but the standard doesn't explicitly say that that other type must be short.
"the same size, signedness, and alignment requirements as one of the other integral types, called its underlying type" (C++98 §3.9.1).
In C compilers this is a typedef, usually defined in stddef.h
No, it doesn't. It translates to 'a wide character.' Making any assumptions about what that happens to be on a particular platform is incorrect, and defeats the entire purpose of having a wchar_t in the first place.
The point of using an abstraction is to separate the semantic meaning of the type from its underlying representation.
For C, wchar_t
is a typedef
. Whether it is a synonym for unsigned int
, whether it is an unsigned type at all, or whether it is 4 bytes, is implementation-defined.
In C++, wchar_t
is a distinct built-in type. Here, too, its size and signedness is implementation-defined.
For anyone else who may come across this answer because function calls in your Visual Studio project won't link, despite both parties taking wchar_t
(or a comparable type, such as LPCTSTR
with UNICODE #defined), and when you DUMPBIN the library's exports the function takes const unsigned short *
, be aware that VS allows you to switch off wchar_t
as a built-in type. If someone changes this in a library, and you don't hit the same compiler switch in your project, it will not link.