Can I create a variable with only one bit in the c programming language?

Viewed 173

I am new in C. And I wonder can I create a variable with only one bit in the c programming language? or a variable with three bits.

For example :

variable_type var1=0; /* 1 bit */

printf("%d",sizeof(var1)); /* output= 0.125 */

variable_type var2=5;  /* 3 bit ,binary= 101 */

printf("%d",sizeof(var2)); /* output=0.375 */

The "sizeof()" notation above is probably wrong. I did something like this to explain myself.And I assume you know. 1 byte = 8 bits.

Thanks.

1 Answers

Can I create a variable with only one bit in the c programming language?

No.

Objects are positive multiples of CHAR_BIT bits wide. CHAR_BIT is at least 8 bits. (Uncommon implementations have CHAR_BIT > 8.)

An struct object can have bit fields. Below, the member bt is 1 bit, yet struct one_bit is more bits due to padding in meeting the above requirement.

struct one_bit {
  unsigned bt:1;
}

Note:

_Bool acts as if it had only one value bit, but like any other object, _Bools cannot be smaller than CHAR_BIT bits. @John Bollinger

Related