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

7

u/F1nnyF6 Jul 08 '26

Short answer no. It wouldnt work. In C each bool is an integer type much larger than 1 bit (typically your cpus native word size I believe), so it would essentially be an array of uint32_t and cannot be initialised with the syntax described, which is for representing a single number.

Why do you want a bool array though?? As people are saying, the by far most efficient way to have an array of boolean values is with the smallest integer type that fits the number of booleans you need. In your case of 6 or 7 bits, that would be a (unsigned) char/int8_t.

From your other comment where you fear about wasting a whopping 2 bits, it seems you are under the impression a bool is just a single bit in C? This is not the case. So an array of bools would be much larger and more wasteful, as well as more clumsy to work with and slower than using the normal approach of masking and shifting an integer type.