Tom
Tom

Reputation: 6981

Using true and false in C

As far as I can see, there are three ways to use Booleans in C:

  1. with the bool type, from <stdbool.h> then using true and false
  2. defining using preprocessor #define FALSE 0 ... #define TRUE !(FALSE)
  3. 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), and 1 is most readable not compatible with all compilers.

Upvotes: 96

Views: 311100

Answers (16)

jla
jla

Reputation: 10174

Adopt a coding style consistent with your current project; if this is not applicable, default to the style used in a project you highly respect or admire:

Linux kernel coding style

  1. Using bool

The Linux kernel bool type is an alias for the C99 _Bool type.

Upvotes: 0

Nick Van Brunt
Nick Van Brunt

Reputation: 15494

You can test if bool is defined in C99's stdbool.h with

#ifndef __bool_true_false_are_defined || __bool_true_false_are_defined == 0
//typedef or define here
#endif

Upvotes: 4

Lothar
Lothar

Reputation: 13092

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). That’s the C way to do things.

Upvotes: 2

Bob Wakefield
Bob Wakefield

Reputation: 834

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.

Upvotes: 6

debuti
debuti

Reputation: 683

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

Upvotes: 0

Maurizio Reginelli
Maurizio Reginelli

Reputation: 3222

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)
{
   ...
}

Upvotes: 0

Clifford
Clifford

Reputation: 93576

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++.

Upvotes: 1

C. K. Young
C. K. Young

Reputation: 223193

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.

Upvotes: 147

T.E.D.
T.E.D.

Reputation: 44824

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.

Upvotes: 1

Will Marcouiller
Will Marcouiller

Reputation: 24152

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.

Upvotes: 2

Marco Demaio
Marco Demaio

Reputation: 34437

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.

Upvotes: 2

wallyk
wallyk

Reputation: 57804

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.

Upvotes: 4

Macarse
Macarse

Reputation: 93183

I usually do a:

typedef enum {FALSE = 0, TRUE} boolean;

Upvotes: 22

b.roth
b.roth

Reputation: 9552

Just use 0 or 1 directly in the code.

For C programmers, this is as intuitive as true or false.

Upvotes: 23

Doug T.
Doug T.

Reputation: 65649

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.

Upvotes: 5

anthares
anthares

Reputation: 11223

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.

Upvotes: 2

Related Questions