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?
-
13@Avinash: In C99 there's a `_Bool`, and a standard library to use `bool` instead. – David Thornley Nov 04 '11 at 18:45
-
Presumably by `byte` you mean `char`? – David Heffernan Nov 04 '11 at 18:52
-
1@David Heffernan `byte` and `char` are analogous, an `octet` is 8 bits – jayjay Jan 13 '15 at 11:04
-
@jayjay Which is why I asked that question – David Heffernan Jan 13 '15 at 11:19
-
@DavidHeffernan a byte is 8 bits. A char indeed is a type in C, but it's also 8 bits / 1 byte in 'storage length'. Saying `How much space does a bool take up? Is it 1 char?` wouldn't be 100% correct. Saying `How much space does a bool take up? Is it the same space as one char?` would be correct. – Paul May 13 '15 at 12:23
-
@FuaZe A char is 8 bits apart from when it is 16 bits – David Heffernan May 13 '15 at 12:39
-
http://stackoverflow.com/questions/2098149/what-platforms-have-something-other-than-8-bit-char I think a `char`'s size would be the smallest addressable on that platform. I.E. some 6-bit platforms use 6-bit `char`'s? Do you have any sources to add on 16-bits chars, I'd like to know more about that or the platforms/compilers which allocate them like this (are they widely used?). But well a `char` =/= `byte` because a `datatype` is not the same as a size. Your case only makes this worse indeed, as datatypes can vary in size. I just didn't know this counted for a char. – Paul May 13 '15 at 12:49
-
1@FuaZe In C99, byte and `char` are 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). – JeremyP May 13 '15 at 15:01
-
BTW the minimum size of `CHAR_BIT` is given as 8 in the standard so no platform can use 6 bit `char`s and be C99 compliant. – JeremyP May 13 '15 at 15:08
-
@JeremyP exactly what I was searching for. So a `byte is the smallest addressable unit of memory` (which can be anything) though a `char (in C99) is the smallest addressable unit of memory but always 8-bits or more` ? – Paul May 14 '15 at 10:40
7 Answers
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.
-
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?) – Paul May 13 '15 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; and `sizeof (_Bool[N]) != sizeof (_Bool) * N`. – Mankarse May 13 '15 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. – Oct 14 '19 at 14:17
-
@AlphaGoku Except for bit-fields, objects are composed of contiguous sequences of one or more bytes (c17 standard 6.2.6.1) – doraemon1 Aug 08 '22 at 13:58
-
And, even for bit fields, it states "The object representation is the set of m bits the bit-field comprises in the addressable storage unit holding it.". Hence, it must be of size one or more. – paxdiablo Jul 26 '23 at 01:09
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)

- 87,323
- 22
- 191
- 272
-
4No, it is at least 1 byte otherwise `&` can't behave as it is required to. – Flexo Jul 31 '17 at 19:08
-
A bit field is *also* a variable so maybe the text should be "Every *addressable* variable must have ...". – paxdiablo Jul 26 '23 at 01:11
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).

- 77,689
- 9
- 166
- 211
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

- 16,318
- 31
- 86
- 141
-
1`sizeof(bool)` most typically returns `1` but not on all platforms. The remaining bits (most typically `7`, `15`, or `31`) should be `0`, otherwise the `bool` has value that is neither `true` nor `false` and reading it is undefined behavior. – Öö Tiib Mar 11 '16 at 09: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.M Jul 10 '18 at 04:33
The exact size of a boolean will be compiler-specific but will always be at least one byte.

- 2,851
- 21
- 24

- 147,927
- 63
- 340
- 553
-
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. – Disposer Jan 22 '15 at 20:13
-
I think my answer answers the question, and the link is just for additional reference. – Eric J. Jan 23 '15 at 01:10
-
1Thank you for providing the most concise answer. There’s a lot of context involved with other answers and having a hard lower limit goes a long way with this question. – phip Aug 27 '23 at 08:54
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.

- 2,497
- 1
- 24
- 30
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;
}

- 1,709
- 1
- 24
- 27
-
5
-
It seems a little strange using C++ code to answer a question about C. The languages have diverged quite a bit and this particular behaviour may also diverge in future. – paxdiablo Jul 26 '23 at 01:15