[geeks] stupid C question

Greg A. Woods woods at weird.com
Thu Feb 28 21:29:00 CST 2002


[ On Thursday, February 28, 2002 at 16:16:32 (-0500), Joshua D Boyd wrote: ]
> Subject: Re: [geeks] stupid C question
>
> So, what platforms do have unsigned chars native?

The AT&T 3B compilers defaulted to unsigned char, for one, though I
don't believe the CPU (at least not the WE32000 micros) really cared.

Also the CPUs in the IBM/Motorola POWER architecture family (including
the PowerPC chips) really highly desire unsigned chars:

       A signed char takes another two instructions more than an
       unsigned char each time the variable is loaded into a register.

Supposedly the S/390 also defaults to wanting unsigned chars too.

If I'm not mistaken the Motorola 68HCxx family of processors also desire
unsigned chars by default because they have no efficient sign extend
instruction.

With GCC thouh you always have a choice, as you'll have no doubt already
discovered if you've read the manual and discovered these options:

`-funsigned-char'
     Let the type `char' be unsigned, like `unsigned char'.

     Each kind of machine has a default for what `char' should be.  It
     is either like `unsigned char' by default or like `signed char' by
     default.

     Ideally, a portable program should always use `signed char' or
     `unsigned char' when it depends on the signedness of an object.
     But many programs have been written to use plain `char' and expect
     it to be signed, or expect it to be unsigned, depending on the
     machines they were written for.  This option, and its inverse, let
     you make such a program work with the opposite default.

     The type `char' is always a distinct type from each of `signed
     char' or `unsigned char', even though its behavior is always just
     like one of those two.

`-fsigned-char'
     Let the type `char' be signed, like `signed char'.

     Note that this is equivalent to `-fno-unsigned-char', which is the
     negative form of `-funsigned-char'.  Likewise, the option
     `-fno-signed-char' is equivalent to `-funsigned-char'.

There's also this (from http://home.att.net/~jackklein/c/inttypes.html)

     Q: Why is it up to the compiler to decide whether the default char
     type is signed or unsigned?
                                                                                
     A: This is just the way that the C language evolved. By the time a
     standard for the C language was being developed there were
     compilers that implemented ordinary char as signed, and others as
     unsigned.  Some programs for these compilers depended on that
     choice.  If the standard required one choice or the other it would
     break a lot of existing, working code.
                                                                                
     On all compilers, "plain" char must have exactly the same
     representation and interpretation as either signed or unsigned
     char.
                                                                                
     Many compilers offer a command line or IDE option to set default
     char to either signed or unsigned for compatibility with code
     developed on either type of system.
                                                                                
> > Either that or make your code explicitly portable the hard way....  :-)
> 
> > (yes this business of signed and unsigned integers of all sizes is messy
> > stuff in C)

and of course there are also these GCC options too:

`-fsigned-bitfields'
`-funsigned-bitfields'
`-fno-signed-bitfields'
`-fno-unsigned-bitfields'

 
-- 
								Greg A. Woods

+1 416 218-0098;  <gwoods at acm.org>;  <g.a.woods at ieee.org>;  <woods at robohack.ca>
Planix, Inc. <woods at planix.com>; VE3TCP; Secrets of the Weird <woods at weird.com>



More information about the geeks mailing list