0
1 bit data type in C
I only need only 1 bit to store "true" and "false". char takes 1 byte or 2^(1*8)=256 bits. That mean 254 bits are being waste. I don't know how much bits are taken by bool data type declared in stdbool.h header file. https://code.sololearn.com/c9Ud7HhaCfpo/#c I think according to this program there are two possibilites. 1. bool is taking 1 byte but not letting me store any other integer other than 0 and 1. 2. bool data type is taking 1 bit but sizeof operator always return value in byte. I just wants to know what is going on here? if bool data type is actually taking 8 bits then how can i use 1 bit data type in c. Can anyone please help me ?
1 Antwort
+ 2
Check "bit fields", there are some good examples if you search with Google or YouTube
Btw you can check the size of bool by making array of booleans and printing adresses of each element (compile time arrays are continuous). I'm pretty sure it must be 1 byte for each element, but I may be wrong.
You can't store any bigger int than 1, because bool is not storing integers. This is the data type which takes "true" for any number but 0, and "false" for 0. So if you assign any number that is not 0 (false) to bool type and try to print it, it will probably always be 1. Probably there is something about it in stdbool.h documentation, but I have never dived so deep into C language
https://www.tutorialspoint.com/cprogramming/c_bit_fields.htm
This is my fav youtube channel, I think these explanations are great and easy to understand:
https://youtu.be/S30RAtj-0dQ