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


Groups > comp.lang.c > #390073

Re: Representation of _Bool

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c
Subject Re: Representation of _Bool
Date 2025-01-18 18:28 -0800
Organization None to speak of
Message-ID <87zfjno7ud.fsf@nosuchdomain.example.com> (permalink)
References <87tums515a.fsf@nosuchdomain.example.com> <42fcea7270de500367eceea7ad5530fd@www.novabbs.com> <20250116192737.759@kylheku.com> <593ed9cd7fcf77cdef190bf8113d85e9@www.novabbs.com>

Show all headers | View raw


learningcpp1@gmail.com (m137) writes:
[...]
> It is not documented (see this thread for GCC:
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88662). But I think it can
> be inferred from the code snippets in Keith's OP and most recent post.
> GCC seems to treat all object representations of `_Bool` other than
> 0b00000000 and 0b00000001 as trap/non-value representations.
> I am not sure about Clang, but compiling the last snippet in this
> article:
> https://www.trust-in-soft.com/resources/blogs/2016-06-16-trap-representations-and-padding-bits with
> Clang 19.1.0 and options "-std=c23 -O3 -pedantic" seems to show that
> Clang treats `_Bool` as having 254 non-value representations (see here:
> https://gcc.godbolt.org/z/4jK9d69P8).

Interesting.

Here's a program based on that snippet:

#include <stdio.h>
#include <string.h>

int f(bool *b) {
    if (*b)
        return 1;
    else
        return 0;
}

int main(void) {
    bool arg;
    unsigned char uc = 123;
    memcpy(&arg, &uc, 1);
    printf("%d\n", f(&arg));
}

With the latest gcc (14.2.0) and clang (19.1.4), it prints 1 when
compiled with "-std=c23 -O0 -pedantic", and 123 when compiled with
-O1, -O2, and -O3.

For gcc, my previous results already indicated that bool has 254
non-value representations.  For clang, the results do seem to
indicate the same thing (though I might argue that it could also
be a bug, unless the clang developers actually intended bool to
have 254 non-value representations).

-- 
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

Re: Representation of _Bool learningcpp1@gmail.com (m137) - 2025-01-17 02:47 +0000
  Re: Representation of _Bool Kaz Kylheku <643-408-1753@kylheku.com> - 2025-01-17 04:40 +0000
    Re: Representation of _Bool David Brown <david.brown@hesbynett.no> - 2025-01-17 10:18 +0100
    Eternal September server retention Was: Representation of _Bool Michael S <already5chosen@yahoo.com> - 2025-01-17 12:06 +0200
    Re: Representation of _Bool James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-01-17 14:10 -0500
    Re: Representation of _Bool learningcpp1@gmail.com (m137) - 2025-01-19 02:08 +0000
      Re: Representation of _Bool Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-01-18 18:28 -0800
  Re: Representation of _Bool Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-01-17 10:39 -0800
    Re: Representation of _Bool learningcpp1@gmail.com (m137) - 2025-01-19 02:11 +0000
      Re: Representation of _Bool Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-01-18 20:37 -0800
  Re: Representation of _Bool Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-01-17 13:34 -0800
    Re: Representation of _Bool Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-01-18 12:17 -0800
    Re: Representation of _Bool learningcpp1@gmail.com (m137) - 2025-01-19 02:30 +0000
      Re: Representation of _Bool gazelle@shell.xmission.com (Kenny McCormack) - 2025-01-19 09:31 +0000
        Re: Representation of _Bool learningcpp1@gmail.com (m137) - 2025-01-21 00:16 +0000

csiph-web