As far as I can see there are 3 ways to use booleans in c
- with the bool type, from then using true and false
- defining using preprocessor
#define FALSE 0 ... #开发者_运维问答define TRUE !(FALSE)
- Just to use constants directly, i.e. 1 and 0
are there other methods I missed? What are the pros and cons of the different methods?
I suppose the fastest would be number 3, 2 is more easily readable still (although bitwise negation will slightly add to overhead), 1 is most readable not compatible with all compilers.
Just include <stdbool.h>
if your system provides it. That defines a number of macros, including bool
, false
, and true
(defined to _Bool
, 0, and 1 respectively). See section 7.16 of C99 for more details.
Just use 0 or 1 directly in the code.
For C programmers, this is as intuitive as true or false.
I usually do a:
typedef enum {FALSE = 0, TRUE} boolean;
With the stdbool.h defined bool type, problems arise when you need to move code from a newer compiler that supports the bool type to an older compiler. This could happen in an embedded programming environment when you move to a new architecture with a C compiler based on an older version of the spec.
In summation, I would stick with the macros when portability matters. Otherwise, do what others recommend and use the bulit in type.
Whichever of the three you go with, compare your variables against FALSE, or false.
Historically it is a bad idea to compare anything to true (1) in c or c++. Only false is guaranteed to be zero (0). True is any other value. Many compiler vendors have these definitions somewhere in their headers.
#define TRUE 1
#define FALSE 0
This has led too many people down the garden path.
Many library functions besides chartype
return nonzero values not equal to 1
on success. There is a great deal of legacy code out there with the same behavior.
Any int other than zero is true; false is zero. That way code like this continues to work as expected:
int done = 0; // `int` could be `bool` just as well
while (!done)
{
// ...
done = OS_SUCCESS_CODE == some_system_call ();
}
IMO, bool
is an overrated type, perhaps a carry over from other languages. int
works just fine as a boolean type.
You can test if bool is defined in c99 stdbool.h with
#ifndef __bool_true_false_are_defined || __bool_true_false_are_defined == 0
//typedef or define here
#endif
I would go for 1. I haven't met incompatibility with it and is more natural. But, I think that it is a part of C++ not C standard. I think that with dirty hacking with defines or your third option - won't gain any performance, but only pain maintaining the code.
I prefer (1) when i define a variable but in expressions i never compare against true and false just take the implicit C definition of if(flag) or if(!flag) or if(ptr). Thats the C way to do things.
I don't know you specific situation. Back when I was writing C programs, we have always used #2.
#define FALSE = 0
#define TRUE = !FALSE
This might be otherwise under alien platform to DOS or Intel-based processors. But I used to use both C and ASM together writing graphic libraries and graphical IDE. I was a true fan of Micheal Abrash and was intending to learn about texture mapping and so. Anyway! That's not the subject of the question here!
This was the most commonly used form to define boolean values in C, as this headerfile stdbool.h did not exist then.
I used to use the #define because they make code easier to read, and there should be no performances degradation compared to using numbers (0,1) coz' the preprocessor converts the #define into numbers before compilation. Once the application is run preprocessor does not come into the way again because the code is already compiled.
BTW it should be:
#define FALSE 0
#define TRUE 1
and remember that -1, -2, ... 2, 3, etc. all evaluates to true.
There is no real speed difference. They are really all the same to the compiler. The difference is with the human beings trying to use and read your code.
For me that makes bool, true, and false the best choice in C++ code. In C code, there are some compilers around that don't support bool (I often have to work with old systems), so I might go with the defines in some circumstances.
1 is most readable not compatible with all compilers.
No ISO C compiler has a built in type called bool
. ISO C99 compilers have a type _Bool
, and a header which typedef's bool
. So compatability is simply a case of providing your own header if the compiler is not C99 compliant (VC++ for example).
Of course a simpler approach is to compile your C code as C++.
I prefer the third solution, i.e. using 1 and 0, because it is particularly useful when you have to test if a condition is true or false: you can simply use a variable for the if argument.
If you use other methods, I think that, to be consistent with the rest of the code, I should use a test like this:
if (variable == TRUE)
{
...
}
instead of:
if (variable)
{
...
}
I prefer to use
#define FALSE (0!=0)
#define TRUE (0==0)
or directly in the code
if (flag == (0==0)) { ... }
The compiler will take care of that. I use a lot of languages and having to remember that FALSE is 0 bothers me a lot; but if I have to, I usually think about that string loop
do { ... } while (*ptr);
and that leads me to see that FALSE is 0
精彩评论