tags:

views:

50

answers:

4

Hello,

gcc 4.4.4 c89

I have seen this in some code I am maintaining.

#define FALSE 0
#define TRUE (!FALSE)

is there any difference in writing the following:

#define FALSE 0
#define TRUE 1

Its been used in some functions like this:

if(condition failed) {
    return FALSE;
}
else {
    return TRUE;
}

Many thanks for any suggestions,

+1  A: 

The C "if" statement interprets zero as false,and absolutely anything else at all as true. So it's not really important what value you use for TRUE, though 1 would be conventional.

But (!0) == 1 anyway so this should not change anything.

>> cat joe.c
#include <stdlib.h>

#define FALSE 0
#define TRUE (!FALSE)

int main (int argc, char * argv[]) {
  printf("false %d\ntrue %d\n", FALSE, TRUE);
  return 0;
}
>> cc joe.c
>> ./a.out
false 0
true 1
>>
joefis
But (!0) == 1 anywayI think you may find that is implementation-dependent. But it doesn't matter much unless you have a condition likeif (bool_value == TRUE){ ...}which you didn't ought, really.
Brian Hooper
! is a boolean operator in C and returns 1 if the argument you gave it is zero and returns 0 otherwise. People sometimes think ! is a "swap the bits" operator, but it's not. But your point is well made. When writing C you can rely on "false" being zero but it's a mistake to rely on "true" having any specific value, you can only be sure that it's not zero :)
joefis
+1  A: 

One difference is, in the (!FALSE) case, if you change:

#define FALSE 0

to

#define FALSE 1

Your program still "works", without modifying TRUE... however, it's unlikely that would be safe anyways, because I'm sure your program relies on constructs such as

if (funtion_that_returns_FALSE()) {
}

Which would break if you changed the definition of FALSE. Don't change it :)

Stephen
+1  A: 

Edit: I read the question backwards. Sorry. Since TRUE is !FALSE and FALSE is 0, then TRUE is 1.

The compiler will evaluate FALSE to 0 anyhow, since it will see (!1) which doesn't make sense to compute at run time.

It's just silly.

Adam Shiemke
in c, anything not zero is true. That's exactly what the code represents. I don't think it's silly, in fact it might even be more sane than arbitrarily choosing a true value.
Stephen
@Stephen: 1 is no arbitrary value. A boolean evaluation in C returns either 0 for false or 1 for true. Thus defining TRUE with 1 makes perfect sense. Using !0 (which is 1) is a bit more explicit about the facts, of course, but if you need explicitness here to be reminded about these basic facts, then you live a dangerous life as a C programmer, anyway. ;)
Secure
@Secure : I certainly agree that depending on these details is walking a dangerous line :) Perhaps arbitrary was a poor choice of words on my part. My point was just that `if (c)` only evaluates the `else` branch if `c` evaluates to zero. So, it's only important that `TRUE` be represented by something non zero. Yay for the development of the `bool` type! :)
Stephen
A: 

The best way to do this is:

#ifndef FALSE
#define FALSE 0
#elif FALSE != 0
#undef FALSE
#define FALSE REWRITE_PROGRAM("FALSE")
#endif

#ifndef TRUE
#define TRUE 1
#elif TRUE != 1
#undef TRUE
#define TRUE REWRITE_PROGRAM("TRUE")
#endif

Where REWRITE_PROGRAM is a macro which calls a function (providing the line number and source file in advance) that, using advanced neural networks, will intelligently rewrite your code so as to avoid conflicting definitions of TRUE or FALSE where TRUE is defined to be anything other than 1 and FALSE to be anything other than 0. After it generates the corrected code and builds it, it will execute the newly built version of the application and exit.

As a backup, the rewrite AI will also make sure that its new assumptions are correct by adding more code with REWRITE_PROGRAM should it ever encounter a case where its assumptions are incorrect.

Ultimately the program will rewrite itself repeatedly until it is correct, and this way you don't have to worry about how TRUE and FALSE are defined, conflicts, and whether those definitions will change in the future.