r/C_Programming • • Sep 21 '26

Question Warn on bool x = 1/0?

Hi y’all. It’s me waging war against sloppy types again. This time I want to make sure that ints are not assigned to bool fields and variables. Is there some compiler flag on GCC and/or Clang that detects when this happens? I tried

  • -Wbool-compare
  • -Wbool-operation
  • Compiling with g++ instead of gcc

No warnings shown on assigning 0/1 to a boolean field. Is there a way to warn on that?

11 Upvotes

29 comments sorted by

2

u/WittyStick Sep 22 '26

There's a --Wint-in-bool-context which will detect some cases, such as where you use integers in if/while condition.

bool itself is an integer type though, which just accepts 0 as false and nonzero as true.

With GCC you can use a different value for true and false with the hardbool attribute. Eg, you can make 0xFF be false and 0x00..0xFE be true.

typedef char __attribute__((hardbool(0xFF))) mybool;

1

u/aartaka Sep 22 '26

Oh, I’ll keep these in mind! Thanks a lot!

25

u/FancySpaceGoat Sep 21 '26 edited Sep 21 '26

The type of comparison expressions is int.

So by your rule bool some_bool = v > 10; would warn, which is obviously undesirable.

You may be tempted to think that this can be special-cased away. But since type inference is a thing, we need to also allow initializing from an int variable:

// This is obviously fine
auto test = v > 10; // type of test is deduced as int 
bool some_bool = test;

// But it's actually the same thing as this:
int some_int = v > 10; 
bool some_bool = some_int;

And at that point warning on bool some_bool = 1; just seems silly.

All this to say that doing this right goes beyond the scope of what a compiler could/should be doing as part of its warnings. This is more of a job for a dedicated static analyzer tool like clang-tidy.

7

u/TheThiefMaster Sep 21 '26

Which I'm amazed they haven't changed to bool now that bool is a first class type.

It wouldn't even be incompatible thanks to the implicit conversions that exist.

That said, OP is still being ridiculous.

18

u/FancySpaceGoat Sep 21 '26 edited Sep 21 '26

sizeof(x>10) still needs to return the same value as before to maintain proper backwards compatibility.

And I'm sure there are other expression-type implications, especially if you throw various compiler intrinsics and pragmas into the mix.

retconning the type of certain expressions is a recipe for backward-compatibility breaks.

-4

u/TheThiefMaster Sep 21 '26

Only if the code becomes invalid if it gets 1 instead of 4 - in which case it was already relying on implementation specific behaviour (there's no requirement that sizeof(int)!=1)

4

u/FancySpaceGoat Sep 21 '26

You are *technically* correct, but it's sort of expected that this value remains stable for a given compiler/architecture/os triplet.

The standard forcing compiler writers to break that convention would be a bit of a dick move.

-2

u/TheThiefMaster Sep 21 '26

You're also expecting that there's any code that contains sizeof(some comparison), which would be an odd thing to write.

6

u/FancySpaceGoat Sep 21 '26

Perhaps, but a macro that takes the sizeof() of an expression passed to it is not that odd.

2

u/flyingron Sep 21 '26

Sort of. The language limitations require that int be at least 16 bits. While you could have 16 bit chars, you'd lose the ability to address 8 bit things (such is the massive suckiness of the overloading of char for three different meanings).

3

u/TheThiefMaster Sep 21 '26

And DSPs exist that are configured like that, unfortunately.

3

u/ericonr Sep 21 '26

It literally would?

Some macro uses typeof or auto to create a variable and assign its value. The result of a comparison is passed to that macro. Someone passes a pointer to that variable anywhere. A 4 byte (likely, for most platforms) value just became 1 byte. You might catch a compiler error due to incompatible types, or the pointer goes through void * and you're screwed...

1

u/RealisticDuck1957 Sep 21 '26

Modern compilers have a setting to warn if an assignment may overflow.

5

u/FancySpaceGoat Sep 21 '26 edited Sep 21 '26

Implicit conversions from int to bool have special rules to preserve existing semantics. Regular narrowing rules and their associated warnings don't apply here.

1

u/RealisticDuck1957 Sep 22 '26

So you actually loose warning capability if you do something other than aliasing bool to an appropriate int type?

1

u/FancySpaceGoat Sep 22 '26 edited Sep 22 '26

All I'm saying is there's never anything to warn against when converting an integer of any size to a bool. All values are well-defined. 0 -> `false`, anything else -> `true`.

I'm not sure what you mean by "aliasing bool to an appropriate int type" If you are using C23, `bool` is its own thing. If you are using C99, then <stdbool.h> aliases it to _Bool, which is the same thing. If you are using something older than C99, then you are outside of the scope of this discussion.

1

u/RealisticDuck1957 Sep 22 '26

Sorry. I learned C way back when an alias of an integer type was all you had for booleans. Sounds like the new bool support does something like

bool b = (x)?true:false;

1

u/FancySpaceGoat Sep 22 '26

Well... "new" as in 25 years ago, but yes, that is how it works.

7

u/_thetek_ Sep 21 '26

IIRC, clang-tidy might have a diagnostic for that.

2

u/pfp-disciple Sep 21 '26

Would -Wfloat-conversion give you what you need? It's a subset of -Wconversion

4

u/AKostur Sep 21 '26

I think OP was meaning ”1 or 0” and not “1 divided by 0”. Besides, division by 0 is a whole different problem.

3

u/pfp-disciple Sep 21 '26

Oh, yeah (facepalm) I didn't think that far. You're right, division by zero is a much bigger problem. 

3

u/RealisticDuck1957 Sep 21 '26

I was wondering why "1/0" didn't flag an error with the way modern compilers like to resolve expressions at compile time when possible.

0

u/[deleted] Sep 21 '26

[deleted] — view deleted comment

13

u/FancySpaceGoat Sep 21 '26 edited Sep 21 '26

> there is no boolean scalar data type.

There is now. _Bool as of C99 with stdbool.h providing bool as an alias of it. bool aliases to it automatically became a proper keyword as of C23.

3

u/realhumanuser16234 Sep 21 '26

bool is the current c keyword _Bool is deprecated

2

u/FancySpaceGoat Sep 21 '26

Thanks for the correction. I made the assumption about the transition mechanism because of longstanding backward-compatibility principles.

0

u/[deleted] Sep 21 '26

[deleted] — view deleted comment

14

u/FancySpaceGoat Sep 21 '26 edited Sep 21 '26

Wanna feel old? C99, which introduced the boolean scalar data type, was over 25 years ago.

3

u/reini_urban Sep 22 '26

But BOOL_WIDTH still varies between 1 and 8. This will need another 15 years to converge