Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #43301

Re: wchar_t: signed or unsigned?

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: wchar_t: signed or unsigned?
Date 2014-04-22 09:10 -0700
Organization None to speak of
Message-ID <ln7g6hz6o0.fsf@nuthaus.mib.org> (permalink)
References <4cial9tecgiks7ov2q3c1oo53492kvf3ti@4ax.com> <978c9431-6725-4fd0-83ca-8f426b94de8d@googlegroups.com>

Show all headers | View raw


Malcolm McLean <malcolm.mclean5@btinternet.com> writes:
> On Monday, April 21, 2014 5:55:52 PM UTC+1, DSF wrote:
>>   Is wchar_t signed or unsigned?  Is it different in C++ than in C? Or
>> is it one of those things left up to the compiler writer?
>> 
>>   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.
>> 
>>   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.
>> 
> It depends how portable you need to be.
> 16 bits aren't enough to encode all the glyphs in every language in
> the world, so if you must support all of Unicode, you can't use
> Windows wchar_t at all. However the higher Unicode numbers are used
> for pretty marginal alphabets, 15 bits is enough for most
> purposes. Unicode defines a series of so- called "non-character code
> points" of which 0xFFFF is one, You can use these internally, but you
> can't export unicode sequences containing these reserved code points.
> I don't know if any Windows fonts actually use Unicode characters
> 0x7FFFF to 0xFFFFF. Obviously you won't be able to support them if you
> use bit 15 as an internal flag.

A 16-bit (unsigned?) wchar_t can encode arbitrary Unicode text with
UTF-16, which I think is what Windows uses by default.  It's not clear
that all Windows software uses or implements UTF-16 correctly, though.

There certainly are Unicode code points in the range 0x8000..0xFFFF, so
stealing the high-order bit will prevent you from being able to
represent those characters.  I think it will also prevent you from using
the encoding that permits UTF-16 to represent characters above 0xFFFF.

If you don't care about characters outside the range 0..0x7FFF, you can
steal that bit -- but I'd suggest finding some other way to add that
information.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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