user950891
user950891

Reputation: 971

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

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

Answers (7)

cnicutar
cnicutar

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 least CHAR_BIT, the width (number of sign and value bits) of a _Bool may be just 1 bit.

Upvotes: 44

Eric J.
Eric J.

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

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

John
John

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

supercat
supercat

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:

  1. 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;
  2. 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.
  3. 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).

Upvotes: 5

armel
armel

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

BuZz
BuZz

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

Related Questions