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.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Haris
  • 12,120
  • 6
  • 43
  • 70
  • 7
    It's there. Look up `` – P.P Sep 22 '14 at 13:02
  • Related: http://stackoverflow.com/questions/1608318/is-bool-a-native-c-type/1608350#1608350 – P.P Sep 22 '14 at 13:04
  • i looked inside `` 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 – Haris Sep 22 '14 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: http://stackoverflow.com/a/10630231/297696. Older implementations may keep `sizeof(bool) == sizeof(int)` (or whatever) for compatibility reasons. – wkl Sep 22 '14 at 13:07
  • Just as a note, how is a boolean type really useful? Boolean variables (or functions returning them) often have quite descriptive names: `isValid`, `hasAttribute`, `needsFree`, ... And (as the others said, one bit isn't addressable in C) there wouldn't be any performance gain justifying such thing. C was designed very minimal (today less so, but still). – mafso Sep 22 '14 at 17:29
  • 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. – M.M Sep 23 '14 at 21:42
  • @ralph: `` doesn't declare any functions; what "return type" are you referring to? Neither the `` on my system nor the description in the C standard refers to `int`. All it does is define a few macros. `_Bool` is the built-in Boolean type (the name was chosen to avoid conflicting with existing code). – Keith Thompson Sep 24 '14 at 14:59
  • You're conflating a logical Boolean type (which has values `false` and `true`) with a *bit* type. The built-in type `_Bool` can hold a *logical* true or false value, but an object of type `_Bool` occupies at least one byte. – Keith Thompson Sep 24 '14 at 15:02
  • @KeithThompson, i was confused about the return type being integer in `stdbool.h`. now its clarified that newer version of gcc has _Bool of size 1 byte. – Haris Sep 24 '14 at 15:45
  • @ralph: What "return type" are you referring to? Functions have return types. `` declares no functions. The same C standard that introduced `` also introduced the built-in type `_Bool`; the macro `bool` in `` has *always* expanded to `_Bool`. (Incidentally, `_Bool` is an integer type. Don't confuse `int`, which is one specific integer type, with "integer", which describes a collection of types including `_Bool`, `char`, `int`, and a number of others. – Keith Thompson Sep 24 '14 at 16:04
  • @KeithThompson: oook. i was so confused when i saw in `http://pubs.opengroup.org/onlinepubs/009695399/basedefs/stdbool.h.html` that _Bool variables true/flase expand to integer constant 1/0. i thought then it should be of 4 bytes. now i understand.. :) – Haris Sep 24 '14 at 16:09
  • `false` and `true` are macros, not variables. They do expand to the constants `0` and `1`, respectively, and those constants are of type `int` (as I could have explained before if you hadn't talked about "return types"). But expressions of type `int` can be implicitly *converted* to `_Bool`, so the fact that `false` and `true` expand to expressions of type `int` is not a problem. Similarly, character constants and enumeration constants are also of type `int`. (An aside: C++ has different rules.) – Keith Thompson Sep 24 '14 at 16:19
  • @KeithThompson: now i understood everything clearly. thanx.. – Haris Sep 24 '14 at 16:22

3 Answers3

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;
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 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. – Haris Sep 22 '14 at 13:15
  • thanx.. i got it now.. :) – Haris Sep 22 '14 at 13:25
  • 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 – phuclv Sep 22 '14 at 15:49
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.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • 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. – Haris Sep 22 '14 at 13:15
-1

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

Varun
  • 16
  • 2