48

I was just writing code in C and it turns out it doesn't have a boolean/bool datatype. Is there any C library which I can include to give me the ability to return a boolean/bool datatype?

Community
  • 1
  • 1
itsaboutcode
  • 24,525
  • 45
  • 110
  • 156
  • 4
    Usually a plain old 'int' is used, with the assumption that 0 is 'false' and anything else is 'true'. – Rooke Nov 11 '10 at 22:09
  • Possible duplicate of [Is bool a native C type?](https://stackoverflow.com/questions/1608318/is-bool-a-native-c-type) – Jim Fell Jun 07 '17 at 14:45

7 Answers7

75

If you have a compiler that supports C99 you can

#include <stdbool.h>

Otherwise, you can define your own if you'd like. Depending on how you want to use it (and whether you want to be able to compile your code as C++), your implementation could be as simple as:

#define bool int
#define true 1
#define false 0

In my opinion, though, you may as well just use int and use zero to mean false and nonzero to mean true. That's how it's usually done in C.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • @James, it worked. But what i should return? i mean 0 or false? – itsaboutcode Nov 11 '10 at 22:09
  • 1
    @ysap: I picked macros because it's closer to what C99 does (In C99, `true` and `false` are both macros that are replaced by `1` and `0`, respectively, and `bool` is a macro that expands to the boolean type, `_Bool`. – James McNellis Nov 11 '10 at 22:09
  • 3
    @its: If you define macros for `bool`, `true`, and `false`, then make your return type `bool` and return `false`. Otherwise, just make your return type `int` and return `0`. It's up to you waht you want to do. I just think the non-macro approach is better. – James McNellis Nov 11 '10 at 22:11
  • @James - sorry, I messed up with this comment, erasing it while editing, so I reposted it as an answer. You were too fast to reply... – ysap Nov 11 '10 at 22:11
  • 1
    I actually prefer in C to use 0 as false and non-zero as true w/o a specific type. In other cases where the call might fail, 0 is success and NZ is an error code (usually negative codes). – caveman Nov 11 '10 at 22:19
18

C99 has a boolean datatype, actually, but if you must use older versions, just define a type:

typedef enum {false=0, true=1} bool;
caveman
  • 1,755
  • 1
  • 14
  • 19
  • 8
    If you ask me, "emulating" `bool` pre-C99 is dangerous because the semantics differ. `(bool)2` yields 2, not 1. A more realistic example: `1U<<(bool)isdigit(c)` will give the wrong result on most implementations. – R.. GitHub STOP HELPING ICE Nov 11 '10 at 23:58
5

C99 has a bool type. To use it,

#include <stdbool.h>
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
kavya
  • 69
  • 1
  • 1
1

As an alternative to James McNellis answer, I always try to use enumeration for the bool type instead of macros: typedef enum bool {false=0; true=1;} bool;. It is safer b/c it lets the compiler do type checking and eliminates macro expansion races

ysap
  • 7,723
  • 7
  • 59
  • 122
  • This doesn't prevent you from saying `bool b = 1;` – James McNellis Nov 11 '10 at 22:12
  • To the best of my knowledge, there is no additional type checking over what you would get with a `#define bool int`. – James McNellis Nov 11 '10 at 22:15
  • Oh, I think I see what you mean. However, you are talking about the variable declaration, while I am relating to the actual usage of the true and false tokens. – ysap Nov 11 '10 at 22:18
  • Can you give an example of where additional type checking is done? – James McNellis Nov 11 '10 at 22:19
  • Well, its hard indeed, but just an example: `float f; f = true;` should raise a warning for the implicit (and supposedly incompatible) type cast. – ysap Nov 11 '10 at 22:23
  • @Paul - I'm curious - are you sure this is a problem? If my memory serves me right, then enums are treated as ints. Ain't so? If you are right then this is definitely a good counter-example. – ysap Nov 11 '10 at 22:25
  • @ysap: you're quick ! I deleted that comment almost immediately because I wasn't sure if I had remembered the problem exactly - I've just been trying to recreate an issue I know I had a while back with enums and ++ but it's still eluding me. – Paul R Nov 11 '10 at 22:32
0

C99 introduced _Bool as intrinsic pure boolean type. No #includes needed:

int main(void)
{
  _Bool b = 1;
  b = 0;
}

On a true C99 (or higher) compliant C compiler the above code should compile perfectly fine.

alk
  • 69,737
  • 10
  • 105
  • 255
0

We can use enum type for this.We don't require a library. For example

           enum {false,true};

the value for false will be 0 and the value for true will be 1.

-3
struct Bool {
    int true;
    int false;
}

int main() {

    /* bool is a variable of data type – bool*/
    struct Bool bool;

    /*below I’m accessing struct members through variable –bool*/ 
    bool = {1,0};
    print("Student Name is: %s", bool.true);
    return 0;
}
Smittey
  • 2,475
  • 10
  • 28
  • 35
M.zar
  • 9
  • 2