r/C_Programming • • Jul 07 '26

If statement checking a bool array?

Hi,

I need a custom sized bit vector so uint8_t won't suffice so the idea was to just initiate a bool array with size so then say i want to compare it. Ex**. the bool array is 6 bits and tried with `if (bool_var = 011001) { // do something}`, i suspect it comes just compares to an int given it compiles and wont run. Any idea on how to make it work?**

0 Upvotes

53 comments sorted by

View all comments

Show parent comment

1

u/TheOtherBorgCube Jul 08 '26

C99 says

An implementation may allocate any addressable storage unit large enough to hold a bit-field.

The bit-field type may be used to determine what the size of the storage unit will be.

#include <stdio.h>

struct state1 {
    unsigned int s0:1;
};
struct state2 {
    unsigned short s0:1;
};
struct state3 {
    unsigned char s0:1;
};
struct state4 {
    unsigned long long int s0:1;
};

int main() {
    printf("%zd %zd %zd %zd\n",
        sizeof(struct state1),
        sizeof(struct state2),
        sizeof(struct state3),
        sizeof(struct state4));
}

$ gcc foo.c
$ ./a.out 
4 2 1 8

Though with suitable application of #pragma or __attribute__, even an 8-byte struct can be reduced to a single byte.

Almost everything about bit-fields is implementation specific, so YMMV.