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?
7 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
_Boolis 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.
-
2Well you could make a byte with 8 booleans in it? Or a packed struct with 8 bits? <- Not sure, heard that somewhere. With the byte, it'll require some bit-toggling to get the right value out. (Or well some instructions are optimized for this?)– PaulMay 13, 2015 at 12:26
-
6@FuaZe: You could have a byte with 8 bits in it; and use each to represent a boolean, but that would not satisfy the requirements of
_Bool. In particular, it would not be possible to take the address of an individual element of that byte; andsizeof (_Bool[N]) != sizeof (_Bool) * N.– MankarseMay 13, 2015 at 15:00 -
-
-
3@Paul:
typedef struct { _Bool b0 : 1; _Bool b1: 1; [...] _Bool b7 : 1; } BoolField;lets you create 8 booleans in 1 byte. .. Never used it though.– user6214440Oct 14, 2019 at 14:17
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)
-
4No, it is at least 1 byte otherwise
&can't behave as it is required to.– Flexo ♦Jul 31, 2017 at 19:08
In older C standards, there was no such type defined. Many embedded microcontrollers, however, include special circuitry to allow for efficient processing of single-bit flags; some allow for such processing of variables stored anywhere, while others only allow it for variables stored in a particular region of memory. Compilers for such processors allow individual variables of static duration to be declared as type "bit"; such variables will generally only take one bit of storage (allocated, if necessary, within a region that can accommodate such usage). Some compilers will allow automatic variables of non-recursive routines to be declared as 'bit' type, but will silently regard them as 'static' (the linkers provided with such compilers require that routines identify which other routines they call, and will reject programs in which routines that are not tagged as re-entrant call each other in mutually-recursive fashion).
A few points worth noting:
- Processors whose compilers support "true" bit variables can generally set, clear, or branch upon the values of such variables faster and with less code than they could set, clear, or branch upon byte-wide flags;
- Many such processors have very small amounts of RAM. On many processors, question of whether individual variables (as distinct from array elements or structure fields) take a bit or a byte each wouldn't be worth worrying about. On a processor with 25 bytes of memory, however, there's a huge difference between having 16 flags taking one byte each, versus having all 16 flags combined into two bytes.
- At least on compilers I've seen, bit variables may not be used as structure fields nor array elements, nor may one take the address of one.
I don't know enough about C99 or later versions of the C or C++ standards to know whether they have any concept of a standalone bit type which doesn't have an address. I can't think of any reason such a thing shouldn't be possible, especially the standards already recognize the concept of things like structure bit-fields which behave much like lvalues but don't have addresses. Some linkers may not support such variables, but such linkers could be accommodated by making their actual size implementation-dependent (indeed, aside from program speed or total memory usage, it would be impossible to tell whether such variables were given 1 bit or 64 bits each).
It doesnt matter whether you are in 32-bit or 64-bit, that's the size of the instructions to the processor, completely different matter.
A bool takes in real 1 bit, as you need only 2 different values. However, when you do a sizeof(bool), it returns 1, meaning 1 byte. For practical reasons, the 7 bits remaining are stuffed.
you can't store a variable of size less than 1 byte.
-> bool takes up 1 byte
-
sizeof(bool)most typically returns1but not on all platforms. The remaining bits (most typically7,15, or31) should be0, otherwise theboolhas value that is neithertruenorfalseand reading it is undefined behavior.– Öö TiibMar 11, 2016 at 9:57 -
1@ÖöTiib representation of types is implementation-defined; compilers might have the padding bits required to be 1; or allow them to be anything– M.MJul 10, 2018 at 4:33
The exact size of a boolean will be compiler-specific but will always be at least one byte.
-
1While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.– DisposerJan 22, 2015 at 20:13
-
I think my answer answers the question, and the link is just for additional reference.– Eric J.Jan 23, 2015 at 1:10
it depends on your compiler. some will take 1 byte, some the size of a int (sometime bool is just a typedef or #define of a int). I even saw bool as a short.
however don't expect it to be a bit. the necessity for any pointer to be cast-able to void* then back and keep same value makes that impossible as void* addresses bytes. BTW this is one reason why individual fields (as in int myvalue:2) cannot be addressed.
there is usually no difference in 32 or 64 build as 32 or 64 bits there is related to pointer size.
It usually takes up one byte (8 bits). The usual code I use to make sure type sizes are what I think they are follows. The sample output in the comment says my char is 1 byte (8 bits), and the same for bool.
/**
* using gcc, you can compile this with the following command:
* g++ -otype-sizes type_sizes.cpp
* and then run with with
* ./type-sizes
*
* output on my 64bit linux machine follows. Note that
* the not-so-primitive types are reporting size on
* the stack (the actual data in on the heap and is
* not reported by sizeof()). To get the "length" of
* these you can use vector<>::size() or string::length().
bits in a single char: 8
Sizes of primitive types:
char: 1
bool: 1
short: 2
int: 4
long: 8
long long: 8
float: 4
double: 8
long double: 16
Not so primitive types:
string(""): 8
string("Hello, World!"): 8
vector<int>(0): 24
vector<int>(10): 24
*
**/
#include <climits>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
cout << "bits in a single char: " << CHAR_BIT << endl
<< endl
<< "Sizes of primitive types:\n"
<< " char: " << sizeof(char) << endl
<< " bool: " << sizeof(bool) << endl
<< " short: " << sizeof(short) << endl
<< " int: " << sizeof(int) << endl
<< " long: " << sizeof(long) << endl
<< " long long: " << sizeof(long long) << endl
<< " float: " << sizeof(float) << endl
<< " double: " << sizeof(double) << endl
<< " long double: " << sizeof(long double) << endl
<< endl
<< " Not so primitive types:\n"
<< " string(\"\"): " << sizeof(string("")) << endl
<< " string(\"Hello, World!\"): " << sizeof(string("Hello, World!")) << endl
<< " vector<int>(0): " << sizeof(vector<int>(0)) << endl
<< " vector<int>(10): " << sizeof(vector<int>(10)) << endl
<< endl;
}
-
4
_Bool, and a standard library to useboolinstead.byteyou meanchar?byteandcharare analogous, anoctetis 8 bitscharare effectively synonyms. Byte is only defined as the addressable unit and consisting of a contiguous sequence of bits. The size of a byte is not defined anywhere in the C standard. If you specifically want to refer to 8 bits, use octet (like all the networking RFCs).