again: sizeof(wchar_t)

Artem B. Bityuckiy
Sat Apr 24 09:21:00 GMT 2004

Hello guys. I have already asked this 
( May be this time 
I'll be lucky and get more answers...

I'm writing some code that depends on sizeof(wchar_t). If 
sizeof(wchar_t) > 2 I'm doing one things, if sizeof(wchar_t) = 2 - 
another things. I need to determine sizeof(wchar_t) in precompiler. How 
can I do this?

My logic is: sizeof(wchar_t) is given by gcc. I can use 
--with-target-short-wchar gcc's configure script option and gcc will 
generate 2-byte wchar_t variables. Therefore, there must be some 
predefined GCC's macro using which I can determine the size of wchar_t. 

The code for which I need to know sizeof(wchar_t) is written for Newlib 
(small standard C library). As I understand situation with wchar_t is 
similar to situation with other basic C types - they are defined with 
help of gcc's stddef.h or limits.h.

llewelly advised me to use WCHAR_MAX macro.

I tried to use something like this:


But to my surprise I've found out that I have WCHAR_MAX==0x7FFFFFFFu 
when sizeof(wchar_t)==2! I've seen into wchar.h of Newlib - there is 
something like

#ifndef _WCHAR_MAX

This is similar to defines of other limits macros. I've seen to GCC's 
headers and not found any _WCHAR_MAX reference there.

I have seen to other libc's (FreeBSD's and Glibc) - they also rely on 
_WCHAR_MAX, but _WCHAR_MAX isn't from GCC, but is from target-depended 
files (also placed in that libc) as I've understood.

Therefore I can conclude that sizeof(wchar_t) is considered as 
target-dependent. An I can't vary gcc (using or not 
--with-target-short-wchar option when compiling it)...

But I want to have one target that is built differently depending on 
sizeof(wchar_t) that is defined by GCC. How can I do this?

One obvious way is to use construction like

if (sizeof(wchar_t) > 2)

but I want to rely on precompiler, not on compiler.

What should I do?


P.S. Please, CC to me if answer...

Best Regards,
Artem B. Bityuckiy,
St.-Petersburg, Russia.

More information about the Gcc-help mailing list