Ruminations on the _Bool variable type

One of the C language keywords added in the C99 update is _Bool. It’s an underscore keyword, which means it lacks the respect gained by the other keywords — even the useless ones like auto.

The _Bool keyword creates a boolean variable type. Boolean, named after English mathematician George Boole, refers to values and operators that deal logic. In programming, it means TRUE and FALSE operations, but also lends itself to the binary values, 0 and 1. A _Bool variable type holds either 0 or 1. It’s one-bit in size.

Theoretically, a _Bool is one-bit in size. If you run the sizeof keyword on a _Bool, you get the value 1, the same as for a char variable.

Internally, who knows what’s going on with the _Bool variable type. It’s most likely a specialized char variable, but that’s not how the compiler treats it. If you create and use a _Bool variable, it’s value is either 0 or 1. If you try to stuff another value into the variable, it becomes 0 if that value is zero, 1 otherwise.

You can use a _Bool variable to act as a toggle, such as demonstrated in this month’s Exercise. I devised asolution that uses a _Bool variable type; click here to view it.

The following code demonstrates what happens when you increment a _Bool variable:

#include <stdio.h>
  
int main()
{
    _Bool alpha = 0;
    int i;

    for(i=0;i<10;i++)
    {
        printf("%d\n",alpha);
        alpha++;
    }

    return(0);
}

_Bool variable alpha is incremented in Line 11. Even so, the output shows zero for the first turn of the loop, then ones afterwards:

0
1
1
1
1
1
1
1
1
1

If you modify Line 11 to read alpha--;, and decrement _Bool variable alpha, the output changes to this:

0
1
0
1
0
1
0
1
0
1

Before I ran this code, my assumption would be that incrementing or decrementing a _Bool variable toggles between 1 and 0. After all, that’s how a bit works inside the computer. But in C, incrementing a _Bool variable is interpreted as positive, so 1 is returned. Decrementing “wraps” the value to 1, which is then decremented back to zero.

I explore more tricks with the _Bool variable type in next week’s Lesson.

4 thoughts on “Ruminations on the _Bool variable type

  1. Just as a side note, if you include stdbool you can use bool, true and false which are really just aliases for _Bool, 1 and 0. (It seems they did it like this because a lot of people had already defined boo, true and false in their own code so making them keywords would break this code.)

    I doubt the specification says how a compiler has to deal with _Bool but I THINK it allocates 1 byte for the first _Bool but only uses 1 bit, but will use the unused bits for any further _Bools used up to 8, therefore you can use up to 8 bools and still only use 1 byte. You can do the same thing with bitfields but _Bool is far simpler.

  2. I think I’ve just invented something – a function with a static bool:

    _Bool get_toggle()

    which flips the bit every time it is called.

    (However, whenever I “invent” anything I usually find out somebody already invented it about fifty years ago!)

  3. I tested the theory that a _Bool is a char or byte that truncates the left-most 7 bits. It’s not. Otherwise the behavior when you increment would echo the behavior of decrementing: 1 0 1 0 1. So something other is going on inside.

    The _Bool function is great! I didn’t think of that, but YES! You can write a _Bool toggle function. Brilliant!

  4. _Bool gettoggle()
    {
        static _Bool b = true;

        return b = !b;
    }

    int main()
    {
        for(int i = 0; i < 32; i++)
        {
            gettoggle() == true ? puts(“true”) : puts(“false”);
        }
    }

Leave a Reply