Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Seungbeom Kim <musiphil@bawi.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: wchar_t: signed or unsigned? |
| Date | 2014-04-21 11:07 -0700 |
| Message-ID | <lj3mpr$dml$1@usenet.stanford.edu> (permalink) |
| References | <4cial9tecgiks7ov2q3c1oo53492kvf3ti@4ax.com> |
On 2014-04-21 09:55, DSF wrote: > Hello, group! > > Is wchar_t signed or unsigned? It can be either. You can test it with WCHAR_MIN (defined in <stdint.h>): it shall be <= -127 if wchar_t is signed, and 0 if wchar_t is unsigned. > Is it different in C++ than in C? Same in C++, despite the difference that it is a distinct type defined as a keyword in C++, rather than a typedef name as in C. > Or is it one of those things left up to the compiler writer? Yes. I don't see it mentioned even as implementation-defined; I think it is unspecified. > I need to know because I'm dealing with an array of chars, but they > are treated by the outside world as ints. EOFs and error codes are > expressed in negative values. So I am using unsigned chars for the > array to avoid the possibility of one of the array members being > 127 > and being sign extended to a negative int. If the values you need to store include character values *and EOF*, you can store them as int, with char values cast to unsigned char. (See what fgetc() does, for example.) If you need to store only character values, it doesn't matter whether you store them as (plain) char or unsigned char; you just have to make sure to cast plain char values to unsigned char before giving them in a 'int' context where it could be taken as EOF, such as isupper() and other <ctype.h> friends. I don't see why you consider wchar_t for this scenario, unless the actual characters you're dealing with are wide characters. > I also have an array of wchar_t for dealing with Windows 16-bit > Unicode. I don't know if Unicode utilizes the fifteenth bit, but I > don't want to take any chances. In my compiler, wchar_t is defined as > "unsigned short" so I don't expect any problems. But I'd like to know > the official word on it. I did Google the question, but found page > after page espousing which form of Unicode is best. Interesting > reading, but not an answer. Full Unicode codespace uses 21 bits (from 0 to 10FFFF); 16 bits can only represent codepoints in Basic Multilingual Plane (BMP). (You can store UTF-16 values in 16-bit wchar_t objects, but they will be a fixed-length encoding only for the characters in BMP; surrogate pairs will take up more space.) Using wchar_t defined as unsigned short cannot represent EOF, just as unsigned char cannot. If you're explicitly dealing with UTF-16, char16_t (declared in <uchar.h> to be the same as uint_least16_t) seems like a more proper choice. -- Seungbeom Kim
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
wchar_t: signed or unsigned? DSF <notavalid@address.here> - 2014-04-21 12:55 -0400
Re: wchar_t: signed or unsigned? Xavier Roche <xroche@free.fr.NOSPAM.invalid> - 2014-04-21 20:02 +0200
Re: wchar_t: signed or unsigned? Seungbeom Kim <musiphil@bawi.org> - 2014-04-21 11:07 -0700
Re: wchar_t: signed or unsigned? Keith Thompson <kst-u@mib.org> - 2014-04-21 11:24 -0700
Re: wchar_t: signed or unsigned? Seungbeom Kim <musiphil@bawi.org> - 2014-04-21 11:57 -0700
Re: wchar_t: signed or unsigned? James Kuyper <jameskuyper@verizon.net> - 2014-04-21 15:13 -0400
Re: wchar_t: signed or unsigned? James Kuyper <jameskuyper@verizon.net> - 2014-04-21 14:31 -0400
Re: wchar_t: signed or unsigned? Stephen Sprunk <stephen@sprunk.org> - 2014-04-21 15:28 -0500
Re: wchar_t: signed or unsigned? James Kuyper <jameskuyper@verizon.net> - 2014-04-21 16:48 -0400
Re: wchar_t: signed or unsigned? Keith Thompson <kst-u@mib.org> - 2014-04-21 14:08 -0700
Re: wchar_t: signed or unsigned? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-04-22 03:21 -0700
Re: wchar_t: signed or unsigned? Stephen Sprunk <stephen@sprunk.org> - 2014-04-22 10:48 -0500
Re: wchar_t: signed or unsigned? Keith Thompson <kst-u@mib.org> - 2014-04-22 09:10 -0700
Re: wchar_t: signed or unsigned? gordonb.8ql82@burditt.org (Gordon Burditt) - 2014-04-23 03:31 -0500
Re: wchar_t: signed or unsigned? Stephen Sprunk <stephen@sprunk.org> - 2014-04-25 10:00 -0500
Re: wchar_t: signed or unsigned? Keith Thompson <kst-u@mib.org> - 2014-04-25 09:00 -0700
Re: wchar_t: signed or unsigned? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-04-25 10:27 -0700
Re: wchar_t: signed or unsigned? Stephen Sprunk <stephen@sprunk.org> - 2014-04-25 16:43 -0500
csiph-web