are there any 1 bit variables in C?
for example if int is 4 bits(I think)
are there any 1 bit variables in C?
for example if int is 4 bits(I think)
Last edited by oods1; 01-12-2021 at 01:10 PM.
No.are there any 1 bit variables in C?
No, char, short, long, are in bytes, a byte is 8 bits.for example if int is 4 bits(I think)
This sounds like an XY problem. What would you need a 1-bit variable for?
Do you mean one bit or one byte? Most computers address memory in 8 bit bytes. In C, and int is usually 4 bytes, or 32 bits.
A boolean variable is logically one bit, either on or off. However C doesn't support this as bits are not directly addressable,
declaring a pointer to a one bit variable would lead to a lot of complexity for little advantage.
So in C booleans are wider types, and any non-zero value is considered true.
In C, a "char" is one byte, usually 8 bits. That's the smallest addressable unit of memory.
I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
Visit my website for lots of associated C programming resources.
https://github.com/MalcolmMcLean
I think you're a bit confused. The smallest variable possible is the smallest addressable value by the system. On most systems this is 1 byte, not 1 bit. A char is 1 byte wide and is the smallest type available.
However, there are struct bit fields. Bit fields are used to pack data into a struct as tightly as possible. For example, you could have a struct like this.
Each of these fields will only take 1 bit of memory and the C compiler will pack them together in the same byte. However, this is a bit misleading. You cannot take the address of any of these bit fields because you cannot address a single bit. Behind the scene the C compiler and just setting or clearing single bits by taking a larger value from memory, toggling and single bit and replacing the entire value. Bit fields blur the line a bit of what is and is not a "variable."Code:struct flags { unsigned active:1; unsigned alive:1; unsigned has_fuel:1; unsigned flying:1; };