2

I sometimes find that using bools in integer expressions make for shorter and simpler code. For example I prefer

int n_items = has_a + has_b + has_box * 5;

over

int n_items = (has_a ? 1 : 0) + (has_b ? 1 : 0) + (has_box ? 5 : 0);

This should be safe since false==0 and true==1. Are the any risks or gotchas one should know about?

With bools I mean either C99 bools or boolean expressions like a>b. Of course I have to watch out for values that are not actually boolean, like the return value of isdigit().

Tor Klingberg
  • 4,790
  • 6
  • 41
  • 51
  • 3
    This is a great way to write very confusing code. – xxbbcc Sep 25 '15 at 15:22
  • 2
    There is no such thing as a boolean. C only has integers, so this question makes no sense. "Booleans" are **already integers**. There is [`_Bool`](http://stackoverflow.com/questions/1608318/is-bool-a-native-c-type) in C99, but again this still holds the *unsigned integer* value of `1` or `0`. – user229044 Sep 25 '15 at 15:22
  • 1
    @meagar: There is a datatype `_Bool`/`bool` in C. However, it actually **is** an unsigned integer type with values `0` and `1`. – too honest for this site Sep 25 '15 at 15:27
  • Boolean expressions do not yield a `bool` result, but an `int` (compatibility). – too honest for this site Sep 25 '15 at 15:28
  • xxbbcc, Richard Chambers: What specifically do you find confusing or unsafe about this? – Tor Klingberg Sep 25 '15 at 15:30
  • C has no boolean type, so the first approach is perfectly okay. But you have to be sure that it will never happen that f.e `has_a = -1` and `has_b = 1` In this case both will be true but adding them will be false – Mr. E Sep 25 '15 at 15:31
  • In theory this should be safe, but it depends how you're storying your booleans. As far as the `if` statement is concerned, zero is false, and non-zero is true (so not necessarily 1!). So booleans with true values aren't necessarily stored as `1`. I guess the `bool` type ensures `1` is stored for all `true` values though? – Noldorin Sep 25 '15 at 15:31
  • 1
    @Emi1303 C has `_Bool` boolean type. – ouah Sep 25 '15 at 15:43
  • It heavily depend on the functions that returns those values. You need to require all of them to return 1 for true. – user3528438 Sep 25 '15 at 16:43
  • I have worked with a C compiler, where 'true' was defined as 2 rather than 1. Such experiences have me testing for 'false' and '!false' I.E. I never check directly for the 'value' of 'true' – user3629249 Sep 27 '15 at 14:48

1 Answers1

4

One of the risk with bool is it has a different semantic than int and a lot of programs already use bool as an alias to int and are not using C99 _Bool typedef from stdbool.h (e.g., programs developed for C89 or meant to be compatible with):

typedef int bool;

Then this expression for example may have different meanings:

int a = (bool) 0.5;  // if bool is _Bool, evaluates to 1
                     // if bool is int, evaluates to 0

This can create some really nasty bugs.

On other hand a shorter form than (has_a ? 1 : 0) is the idiomatic !!has_a.

ouah
  • 142,963
  • 15
  • 272
  • 331