2

I was reading comp.lang.cs description of booleans values, pre-C99. It mentions that some people prefer to define their own boolean values as:

#define TRUE (1==1)
#define FALSE (!TRUE)

However, the standard defines the equality operator to always return a signed int with a value of 1 when two values compare equal (C11 - 6.5.9) and the logical not operator shall return a int with a value of 0 if the value compares unequal to 0 (C11 - 6.5.3.3).

If this is the case and the above definitions use literals, won't the evaluation happen compile time and the resulting definitions be:

#define TRUE (1)
#define FALSE (0)

And a follow-up question. Is there any case where it makes sense to define the true and false labels to anything other than 1 and 0, respectively?

And pardon that I reference C11 when my question concerns C89 but I only have the C11 standard at hand.

5
  • TRUE=1 and FALSE=0 are bound to standard as you have noted. TRUE=(1==1) and FALSE=(!TRUE) are bound to compiler-specific implementation. So if you get a non-standard-compliant compiler (which is bad), you can still have somewhat portable code. Feb 18, 2015 at 9:27
  • 2
    Note that 1 == 1 is 1 and !(1 == 1) is zero – since they are constant expressions. One case where it would make sense is e. g. when you want your compiler not to accept FALSE as a null pointer literal. Feb 18, 2015 at 9:31
  • 2
    It is also useful if done in a header file that may be included by a C++ program. Then TRUE will be the proper bool typed true.
    – rodrigo
    Feb 18, 2015 at 9:34
  • Related (with relevant discussion): stackoverflow.com/q/14642096/694733
    – user694733
    Feb 18, 2015 at 9:40
  • @TheParamagneticCroissant: !(1==1) is a valid null pointer constant. (E.g. (0,0) or (0 , !(1==1)) etc aren't, and would make the compiler complain if used in a pointer context.)
    – mafso
    Feb 18, 2015 at 15:34

1 Answer 1

3

(1==1) and (!TRUE) are useful definitions on some compilers (I don't have a concrete example off of the top of my head) that track whether an integer came from a boolean comparison. This enables them to warn for

if (i)

while at the same time not warning for

if (i != 0)

and also not warning for

j = i != 0;
if (j)

even though in all three cases, the conditional is a non-constant int.

This way, no warning would be generated for int b = TRUE;...if (b), since b would be considered a truth-integer.

You can make a legitimate argument that such warnings are useless, but others can make an equally legitimate argument that such warnings do have use. It will have many false positives in common code, but it may make code more readable if it is written in a way that avoids such warnings.

At the same time, such definitions are harmless for other compilers that do not track this, since they just see constant expressions that evaluate to 1 and 0.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.