I was hoping somebody could explain why
#include <stdbool.h>
printf("size of bool %d\n", sizeof(bool));
printf("size of int %d\n", sizeof(int));
outputs to
size of bool 1
size of int 4
I've looked at http://pubs.opengroup.org/onlinepubs/009695399/basedefs/stdbool.h.html which seems to indicate that bool is essentially a macro for _Bool which, when set to true or false, is really just a macro for an integer constant. If it is an integer, why is it not the same size?
I'm asking because it took us far too long to debug a program for which we did not allocate enough memory.
Surprisingly, g(bool) generates more asm instructions! Does it mean that if(bool) is little slower than if(int) ? I used to think bool is especially designed to be used in conditional statement such as if , so I was expecting g(bool) to generate less asm instructions, thereby making g(bool) more efficient and fast.
It is something C programmer use, but C++ programmers should avoid, since C++ has bool . bool is a language integral type whose supported values are just true and false . When converted to int true becomes 1 and false becomes 0.
The simple answer: it depends. The more complex answer: it depends on how your language, compiler, and runtime deal with bools or bits, and the word length of your CPU registers (64, 32, even 16 or 8 still have some use in simpler computing devices).
The _Bool
type in C99 (typedef
'ed to bool
in stdbool.h
) doesn't have a standard defined size, but according to section 6.2.5 of the C99 Standard:
2 An object declared as type _Bool is large enough to store the values 0 and 1.
In C, the smallest addressable object (aside from bitfields) is the char
, which is at least 8-bits wide, and sizeof(char)
is always 1
.
_Bool
and bool
therefore have a sizeof
of at least 1
, and in most implementations that I've seen, sizeof(bool)
/ sizeof(_Bool)
is 1
.
If you take a look at GCC's stdbool.h
, you'll get this:
#define bool _Bool
#if __STDC_VERSION__ < 199901L && __GNUC__ < 3
typedef int _Bool;
#endif
#define false 0
#define true 1
So if using an older version of GCC and an old version of the C standard when compiling, you will use int
as a _Bool
type.
Of course, as an interesting thing, check this out:
#include <stdio.h>
#include <stdbool.h>
int main() {
printf("%zu\n", sizeof(_Bool));
printf("%zu\n", sizeof(true));
printf("%zu\n", sizeof(false));
}
Output:
λ > ./a.out
1
4
4
GCC 4.2.4, Clang 3.0, and GCC 4.7.0 all output the same. As trinithis points out, sizeof(true)
and sizeof(false)
produce larger sizes because they are taking the size of an int literal, which is at least sizeof(int)
.
C99 standard introduced the _Bool
type. _Bool
is guaranteed to be big enough to hold integer constants 0 or 1 , this does not neccessarily mean it is an int. The actual size is compiler dependent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With