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


Groups > comp.lang.c > #91460

Re: unsigned int

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: unsigned int
Date 2016-08-01 11:01 +0200
Organization A noiseless patient Spider
Message-ID <nnn35u$2dd$1@dont-email.me> (permalink)
References <unsigned-20160731163521@ram.dialup.fu-berlin.de> <e06rglFmcd5U1@mid.individual.net>

Show all headers | View raw


On 31/07/16 19:40, Osmium wrote:

> 
> A novice would naturally think that char was signed by default, the same
> as an int.  It is not, the default is chosen by the compiler writer. 
> Among other things, this has ramifications in bit shifting within
> characters,.

A novice may equally well think that a char was /unsigned/ by default,
since the obvious interpretation of "char" is "character", such as
letters, digits, etc. - and the concept of a "negative character" is
totally meaningless.

Clearly, assuming that "char" is unsigned by default is just as bad as
assuming it is signed by default.  In any situation where you use char
types to store numbers or whenever the signedness of char matters, you
should be explicit about it.

Since this is in the context of teaching C, I think programmers should
be strongly encouraged not to consider "char" for arithmetic at all -
including bit shifting (what does it mean to bit-shift a "z"
character?).  They should be encouraged to use "uint8_t" and "int8_t" as
appropriate.

I know there are targets that don't support uint8_t or int8_t.  But
these are few and far between these days, and mostly for rather
specialised programming.  People should be taught that such systems
exist, but for most programming tasks you can freely use uint8_t and
friends if that is what makes your code clearer and more sensible.

And with bit shifts in particular, people should be taught to stick to
unsigned types when doing bit manipulation.  It is almost always the
type you want, and you run far less risk of accidentally getting
undefined behaviour.

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


Thread

Re: unsigned int "Osmium" <r124c4u102@comcast.net> - 2016-07-31 12:40 -0500
  Re: unsigned int David Brown <david.brown@hesbynett.no> - 2016-08-01 11:01 +0200
    Re: unsigned int James Kuyper <jameskuyper@verizon.net> - 2016-08-01 10:15 -0400
    Re: unsigned int Les Cargill <lcargill99@comcast.com> - 2016-08-01 09:59 -0500
      Re: unsigned int David Brown <david.brown@hesbynett.no> - 2016-08-01 17:21 +0200
        Re: unsigned int Keith Thompson <kst-u@mib.org> - 2016-08-01 09:55 -0700
          Re: unsigned int David Brown <david.brown@hesbynett.no> - 2016-08-01 22:42 +0200
            Re: unsigned int supercat@casperkitty.com - 2016-08-01 14:18 -0700
        Re: unsigned int Les Cargill <lcargill99@comcast.com> - 2016-08-01 13:11 -0500
          Re: unsigned int Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-08-01 19:46 +0100
            Re: unsigned int Les Cargill <lcargill99@comcast.com> - 2016-08-01 18:20 -0500
              Re: unsigned int David Brown <david.brown@hesbynett.no> - 2016-08-02 10:18 +0200
                Re: unsigned int Les Cargill <lcargill99@comcast.com> - 2016-08-02 09:35 -0500
                Re: unsigned int Keith Thompson <kst-u@mib.org> - 2016-08-02 08:00 -0700
                Re: unsigned int Les Cargill <lcargill99@comcast.com> - 2016-08-02 11:12 -0500
                Re: unsigned int David Brown <david.brown@hesbynett.no> - 2016-08-02 17:11 +0200
                Re: unsigned int Les Cargill <lcargill99@comcast.com> - 2016-08-02 11:55 -0500
                Re: unsigned int supercat@casperkitty.com - 2016-08-02 10:16 -0700
                Re: unsigned int David Brown <david.brown@hesbynett.no> - 2016-08-03 14:02 +0200
                Re: unsigned int Keith Thompson <kst-u@mib.org> - 2016-08-03 08:58 -0700
                Re: unsigned int James Kuyper <jameskuyper@verizon.net> - 2016-08-02 13:57 -0400
                Re: unsigned int Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-08-02 19:30 +0100
                Re: unsigned int Keith Thompson <kst-u@mib.org> - 2016-08-02 11:38 -0700
                Re: unsigned int David Brown <david.brown@hesbynett.no> - 2016-08-03 14:12 +0200
                Re: unsigned int David Brown <david.brown@hesbynett.no> - 2016-08-03 13:45 +0200
          Re: unsigned int Keith Thompson <kst-u@mib.org> - 2016-08-01 12:02 -0700
            Re: unsigned int David Brown <david.brown@hesbynett.no> - 2016-08-01 23:01 +0200

csiph-web