You are correct that using bool type will generally lead to a savings in memory versus int.


There are several things to consider. First, bool is a "new" type. It was not added to the original ANSI standard, and so as a result there are many compilers out there which never supported it. Worse, Microsoft for a number of years refused to fully implement/support C99, so the standard didn't have the popularity that might have forced other vendors to upgrade their own offerings. So until C11, there wasn't much pressure on those vendors to support anything past C89.

This might not seem like much of a justification, since ... 40 years ... but people writing code for microcontrollers in the 1990's and 2000's did their design based on what their microcontroller vendor was willing to pay to have developed, compiler-wise. And mostly that was "as little as possible." So even in the 2000's, there were CPUs being released with C89-only compilers (and at least one that was still K&R).

Next is the notion of "standard integer promotions." When calling a function, small integer values get widened to larger values. In general, if a parameter is going to use an entire register, it is easier to widen the parameter to the same width as the register than to try and "mask" the unused bits all the time. Likewise, when returning a value it is easier to allocate the entire register than try to mask off unused bits after returning to the caller.

For these reasons, bool tends to convert to int when calling and when returning. If you look at the isXXX functions, or ferror(), or other functions from the standard that are used to indicate a Boolean condition, they are mostly defined to return an integer. So even today bool is not receiving the respect it might deserve.

Finally, there is the byte barrier. You can store boolean values in a single bit, if you have access to a single bit. But creating a simple type, as the committee did, prevented them from breaking the byte barrier because the C memory model won't let pointers go below that level, and because bitfields are poorly defined.

So while any competent C programmer can stuff 8 boolean values into a single char member, the bool type reserves an entire byte for every single value. Thus, bool is too small (from an integer promotion viewpoint) and also too large (from an entropy viewpoint) at the same time.

In general, yes, you can use bool to store yes/no values. But you only save memory when you have a single bool value. As soon as you have 2 or more, you're better off using a bitfield or bitwise masking. I would say the greatest value of bool lies in the ability to document your intent. By using bool you are telling future readers that you intend for this to be a yes/no field, nothing more.