1

I would like to use bool in c language

first I see in C++

int maximum(int state, **bool choose**);

then

new_state = maximum(now_State, **true**);

I use boolean in C language by this:

#define true 1 
#define false 0 

typedef int boolean; 
boolean choose = false ;

but I dont know how to set on function like this(C++).

int maximum(int state, **bool choose**);

new_state = maximum(now_State, **true**);

Im foreigner. If you dont understand please tell me.

4
  • Both C++ and C have a built-in bool type. For C it's called _Bool but if you #include <stdbool.h> then you can use bool. Commented Dec 17, 2013 at 10:55
  • "but I dont know how to set on function like this(C++)." - I don't understand this. What do you mean by "set on function"? Commented Dec 17, 2013 at 11:00
  • for example int maximum(int state, bool choose); bool dont have in C language. So, int maximum(int state, xxxxxx); How I change. Commented Dec 17, 2013 at 11:04
  • @SupaphonKamon There is a bool type in C. Commented Dec 17, 2013 at 11:14

2 Answers 2

6

To use Boolean type C99 provides <stdbool.h> header. This header provides a macro bool. This header also contains the header true and false which stand for 1 and 0 respectively.

Sign up to request clarification or add additional context in comments.

7 Comments

More to the point it is a proper boolean type. So something along the lines of bool k = 2; if(k == true) will work as expected
@doynax: well, true is a macro that expands to 1, and false expands to 0, so defining typedef int bool and #define true 1 isn't that different
@EliasVanOotegem Except that the code which donyax shows wouldn't work with typedef int bool.
@SupaphonKamon: That's because microsoft didn't support C99. That should've changed with VS2013, though
It is best to always test booleans as if(k) or if(!k) anyway, as it makes porting easier.
|
2

I would recommend to use enum:

#include <stdio.h>

typedef enum { false, true } bool;

int main() {
    bool x = false;
    if(x != true)
        printf("x=%d\n",x);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.