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


Groups > comp.lang.c > #105311

Re: "usual arithmetic conversions": please explain something...

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: "usual arithmetic conversions": please explain something...
Date 2017-03-01 14:18 -0800
Organization None to speak of
Message-ID <lnshmwbpzk.fsf@kst-u.example.com> (permalink)
References <448topoppn.fsf@be-well.ilk.org> <ln8topeuc6.fsf@kst-u.example.com> <o97fs5$ai$1@dont-email.me>

Show all headers | View raw


Nick Bowler <nbowler@draconx.ca> writes:
> On Tue, 28 Feb 2017 16:03:53 -0800, Keith Thompson wrote:
>> The assignment converts that value from unsigned long to (signed) int.
>> The behavior is undefined (assuming that value exceeds INT_MAX, which it
>> almost certainly does, but *typically* it will discard the high-order
>> bits yielding -40000.
>
> Nit: the behaviour of converting out-of-range values to signed integer
> types is not, strictly speaking, undefined.  See C11ยง6.3.1.3p3:
>
>   "[if] the new type is signed and the value cannot be represented in
>    it, either the result is implementation-defined or an implementation-
>    defined signal is raised.

Quite right.  I apologize for the misinformation.  Apparently there was
too much blood in my caffeine system.

If an arithmetic operation yielding a signed value overflows (such as
INT_MAX + 1), the behavior is undefined.

An overflowing conversion from a signed or unsigned integer type to a
signed integer type, as you say, yields an implementation-defined value
or raises an implementation-defined signal.  (Digression: The latter
permission was added in C99.  IMHO it made it more difficult to write
portable code, and I'm not aware of any implementation that takes
advantage of it.)

An overflowing conversion from a floating-point type to a signed or
unsigned integer type (other than _Bool) has undefined behavior.

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


Thread

"usual arithmetic conversions": please explain something... Lowell Gilbert <lgusenet@be-well.ilk.org> - 2017-02-28 18:33 -0500
  Re: "usual arithmetic conversions": please explain something... BartC <bc@freeuk.com> - 2017-02-28 23:45 +0000
    Re: "usual arithmetic conversions": please explain something... Keith Thompson <kst-u@mib.org> - 2017-02-28 16:59 -0800
      Re: "usual arithmetic conversions": please explain something... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-01 02:20 +0000
        Re: "usual arithmetic conversions": please explain something... Keith Thompson <kst-u@mib.org> - 2017-02-28 18:51 -0800
      Re: "usual arithmetic conversions": please explain something... BartC <bc@freeuk.com> - 2017-03-01 12:04 +0000
        Re: "usual arithmetic conversions": please explain something... Keith Thompson <kst-u@mib.org> - 2017-03-01 08:25 -0800
          Re: "usual arithmetic conversions": please explain something... BartC <bc@freeuk.com> - 2017-03-01 16:47 +0000
            Re: "usual arithmetic conversions": please explain something... Keith Thompson <kst-u@mib.org> - 2017-03-01 09:43 -0800
              Re: "usual arithmetic conversions": please explain something... BartC <bc@freeuk.com> - 2017-03-01 18:00 +0000
                Re: "usual arithmetic conversions": please explain something... Keith Thompson <kst-u@mib.org> - 2017-03-01 10:15 -0800
                Re: "usual arithmetic conversions": please explain something... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-01 19:08 +0000
          Re: "usual arithmetic conversions": please explain something... supercat@casperkitty.com - 2017-03-01 09:01 -0800
  Re: "usual arithmetic conversions": please explain something... jameskuyper@verizon.net - 2017-02-28 15:49 -0800
  Re: "usual arithmetic conversions": please explain something... Keith Thompson <kst-u@mib.org> - 2017-02-28 16:03 -0800
    Re: "usual arithmetic conversions": please explain something... Nick Bowler <nbowler@draconx.ca> - 2017-03-01 21:53 +0000
      Re: "usual arithmetic conversions": please explain something... Keith Thompson <kst-u@mib.org> - 2017-03-01 14:18 -0800
  Re: "usual arithmetic conversions": please explain something... Lowell Gilbert <lgusenet@be-well.ilk.org> - 2017-03-01 08:29 -0500
    Re: "usual arithmetic conversions": please explain something... Lowell Gilbert <lgusenet@be-well.ilk.org> - 2017-03-01 09:11 -0500
    Re: "usual arithmetic conversions": please explain something... Keith Thompson <kst-u@mib.org> - 2017-03-01 08:59 -0800
  Re: "usual arithmetic conversions": please explain something... scott@slp53.sl.home (Scott Lurndal) - 2017-03-01 14:00 +0000
    Re: "usual arithmetic conversions": please explain something... "James R. Kuyper" <jameskuyper@verizon.net> - 2017-03-01 10:58 -0500

csiph-web