Sujet : Re: how cast works?
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 11. Aug 2024, 21:38:23
Autres entêtes
Organisation : None to speak of
Message-ID : <87bk1yg5hc.fsf@nosuchdomain.example.com>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
User-Agent : Gnus/5.13 (Gnus v5.13)
Thiago Adams <
thiago.adams@gmail.com> writes:
Em 8/10/2024 9:10 PM, Keith Thompson escreveu:
Thiago Adams <thiago.adams@gmail.com> writes:
Em 8/10/2024 1:14 PM, Bart escreveu:
>
Bart, Does your compiler support the `bool` type, where the value
is always either 1 or 0?
There is a bool type, but it is treated like unsigned char, so is
non-conforming.
>
I do the same in my compiler , when I transpile from C99 to C89.
I was thinking how to make it conforming.
For instance on each write.
>
bool b = 123; -> unsigned char b = !!(123);
>
The problem this does not fix unions, writing on int and reading from char.
I don't think you need to fix that.
>
[....]
>
Summary:
Conversion from any scalar type to _Bool is well defined, and must
yield
0 or 1.
>
>
I will fix in terns of expressions types.
>
- In this case cast to bool
- Assignment to bool
You need to cover all cases where a scalar value is converted to _Bool.
That includes (explicit) casts, assignment, initialization, argument
passing, and returning from a _Bool function.
Ideally you'd have one place in your code that handles conversions, but
you'll want to test all those cases and more.
It's possible to force a representation other than 0 or 1 into a _Bool
object, bypassing any value conversion.
Conversion from _Bool to any scalar type is well defined if the
operand is a _Bool object holding a representation of 0 or 1.
Conversion from _Bool to any scalar type for an object holding some
representation other than 0 or 1 either yields 0 or 1 (depending
on the low-order bit) or has undefined behavior.
>
I did a sample now..
>
#include <stdio.h>
>
int main() {
union {
int i;
_Bool b;
} data;
data.i = 123;
printf("%d", data.b);
}
>
it printed 123 not 1.
I get 123 if I compile with gcc, 1 if I compile with clang.
Both results are valid.
So I think the assignment and cast covers all/most cases.
(From some previous tests I thought this was printing 1)
>
The motivation for C89 in cake was not to support old compilers, but
generate code that is compatible with C++98. In this aspect bool was
already there in C++98.(This just gave me idea to add target c++98)
C++ may have different rules, which you can discuss in comp.lang.c++.
>
>
>
>
>
A lot of your articles have multiple blank lines at the end. Can you
try to avoid that?
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */