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


Groups > comp.lang.c > #385925

Re: "undefined behavior"?

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c
Subject Re: "undefined behavior"?
Date 2024-06-13 16:39 -0700
Organization None to speak of
Message-ID <87h6dw5s53.fsf@nosuchdomain.example.com> (permalink)
References <666a095a$0$952$882e4bbb@reader.netnews.com> <v4d4h5$1rc9e$1@dont-email.me> <877cet7qkl.fsf@nosuchdomain.example.com> <v4epff$2912j$1@dont-email.me>

Show all headers | View raw


David Brown <david.brown@hesbynett.no> writes:
> On 13/06/2024 00:18, Keith Thompson wrote:
>> David Brown <david.brown@hesbynett.no> writes:
>> [...]
>>> I recommend never using "char" as a type unless you really mean a
>>> character, limited to 7-bit ASCII.  So if your "outliers" array really
>>> is an array of such characters, "char" is fine.  If it is intended to
>>> be numbers and for some reason you specifically want 8-bit values, use
>>> "uint8_t" or "int8_t", and initialise with { 0 }.
>> [...]
>> The implementation-definedness of plain char is awkward, but char
>> arrays generally work just fine for UTF-8 strings.
>
> Yes, but "generally work" is not quite as strong as I would like.

Agreed, but we're stuck with it.

>                                                                    My
> preference for UTF-8 strings is a const unsigned char type (with C23,
> it will be char8_t, which is defined to be the same type as "unsigned 
> char").

But then you can't use standard library functions (unless you use
pointer conversions).

#include <stdio.h>
int main(void) {
    const          char *s = "héllo, wörld";
    const unsigned char *u = "héllo, wörld";
    puts(s);
    puts(u);              // constraint violation
    puts((const char*)u); // valid but ugly
}

Implementations that make plain char signed *have to* deal sanely with
8-bit data.  The standard might permit some things to misbehave, but as
a QoI issue it's reasonably safe to assume that it Just Works unless
you're using the DeathStation 9000.

> (What happens if you have a platform that uses ones' complement
> arithmetic, with "char" being signed and a range of -127 to +127, and
> you have a u8"..." string which has a code unit of 0x80 that cannot be
> represented in "char" ?  It's just a hypothetical question, of
> course.)

