Difference between bool and boolean

Is bool a datatype or boolean a datatype??
Which is which, i can't tell the difference?

If you look in Arduino.h, you'll find:

typedef bool boolean;
1 Like

Worth noting
Boolean in Arduino land still occupies a full byte.

If you’re keen, try implementing bit fields as manually setting, clearing bits in a byte defined by yourself.

8/16/32 bit is a lot more space efficient.

Are the same thing
boolean or bool . bool short for boolean.
Like integer and int. int short for integer.

1 Like

boolean is a bool in Arduino context. Not worth to use.
Just use bool.

2 Likes

+1

Same as the byte data type. They are just synonyms of the real thing meant to make code supposedly more readable to some.

Learn the real thing

3 Likes

The bool type occupies the space of one byte,
but it can only assume values 0 or 1. (true or false)
The byte type also occupies only one byte,
but can assume values from 0 to 15 (0x00 to 0x0F)..
sorry
but can assume values from 0 to 255 (0x00 to 0xFF).

See this example code:

1 Like

Oops

1 Like

tks

1 Like

in C++ bool is a native type, so it's not 0 or 1, it's really to be seen as holding a truth value (true or false). The fact that you can get an implicit promotion to an integral type with 0 being false and 1 being true is something else that is there for historical / C compatibility reason but should not be encouraged.

Note also that the value of sizeof(bool) is implementation defined and might differ from 1 - so from a pure standard perspective a bool could occupy more than 1 byte.

for more on types, read Fundamental types - cppreference.com

1 Like

Recently I was looking at a some disassembled programming, and it came to me why the discussion about bool, Boolean and datatypes is so confusing. The question asked is something like: is it char? Is it byte? 1 or 2 bnytes?

Bool or Boolean is usually not used as data but as the choice to take a certain jump or not. Therefore the question whether it is 1 byte or 2, type char, byte or int, is missing the point.
In C++ a datatype bool is defined, because the compiler needs to deal with condition jumps, but it is not byte, char or int!

bool is the official C++ data type for a truth value
boolean is something arduino defined to be fancy :slight_smile: but it's the same thing

the standard says

Boolean type
bool — type, capable of holding one of the two values: true or false. The value of sizeof(bool) is implementation defined and might differ from 1.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.