3

There is a special, non-standard Arduino data type called "boolean".

How does it compare with the standard C++ data type "bool", and what are the reasons to use one or the other?

If they are identical in usage, why do both exist, and which one is preferred in Arduino code?

4
  • This is a basic programming question, not specific to Arduino. The Definitive C++ Book Guide and List. Or Google Aug 15 '17 at 15:38
  • Welcome to Arduino Stack Exchange. Please take the tour at arduino.stackexchange.com/Tour to get the most out of this site. Boolean means binary, like true/false. Do a Google search on "Arduino data types" to find out about it.
    – SDsolar
    Aug 15 '17 at 17:49
  • 5
    I disagree that this is not an Arduino specific question. boolean is a non-standard type alias for bool set in the Arduino core library and therefore is very much Arduino specific. See my answer for details.
    – per1234
    Aug 15 '17 at 23:27
  • 1
    Renaming something doesn't mean it's then specific to Arduino.
    – Avamander
    Aug 16 '17 at 7:20
8

While the other answers give generally useful information, the authors seem to have not read the question very carefully because they are talking about the bool type rather than the boolean type. In the Arduino API, boolean is a type alias for bool, as defined in Arduino.h:

https://github.com/arduino/Arduino/blob/1.8.3/hardware/arduino/avr/cores/arduino/Arduino.h#L125

typedef bool boolean;

So you can use the boolean type just as you would bool and any standard C++ programming reference will instruct you in the use of bool. For this reason the other answers which mistakenly refer to bool are still somewhat useful.

Apparently at some point in the past the Arduino folks decided the name boolean would be more beginner friendly than bool, perhaps to be consistent with the Processing language. However, as discussed here:

https://github.com/arduino/Arduino/issues/4673

the unnecessary use of a non-standard type is now considered by some to be a poor decision as its use makes code less portable and really doesn't provide any clear advantage over bool. A proposal is in the works to remove the use of boolean from all official Arduino code and document bool in the Arduino reference and some even wish to formally deprecate boolean. Therefore my advice is to use bool instead of boolean in your code.

1

A Boolean variable type is one that has only 2 possible values, true or false. Internally, it is created as an int (16-bit integer) or a uint8_t type (8-bit integer).

You can assign the "values" of true or false using the assignment operator:

bool foo;
foo = true;
if (foo) {
  do_something();
}

Internally, a true is a 1 and a false is a 0, and the number values could be equally substituted.

Boolean types in Arduino and C++ mostly exist for readability, and not for any particular efficiency over int type.

1

The official reference can be found here

It is best to always use true and false, and not use numbers.

Below some examples are given

Assign false resp true to a Boolean:

bool a = true;
bool b = false;

Check the Boolean value:

if (b)
{
   // Executed when b is true
}
else
{
   // Executed when b is false
}

if (a && b)
{
   // Executed when a is true and b is true
}

if (a || b)
{
   // Executed when a is true or b is true
}

If b is false, a will be false, otherwise a will stay as it was

a &= b;

If b is true, a will be true, otherwise a will stay as it was

a |= b;

// If a is true it will be false and vice versa.

a = !a;
1
  • 1
    That last one should be a = !a shouldn't it? ~ is a bitwise not. Depending on how true is defined ~true may still be true.
    – Delta_G
    Aug 15 '17 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy