The Unix Heritage Society mailing list
 help / color / mirror / Atom feed
From: "Alejandro Colomar (man-pages)" <alx.manpages@gmail.com>
To: Dan Cross <crossd@gmail.com>
Cc: TUHS <tuhs@tuhs.org>
Subject: [TUHS] Re: [TUHS]: C dialects (was: I can't drive 55: "GOTO considered harmful" 55th anniversary)
Date: Mon, 13 Mar 2023 23:25:37 +0100	[thread overview]
Message-ID: <CAJbEFzh4O-62SFh8QdhLP5bt8wOvLuvUtB87kP7e8kaLbvY=Eg@mail.gmail.com> (raw)
In-Reply-To: <CAEoi9W6D0gnTYA7AWtQP5CFovMsPhz0O+PeRrcyebjGOsDLESw@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 3386 bytes --]

On Mon, Mar 13, 2023, 21:27 Dan Cross <crossd@gmail.com> wrote:

> On Mon, Mar 13, 2023 at 3:16 PM Steve Nickolas <usotsuki@buric.co> wrote:
> > On Mon, 13 Mar 2023, Clem Cole wrote:
> > > Frankly, I'd probably rather see ISO drop a bunch of the stuff they
> are now
> > > requiring and fall back at least to K&R2 -- keep it simple. The truth
> is
> > > that we still use the language today is that K&R2 C was then (and
> still is)
> > > good enough and got (gets) the job done extremely well.    Overall,
> I'm not
> > > sure all the new "features" have added all that much.
> >
> > C99 did introduce one thing I use: <stdint.h>
> >
> > Beyond that, I still code strict C89.  I simply treat the language itself
> > as ossified.  I also still make assumptions about the compiler that might
> > not still be true, so for example
> >
> >   unsigned short a;
> >   unsigned char b;
> >
> >   b=0xFF;
> >   a=b<<8;
> >
> > I expect to return 0 even though the logical answer is 0xFF00,
>
> I don't know why one would expect that. `b` will be promoted to
> (signed!!) `int` before the shift, and then the result of that
> assigned to `a`, wrapping as needed to fit into the `unsigned short`;
> on most reasonable systems the UB gods won't be angered.
>

C23 will be adding new types that don't have this issue (default promotion
to in).  If a variable has 3 bits, it will have 3 bits (until you mix it
with a wider variable).

Another whole class of Undefined Behavior (in ISO C) or
implementation-defined behavior (K&R C) is 2's complement arithmetic which
has never been portable, until C23.  So it's not all bad.


> OTOH, `uint16_t mul(uint16_t a, uint16_t b) { return a * b; }` is a UB
> minefield on most systems.
>
> > and I
> > _always_ code it like this:
> >
> >   b=0xFF;
> >   a=b;
> >   a<<=8;
>
> Curiously, this will be subject to the same type promotion rules as
> the original.
>
> > or alternatively
> >
> >   b=0xFF;
> >   a=((unsigned short) b)<<8;
>
> As will this. In fact, the cast here is completely superfluous; the
> shift will still be done using after promotion to signed int.
>
> > and there's other defensive stuff I do.  I honestly don't see the point
> in
> > the other changes to the language and feel they take C away from what it
> > has always been.
>
> I think an issue is that there is what people _think_ C does, and what
> C _actually_ does, and the two are often discordant;


Indeed.  That's my feeling too.  When discussing about features that
programmers don't understand why they were added, it's rather often the
case that the feature has been there for longer than they thought (if not
since forever).

this is why I
> think you see people tweaking compiler options to create a dialect
> that's reasonable to program in: given a large-enough code base, you
> inevitably end up with a very peculiar dialect, which compounds the
> problem. For example, I'm quite sure that the C dialect that Linux
> uses is not the same as the C that Windows uses, and so on. The
> compiler writers now seem very much of the mind where you point this
> out and they look at you and say, "tough."
>

Even "safe" Rust is having its share of Undefined Behavior.  Let's see how
they deal with it.
<https://github.com/rust-lang/rust/issues/107975>


>         - Dan C.
>

[-- Attachment #2: Type: text/html, Size: 4951 bytes --]

  reply	other threads:[~2023-03-13 22:26 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-10 11:37 [TUHS] Re: I can't drive 55: "GOTO considered harmful" 55th anniversary Noel Chiappa
2023-03-10 11:51 ` [TUHS] Conditions, AKA exceptions. (Was: I can't drive 55: "GOTO considered harmful" 55th anniversary) Ralph Corderoy
2023-03-10 15:54 ` [TUHS] Re: I can't drive 55: "GOTO considered harmful" 55th anniversary Dan Cross
2023-03-12  7:39   ` Anthony Martin
2023-03-12 11:40     ` Dan Cross
2023-03-12 16:40       ` Paul Winalski
2023-03-13  3:25       ` John Cowan
2023-03-13 10:40         ` Alejandro Colomar (man-pages)
2023-03-13 12:19           ` Dan Cross
2023-03-13 12:43             ` [TUHS] [TUHS]: C dialects (was: I can't drive 55: "GOTO considered harmful" 55th anniversary) Alejandro Colomar
2023-03-13 12:46               ` [TUHS] " Dan Cross
2023-03-13 16:00               ` Paul Winalski
2023-03-13 19:00                 ` Clem Cole
2023-03-13 19:09                   ` Larry McVoy
2023-03-13 19:17                   ` Steve Nickolas
2023-03-13 20:26                     ` Dan Cross
2023-03-13 22:25                       ` Alejandro Colomar (man-pages) [this message]
2023-03-13 19:24                   ` [TUHS] Re: [TUHS]: C dialects Luther Johnson
2023-03-13 19:38                     ` Luther Johnson
2023-03-14 19:48                     ` John Cowan
2023-03-14 19:56                       ` Joseph Holsten
2023-03-14 20:01                       ` Luther Johnson
2023-03-13 20:48                   ` [TUHS] Re: [TUHS]: C dialects (was: I can't drive 55: "GOTO considered harmful" 55th anniversary) Paul Winalski
2023-03-13 20:56                     ` Bakul Shah
2023-03-14  1:06                     ` Larry McVoy
2023-03-13 21:00                   ` Paul Winalski
2023-03-13 21:07                     ` Bakul Shah
2023-03-13 21:14                       ` Dan Cross
2023-03-13 22:15                         ` Dave Horsfall
2023-03-13 22:47                           ` Dave Horsfall
2023-03-14  0:23                             ` Dan Cross
2023-03-14  0:21                           ` Dan Cross
2023-03-14 13:52                             ` Chet Ramey
2023-03-14  1:27                         ` Bakul Shah
2023-03-13 21:28                       ` Paul Winalski
2023-03-14 10:04                       ` [TUHS] C dialects Ralph Corderoy
2023-03-14 20:02                         ` [TUHS] " John Cowan
2023-03-14 21:34                           ` Thomas Paulsen
2023-03-14  0:38                     ` [TUHS] Re: [TUHS]: C dialects (was: I can't drive 55: "GOTO considered harmful" 55th anniversary) John Cowan
2023-03-14  2:49                 ` Theodore Ts'o
2023-03-14  3:06                   ` G. Branden Robinson
2023-03-15  3:59 Noel Chiappa
2023-03-15  4:33 ` John Cowan
2023-03-16 22:50 ` Bakul Shah

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAJbEFzh4O-62SFh8QdhLP5bt8wOvLuvUtB87kP7e8kaLbvY=Eg@mail.gmail.com' \
    --to=alx.manpages@gmail.com \
    --cc=crossd@gmail.com \
    --cc=tuhs@tuhs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).