10

One of my friends asked a question, why is there no Boolean data type in the C programming language. I did a bit of searching and reading. I got few questions and answers in stack overflow saying that,

  1. All data types should be addressable, and a bit cannot be addressed.
  2. The basic data structure at the hardware level of mainstream CPUs is a byte. Operating on bits in these CPUs require additional processing.

We can use a bool in this manner

#define bool int #define TRUE 1 #define FALSE 0 

or use typedefs.

But my question is this: why wasn't it implemented as a data type in C, even after so many years. doesn't it make sense to implement a one byte data type to store a boolean value rather than using int or short explicitly.

13
  • 7
    It's there. Look up <stdbool.h> Commented Sep 22, 2014 at 13:02
  • Related: stackoverflow.com/questions/1608318/is-bool-a-native-c-type/… Commented Sep 22, 2014 at 13:04
  • i looked inside <stdbool.h> but that uses integer as a return type. why integer, wont 4 bytes be redundant (considering sizeof(int) = 4), if we can do that using just 1 byte Commented Sep 22, 2014 at 13:04
  • 7
    @ralph - sizeof(bool) depends on the implementation, but if you're in C99 mode, sizeof(bool) is typically equal to 1. I answered a similar question long ago: stackoverflow.com/a/10630231/297696. Older implementations may keep sizeof(bool) == sizeof(int) (or whatever) for compatibility reasons. Commented Sep 22, 2014 at 13:07
  • 2
    @mafso example - bool b = (x & 0x8000); . If bool is an alias for char then this will set it to false even if the intended bit is set. This problem is more insidious for a function that takes bool as parameter, or returns bool. Commented Sep 23, 2014 at 21:42

3 Answers 3

20

That's not true any more. The built-in boolean type, aka _Bool is available since C99. If you include stdbool.h, its alias bool is also there for you.


_Bool is a true native type, not an alias of int. As for its size, the standard only specifies it's large enough to store 0 and 1. But in practice, most compilers do make its size 1:

For example, this code snippet on ideone outputs 1:

#include <stdio.h> #include <stdbool.h> int main(void) { bool b = true; printf("size of b: %zu\n", sizeof(b)); return 0; } 
Sign up to request clarification or add additional context in comments.

3 Comments

i got this. but again the thing that comes in my mind is why will it return integer. the data type _Bool could have had its own native type which would take up only 1 Byte.
thanx.. i got it now.. :)
because the smallest addressable memory element size is 1 byte, so they can't make it 1 bit or less than 1 byte, unless its address aren't gonna be taken
5

C99 added support for boolean type _Bool, is not simply a typedef and does not have to be the same size as int, from the draft C99 standard section 6.2.5 Types:

An object declared as type _Bool is large enough to store the values 0 and 1.

We have convenience macros through the stdbool.h header. we can see this from going to the draft C99 standard section 7.16 Boolean type and values whcih says:

The header defines four macros.

The macro

bool

expands to _Bool.

The remaining three macros are suitable for use in #if preprocessing directives. They are

true

which expands to the integer constant 1,

false

which expands to the integer constant 0, and

__bool_true_false_are_defined

which expands to the integer constant 1.

1 Comment

i got this. but again the thing that comes in my mind is why will it return integer. the data type _Bool could have had its own native type which would take up only 1 Byte.
-1

Since the data types are predefined ,we cannot use the "bool" data type as it is not present in the documentation

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.