In C ++, what's the point of using bool instead of char?

sizeof (char) and sizeof (bool) are 1 (in my compiler / system / regardless, I heard that this is not always the same value), bool can only store true or false, while a char can take more values and can act as several bool variables using bitwise operators (8 bits, each bit can be used as 1 bool for a total of 8 bools)

So, is there any advantage when using bool instead of char?

So besides readability, is there anything else? I read somewhere that int is processed faster than short or byte, even if it takes up more memory. Is there any speed difference between char and bool?

+4
source share
2 answers

The main point of use boolis to express intention. If a variable is intended to store a value using true / false semantics, the use of additional values ​​is only a potential source of errors.

+11
source

boolis an abbreviation of "boolean", so anyone familiar with Boolean algebra can be sure that a variable can only store one of two logical values ​​( trueor false): if you need a variable that can only be in one of these two logical states, is there any reason to use something that could store anything else?

, , sizeof(char), 1 , sizeof(bool) . 1 , .

[ ]

, ? , , , .

+3

All Articles