C23 mandates two's-complement for all integer types.
Ones'-complement implementations are rare, and I don't think any of
them support recent C standards, so "u8"..." is going to be a syntax
error anyway.  My guess (and it's nothing more than that) is that
any ones'-complement implementations make plain char unsigned just
to avoid this kind of issue.  But even if they don't, a signed byte
with all bits 1 (-0 in ones'-complement) is likely to be treated
as 0xff by I/O functions.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */

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


Thread

"undefined behavior"? DFS <nospam@dfs.com> - 2024-06-12 16:47 -0400
  Re: "undefined behavior"? Barry Schwarz <schwarzb@delq.com> - 2024-06-12 14:30 -0700
    Re: "undefined behavior"? DFS <nospam@dfs.com> - 2024-06-12 17:53 -0400
      Re: "undefined behavior"? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-12 15:30 -0700
        Re: "undefined behavior"? DFS <nospam@dfs.com> - 2024-06-12 19:07 -0400
          Re: "undefined behavior"? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-12 17:33 -0700
            Re: "undefined behavior"? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2024-06-13 05:47 +0100
              Re: "undefined behavior"? scott@slp53.sl.home (Scott Lurndal) - 2024-06-13 15:39 +0000
                Re: "undefined behavior"? Ben Bacarisse <ben@bsb.me.uk> - 2024-06-13 18:08 +0100
                Re: "undefined behavior"? bart <bc@freeuk.com> - 2024-06-13 19:01 +0100
                Re: "undefined behavior"? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2024-06-13 19:54 +0100
                Re: "undefined behavior"? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-06-13 12:34 -0700
                Re: "undefined behavior"? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2024-06-14 00:32 +0100
                Re: "undefined behavior"? Ben Bacarisse <ben@bsb.me.uk> - 2024-06-14 00:55 +0100
                Re: "undefined behavior"? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2024-06-14 02:48 +0100
                Re: "undefined behavior"? Ben Bacarisse <ben@bsb.me.uk> - 2024-06-14 12:44 +0100
                Re: "undefined behavior"? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2024-06-14 15:30 +0100
                Re: "undefined behavior"? Richard Harnden <richard.nospam@gmail.invalid> - 2024-06-14 16:32 +0100
                Re: "undefined behavior"? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2024-06-14 19:06 +0100
                Re: "undefined behavior"? bart <bc@freeuk.com> - 2024-06-14 19:31 +0100
                Re: "undefined behavior"? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2024-06-14 20:13 +0100
                Re: "undefined behavior"? Ben Bacarisse <ben@bsb.me.uk> - 2024-06-14 22:29 +0100
                Re: "undefined behavior"? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2024-06-14 23:35 +0100
                Re: "undefined behavior"? Ben Bacarisse <ben@bsb.me.uk> - 2024-06-15 00:14 +0100
                Re: "undefined behavior"? David Brown <david.brown@hesbynett.no> - 2024-06-15 20:57 +0200
                Re: "undefined behavior"? Richard Harnden <richard.nospam@gmail.invalid> - 2024-06-15 20:27 +0100
                Re: "undefined behavior"? Ben Bacarisse <ben@bsb.me.uk> - 2024-06-15 23:13 +0100
                Re: "undefined behavior"? David Brown <david.brown@hesbynett.no> - 2024-06-16 12:53 +0200
                Re: "undefined behavior"? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2024-06-16 14:44 +0100
                Re: "undefined behavior"? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-06-14 11:49 -0700
                Re: "undefined behavior"? Ben Bacarisse <ben@bsb.me.uk> - 2024-06-14 22:32 +0100
                Re: "undefined behavior"? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-06-15 00:56 -0700
                Re: "undefined behavior"? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-13 15:58 -0700
                Re: "undefined behavior"? bart <bc@freeuk.com> - 2024-06-14 02:18 +0100
                Re: "undefined behavior"? David Brown <david.brown@hesbynett.no> - 2024-06-14 19:08 +0200
                Re: "undefined behavior"? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-14 12:34 -0700
                Re: "undefined behavior"? David Brown <david.brown@hesbynett.no> - 2024-06-15 22:13 +0200
                Re: "undefined behavior"? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-14 13:43 -0700
              Re: "undefined behavior"? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-13 14:47 -0700
                Re: "undefined behavior"? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2024-06-14 00:41 +0100
                Re: "undefined behavior"? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-13 17:09 -0700
  Re: "undefined behavior"? David Brown <david.brown@hesbynett.no> - 2024-06-12 23:38 +0200
    Re: "undefined behavior"? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-12 15:18 -0700
      Re: "undefined behavior"? David Brown <david.brown@hesbynett.no> - 2024-06-13 14:42 +0200
        Re: "undefined behavior"? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-13 16:39 -0700
      Re: "undefined behavior"? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-06-18 17:23 -0700
        Re: "undefined behavior"? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-18 17:42 -0700
          Re: "undefined behavior"? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-06-22 09:28 -0700
    Re: "undefined behavior"? DFS <nospam@dfs.com> - 2024-06-12 18:29 -0400
      Re: "undefined behavior"? Ike Naar <ike@sdf.org> - 2024-06-13 07:25 +0000
        Re: "undefined behavior"? DFS <nospam@dfs.com> - 2024-06-13 11:13 -0400
          Re: "undefined behavior"? scott@slp53.sl.home (Scott Lurndal) - 2024-06-13 15:40 +0000
          Re: "undefined behavior"? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2024-06-13 15:49 +0000
            Re: "undefined behavior"? DFS <nospam@dfs.com> - 2024-06-13 13:05 -0400
      Re: "undefined behavior"? David Brown <david.brown@hesbynett.no> - 2024-06-13 15:15 +0200
        Re: "undefined behavior"? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-13 16:47 -0700
          Re: "undefined behavior"? David Brown <david.brown@hesbynett.no> - 2024-06-14 19:13 +0200
  Re: "undefined behavior"? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-06-12 23:38 +0200
    Re: "undefined behavior"? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-12 15:22 -0700
      Re: "undefined behavior"? DFS <nospam@dfs.com> - 2024-06-12 18:34 -0400
        Re: "undefined behavior"? David Brown <david.brown@hesbynett.no> - 2024-06-13 15:21 +0200
          Re: "undefined behavior"? DFS <nospam@dfs.com> - 2024-06-13 10:38 -0400
            Re: "undefined behavior"? David Brown <david.brown@hesbynett.no> - 2024-06-14 19:18 +0200
              Re: "undefined behavior"? scott@slp53.sl.home (Scott Lurndal) - 2024-06-14 17:36 +0000
                Re: "undefined behavior"? David Brown <david.brown@hesbynett.no> - 2024-06-15 22:15 +0200
              Re: "undefined behavior"? DFS <nospam@dfs.com> - 2024-06-14 19:05 -0400
                Re: "undefined behavior"? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-14 18:39 -0700
                Re: "undefined behavior"? DFS <nospam@dfs.com> - 2024-06-14 23:49 -0400
                Re: "undefined behavior"? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-14 20:56 -0700
                Re: "undefined behavior"? DFS <nospam@dfs.com> - 2024-06-15 00:45 -0400
                Re: "undefined behavior"? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-06-15 07:03 +0200
                Re: "undefined behavior"? DFS <nospam@dfs.com> - 2024-06-15 07:39 -0400
                Re: "undefined behavior"? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-06-15 01:05 -0400
                Re: "undefined behavior"? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-14 22:20 -0700
                Re: "undefined behavior"? bart <bc@freeuk.com> - 2024-06-15 09:37 +0100
                Re: "undefined behavior"? David Brown <david.brown@hesbynett.no> - 2024-06-15 22:22 +0200
      Re: "undefined behavior"? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-06-13 02:19 +0200
        Re: "undefined behavior"? David Brown <david.brown@hesbynett.no> - 2024-06-13 15:28 +0200
  Re: "undefined behavior"? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-12 14:57 -0700
  Re: "undefined behavior"? bart <bc@freeuk.com> - 2024-06-13 10:43 +0100
  Re: "undefined behavior"? Bonita Montero <Bonita.Montero@gmail.com> - 2024-06-13 11:45 +0200

csiph-web