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


Groups > comp.lang.c > #91409

Re: unsigned int

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: unsigned int
Date 2016-07-31 11:58 -0700
Organization None to speak of
Message-ID <lnmvkx63ek.fsf@kst-u.example.com> (permalink)
References <unsigned-20160731163521@ram.dialup.fu-berlin.de>

Show all headers | View raw


ram@zedat.fu-berlin.de (Stefan Ram) writes:
>   I am now trying to write a new lesson for my German-language
>   C course. This time about »unsigned« (say, »unsigned int«,
>   for a start).
>
>   So, what are the crucial facts the readers need to know?
>
>   Some things that come to my mind right now ("brainstorming"):
>
> - unsigned has defined semantics on overflow and underflow
>
> - comparisons like »<« can give unexpected results (I need
>   to figure out the possible cases where this can happen,
>   maybe you have such a case handy?)

The comparisons themselves don't give unexpected results; `<` yields a
true result if and only if its left operand is numerically less than its
right operand.  But other operations can yield unexpected results, which
can in turn cause `<` to yield *seemingly* unexpected results.

An obvious example:

    unsigned int x = 1;
    if (-x < 0) ...

The value of the LHS of the `<` is UINT_MAX, not -1.

The promotion rules can also cause unexpected results for unsigned
types narrower than unsigned int.

> - there are suffixes for numerals to mark them as usigned,
>   like »u«

"u" (or "U") is the *only* suffix that marks a constant as unsigned.
(I suggest using the standard term "constants" rather than
"numerals".  "Literals" is also ok; the standard doesn't refer to
integer constants as literals, but the meaning is clear enough.)

> - But sometimes, the type of a numeral that is not marked
>   with a suffix but is too large to be an int is »unsigned«? 
>   (But this has changed between several standards? Or was 
>   this in C++?)

In C90, an unsuffixed decimal integer constant is of type int, long
int, or unsigned long int, the first in which it can be represented.

In C99, this changed to int, long int, and long long int; an
unsuffixed decimal integer constant is never of an unsigned type.
This is unchanged in C11.

(In C++2003, the corresponding list is int, long int; C++2012
changed this to int, long int, long long int.)

> - A special conversion specification has to be used for
>   printf (scanf will only be treated later in the course)

I'm not sure I'd call it any more "special" than the specification
for signed int.  You need to use "%d" for signed int, "%u" for
unsigned int.

> - there are the usual macros in <limits.h> for »unsigned int«.
>
> - I don't actually know the semantics for conversions
>   between signed and unsigned, but I think a can look them
>   up in the standard.
>
> - what did I miss? (Especially with regard to common errors
>   commited when using »unsigned«.)

For all integer types, the behavior of arithmetic operations
follows the usual mathematical rules when all operands and results
are within the range of the type.  For operands or results close
to the limits of the type, you get results that don't match the
corresponding results for mathematical integers -- undefined
behavior for signed types, defined wraparound for unsigned types.
(You can loosely think of the limits as singularities.)

For most signed integer types, for many problems, the limits have
very large magnitudes, and you likely don't have to worry about
running into them.  For unsigned integer types, one of the limits
is at 0, which is easy to run into accidentally.

Gareth Owen's code:

    for(x = strlen(s);x >= 0; --x) ...

is a good example of this.

-- 
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 | Next — Next in thread | Find similar | Unroll thread


Thread

Re: unsigned int Keith Thompson <kst-u@mib.org> - 2016-07-31 11:58 -0700
  Re: unsigned int supercat@casperkitty.com - 2016-07-31 12:28 -0700
    Re: unsigned int Ian Collins <ian-news@hotmail.com> - 2016-08-01 07:33 +1200
      Re: unsigned int Les Cargill <lcargill99@comcast.com> - 2016-07-31 17:06 -0500

csiph-web