Question

I cannot make clang on 64 bit linux to compile wchar_t to 16 bit

printf("%i ", sizeof (wchar_t));

prints 4

clang++ -fshort-char test.cpp

although I use -fshort-char

my question is how to make wchar_t to 16 bit. is #define the only option?

#define wchar_t unsigned short
Was it helpful?

Solution

The way to make wchar_t be 16 bits with clang (or gcc) is to pass the compiler option -fshort-wchar (not -fshort-char).

This is a rather drastic measure, however, as it may break code that calls the Standard Library or 3rd party libraries passing wchar_t data. Note that by default wchar_t is 32 bits for gcc and clang regardless of whether the compiler targets 32-bit or 64-bit systems. In this respect they conform with the C Standard 7.17/2 which requires wchar_t to be:

an integer type whose range of values can represent distinct codes for all members of the largest extended character set specified among the supported locales

As you are invoking clang++ I presume you are compiling C++ source. If you simply require a character type that is 16 bits wide, and can compile with the option -std=c++11, the core language offers you char16_t, which is suitable for storing any UTF-16 character. (And should you wish to be able to store any UTF-32 character, char32_t will do).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top