Reputation: 971
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?
Upvotes: 46
Views: 127778
Reputation: 182794
All references to the standard herein refer to ISO C17
.
The following code will show you the size of a boolean, which must be at least one given that this is the minimum addressable unit:
printf("%zu\n", sizeof(_Bool)); // Typically, but not necessarily, 1.
The standard (6.2.5 Types
, though this is also the case back to C99
) states:
An object declared as type
_Bool
is large enough to store the values 0 and 1.
As mentioned earlier, the size cannot be smaller than one(1) but it would be legal for it to be larger, as supported by footnote 124 in section 6.7.2.1 Structure and union specifiers
of the standard, noting in particular the "at least" section:
While the number of bits in a
_Bool
object is at leastCHAR_BIT
, the width (number of sign and value bits) of a_Bool
may be just 1 bit.
Upvotes: 44
Reputation: 150238
The exact size of a boolean will be compiler-specific but will always be at least one byte.
Why is a char and a bool the same size in c++?
Upvotes: 3
Reputation: 88801
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)
Upvotes: 11
Reputation: 1819
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;
}
Upvotes: 0
Reputation: 81347
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:
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).
Upvotes: 5
Reputation: 2580
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.
Upvotes: 0
Reputation: 17505
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
Upvotes: 5