How to declare boolena in C?
I tired to do
bool a;
And it didn't work for me gave
'bool' undeclared (first use in this function) , while compiling.
How to declare boolena in C?
I tired to do
bool a;
And it didn't work for me gave
'bool' undeclared (first use in this function) , while compiling.
c does not have a Boolean data type
╔╗╔══╦╗
║║║╔╗║║
║╚╣╚╝║╚╗
╚═╩══╩═╝
Try this:
You use the char type instead of int to save memory.Code:#define TRUE 1 #define FALSE 0 char a; a = TRUE; if(a) a = FALSE;
Last edited by ITAmember; 05-31-2009 at 06:39 PM. Reason: char's a type not a value :P
Unless you learned to program after 1999, in which case C does have a boolean data type (called _Bool).
how to use _Bool?
I usually type '_', then 'B', then 'o', then another 'o', and then 'l'.
If you also #include <stdbool.h>, then you also get "true" and "false", as well as the privilege of saying "bool" instead of "_Bool".Code:_Bool a_boolean_variable;
thanx a lot
Not when you've got fewer than four of them in a struct or array you don't!
Best option is to use int unless you have an array of more than say 50 or so of them. Of course if you've got more than 100000 of them then it can even make sense to pack them into 1 bit each using bitwise operations. But for one boolean, ALWAYS just use an int in C.
Bottom line is, unless you're saving large amounts of memory by doing it, you're most likely slowing your program down unnecessarily. This kind of thing is seldom smaller AND faster.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Or include stdbool.h which defines bool, true and false. It's IMO better than _Bool, 1 and 0.
Ooops. Didn't see it while reading the other two lines he wrote.