Keeping Up with the Booleans

As a programmer well-versed in Assembly Language, I’m all too familiar with the bits that populate a byte. At the low, low level you find plenty of tools to do bit manipulation. The C language has bitwise operators as well, which remains one of its advantages over the newer, trendier programming languages. So, how does all this fun activity cross over into the realm of the _Bool or bool data type in C?

I shall use the more apropos bool in this Lesson. Primarily because it’s a new data type with the C23 standard, but it’s also the way the stdbool.h header sets it up as mentioned in last week’s Lesson.

In Assembly, I can use binary digits to concoct certain values. For example, in the following code, a bool array consists of what I believe to be eight binary digits.

2026_09_05-Lesson-a.c

#include <stdio.h>
#include <stdbool.h>

int main()
{
    bool data[] = { 0, 0, 1, 0, 0, 0, 0, 1 };

    printf("%c\n",(char)data);

    return 0;
}

Array data[] contains eight one/zero values of the bool type. This declaration is legit, but it turns out it doesn’t build a byte as it would according to my Assembly language brain. If it did, the printf() statement would output the result as an 8-bit value, a char (character), ASCII code 0x21 which represents the ! (bang). Instead, here is the output on one of my systems:

d

I see no output when the program is run on another system. Regardless, a compiler warning is issued explaining that the data[] array is technically composed of integers and not true binary digits. This result is something I suspected: How is binary data stored as a bool? Are they truly individual bits or are they integers wherein only one digit is considered valid?

The following code adds two statements to help describe what’s going on with the bool in C:

2026_09_05-Lesson-b.c

#include <stdio.h>
#include <stdbool.h>

int main()
{
    bool data[] = { 0, 0, 1, 0, 0, 0, 0, 1 };

    printf("Size of bool: %lu\n",sizeof(bool));
    printf("Size of 'data': %lu\n",sizeof(data));
    printf("%c\n",(char)data);

    return 0;
}

This update to the code uses the sizeof(bool) operator to return the number of bytes occupied by a bool value. I suppose the result could be 0.125 (1/8), but the value generated is 1, as shown in the output:

Size of bool: 1
Size of 'data': 8
 

Most telling in the output is the size of the data[] array: It’s 8 bytes long. The Boolean values are stored as char data types, technically a small integer.

One more modification as long as I’m messing around:

2026_09_05-Lesson-c.c

#include <stdio.h>
#include <stdbool.h>

int main()
{
    bool data = 0x21;

    printf("Size of bool: %lu\n",sizeof(bool));
    printf("Size of 'data': %lu\n",sizeof(data));
    printf("%c\n",(char)data);

    return 0;
}

Rather than build a bool array, this update to the code assigns bool value data to 0x21 (ASCII code for !). Here is output from a sample run:

Size of bool: 1
Size of 'data': 1
 

The printf() statement doesn’t output the ! character. Internally, the value 0x21 is stored, but only the far right bit is read: one. This result is output, but you can’t see ASCII code 0x01. Instead, I ran the program through the hexdump utility to see this:

00000000  53 69 7a 65 20 6f 66 20  62 6f 6f 6c 3a 20 31 0a  |Size of bool: 1.|
00000010  53 69 7a 65 20 6f 66 20  27 64 61 74 61 27 3a 20  |Size of 'data': |
00000020  31 0a 01 0a                                       |1...|
00000024

The value 0x01 is output (above in red).

In conclusion, while a bool data type represents a binary value of one or zero, the data is stored in a byte (char). Only the far right digit in the byte is significant. But the takeaway is that an array of 8 bool values occupies 8 bytes of memory not 8 bits.

I continue my exploration of bool values in next week’s Lesson.

Leave a Reply