Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: c99 and the lack of warnings when int operations are applied to a bool |
| Date | 2012-03-23 12:07 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <4F6C9FA9.5030901@verizon.net> (permalink) |
| References | <jki5iu$jea$1@dont-email.me> |
On 03/23/2012 11:40 AM, mathog wrote:
> I just found out the hard way that the C99 "bool" type can result in
> some very hard to diagnose bugs. This problem came up in the context of
> a field in a structure which had been declared in a file "long ago and
> far away" and which I had not seen. From the usage in the file where
> the problem was found it looked like this value was an int, but it was a
> bool. As it turns out at least the gcc compiler (4.4.1) gives no
> warnings when bools are used in ways that are "int" like but make little
> or no sense for a bool.
>
> Example code showing some of these issues:
>
> /* testbool.c */
> #include <stdlib.h>
> #include <stdio.h>
> #include <stdbool.h>
>
> void main(void){
> bool avar;
> avar=1;
> (void) fprintf(stdout,"avar = %d\n",avar);
> avar=2;
> (void) fprintf(stdout,"avar = %d\n",avar);
> avar+=2;
> (void) fprintf(stdout,"avar = %d\n",avar);
> avar &= 2;
> (void) fprintf(stdout,"avar = %d\n",avar);
> avar=2;
> if(avar==2){
> (void) fprintf(stdout,"avar == 2\n");
> }
> else {
> (void) fprintf(stdout,"avar != 2\n");
> }
> }
>
> # gcc -Wall -std=c99 -o testbool testbool.c
> testbool.c:5: warning: return type of 'main' is not 'int'
> # ./testbool
> avar = 1
> avar = 1
> avar = 1
> avar = 0
> avar != 2
>
> To my mind the only two of these statements that make sense as operators
> on a bool are "=1" and "=2", both setting the bool to 1 (true), and the
> second one only if
>
> bool_variable = int_variable;
>
> is always automatically converted to
>
> bool_variable = (bool) int_variable;
>
> The rest of them make little or no sense for an operation on a bool, yet
> the compiler gives not a hint of a problem. For instance, there is no
> point, ever, in incrementing a bool by 2 since the most it can change is
> by 1. Similarly logic on any but the lowest bit is pointless since they
> will never be set. Finally, the test "avar == 2" really should trigger
> a warning since a bool can never be 2, and the 2 is not automatically
> promoted to a bool (maybe the bool is promoted to an int?) In any case
> the test is clearly a programming error since it can never be true, and
> the compiler can know that without having to reference any other piece
> of code. Seems to me that if this does not generate a warning then the
> compiler should be doing it the other way around, consider the "2" as a
> bool, thus true, and the result should be True. (Which it isn't.)
>
> What exactly are the rules for bool such that the behavior shown above
> all makes sense (and is not worthy of a compiler warning)?
There are just two special rules that apply to expressions involving
_Bool operands:
6.3.1.1p1: "The [integer conversion] rank of _Bool shall be less than
the rank of all other standard integer types."
This means, in particular, it has a rank less than that of 'int'. As a
result, operands of type _Bool are promoted to 'int' in just about every
context, before an expression is evaluated. That's why _Bool is usable
just about anywhere that an 'int' is usable.
There are exceptions, such as sizeof(avar), which is not necessarily the
same as sizeof(int), but none of those exceptions come up in your code.
6.3.1.2p1: "When any scalar value is converted to _Bool, the result is 0
if the value compares equal to 0; otherwise, the result is 1." Note that
null pointers compare equal to 0, and NaNs do not.
Everything you've seen follows from those two rules.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
c99 and the lack of warnings when int operations are applied to a bool mathog <dmathog@gmail.com> - 2012-03-23 08:40 -0700
Re: c99 and the lack of warnings when int operations are applied to a bool James Kuyper <jameskuyper@verizon.net> - 2012-03-23 12:07 -0400
Re: c99 and the lack of warnings when int operations are applied to a bool jacob navia <jacob@spamsink.net> - 2012-03-23 21:45 +0100
Re: c99 and the lack of warnings when int operations are applied to a bool Robert Wessel <robertwessel2@yahoo.com> - 2012-03-23 17:17 -0500
Re: c99 and the lack of warnings when int operations are applied to a bool jacob navia <jacob@spamsink.net> - 2012-03-23 23:29 +0100
Re: c99 and the lack of warnings when int operations are applied to a bool Ian Collins <ian-news@hotmail.com> - 2012-03-24 11:48 +1300
Re: c99 and the lack of warnings when int operations are applied to a bool jacob navia <jacob@spamsink.net> - 2012-03-24 23:47 +0100
Re: c99 and the lack of warnings when int operations are applied to a bool Willem <willem@toad.stack.nl> - 2012-03-25 09:05 +0000
Re: c99 and the lack of warnings when int operations are applied to a bool "BartC" <bc@freeuk.com> - 2012-03-25 11:30 +0100
Re: c99 and the lack of warnings when int operations are applied to a bool Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-03-25 21:05 +0100
Re: c99 and the lack of warnings when int operations are applied to a bool Lowell Gilbert <lgusenet@be-well.ilk.org> - 2012-03-25 16:15 -0400
Re: c99 and the lack of warnings when int operations are applied to a bool Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-03-25 22:14 +0100
Re: c99 and the lack of warnings when int operations are applied to a bool James Kuyper <jameskuyper@verizon.net> - 2012-03-25 23:46 -0400
Re: c99 and the lack of warnings when int operations are applied to a bool Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-03-26 12:05 +0100
Re: c99 and the lack of warnings when int operations are applied to a bool Lowell Gilbert <lgusenet@be-well.ilk.org> - 2012-03-26 15:22 -0400
Re: c99 and the lack of warnings when int operations are applied to a bool Gareth Owen <gwowen@gmail.com> - 2012-03-25 11:44 +0100
Re: c99 and the lack of warnings when int operations are applied to a bool Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-03-25 13:06 +0100
Re: c99 and the lack of warnings when int operations are applied to a bool Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-27 11:12 -0700
Re: c99 and the lack of warnings when int operations are applied to a bool Robert Wessel <robertwessel2@yahoo.com> - 2012-03-24 01:15 -0500
Re: c99 and the lack of warnings when int operations are applied to a bool jacob navia <jacob@spamsink.net> - 2012-03-24 23:29 +0100
Re: c99 and the lack of warnings when int operations are applied to a bool mathog <dmathog@gmail.com> - 2012-03-27 09:53 -0700
Re: c99 and the lack of warnings when int operations are applied to a bool Keith Thompson <kst-u@mib.org> - 2012-03-27 11:07 -0700
Re: c99 and the lack of warnings when int operations are applied to a bool James Kuyper <jameskuyper@verizon.net> - 2012-03-27 14:16 -0400
Re: c99 and the lack of warnings when int operations are applied to a bool Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-04-26 07:57 -0400
Re: c99 and the lack of warnings when int operations are applied to a bool jacob navia <jacob@spamsink.net> - 2012-04-26 14:08 +0200
Re: c99 and the lack of warnings when int operations are applied to a bool James Kuyper <jameskuyper@verizon.net> - 2012-04-26 09:09 -0400
Re: c99 and the lack of warnings when int operations are applied to a bool Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-04-26 21:29 -0400
Re: c99 and the lack of warnings when int operations are applied to a bool James Kuyper <jameskuyper@verizon.net> - 2012-04-26 21:58 -0400
Re: c99 and the lack of warnings when int operations are applied to a bool Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-04-26 22:32 -0400
Re: c99 and the lack of warnings when int operations are applied to a bool James Kuyper <jameskuyper@verizon.net> - 2012-04-27 06:31 -0400
Re: c99 and the lack of warnings when int operations are applied to a bool ImpalerCore <jadill33@gmail.com> - 2012-04-27 08:31 -0700
csiph-web