Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C how much space does a bool (boolean) take up? Is it 1 bit, 1 byte or something else?

Tags:

c

boolean

In C how much space does a bool (boolean) take up? Is it 1 bit, 1 byte or something else? Does it matter if the program is 32-bit or 64-bit?

like image 958
user950891 Avatar asked Nov 04 '11 18:11

user950891


2 Answers

If you are referring to C99 _Bool try:

printf("%zu\n", sizeof(_Bool)); /* Typically 1. */ 

Note the standard says:

6.2.5

An object declared as type _Bool is large enough to store the values 0 and 1.

The size cannot be smaller than one byte. But it would be legal to be larger than one byte.

like image 59
cnicutar Avatar answered Sep 23 '22 10:09

cnicutar


The smallest addressable "thing" in C is a char. Every variable in C must have a unique address, therefore your bool can't be smaller than that. (Note that char isn't always 8 bits though)

like image 28
Flexo Avatar answered Sep 26 '22 10:09

Flexo