Hieli
Hieli

Reputation: 73

C++, using #if TRUE conditional directive

When using a statement like #if TRUE, what should I expect to happen? An explanation would be very appreciated! I understand how #if 1 works, but it gives a completely different result in my code than using #if TRUE. I understand #if is a conditional directive, and what that implies; it's just the TRUE (or FALSE) part that I don't understand. It seems as though using it in this way never executes code following the statement. Here is an example:

#if TRUE
     cout << "true" << endl;
#endif

#if FALSE
     cout << "false" << endl;
#endif

I never seem to see "true" or "false" printed to screen and using Visual Studio, the inner statement is automatically grayed out.

Upvotes: 7

Views: 10082

Answers (4)

Yun
Yun

Reputation: 3812

TL;DR: TRUE and FALSE are not defined by default, and any non-defined preprocessor token is replaced by 0.

Full explanation

The C++17 standard 19.1.7 states this about preprocessing directives in the if-group:

Preprocessing directives of the forms

  • # if constant-expression new-line group-opt
  • # elif constant-expression new-line group-opt

check whether the controlling constant expression evaluates to nonzero.

(Emphasis added)

The standard 19.1.9 states this about undefined identifiers used in preprocessor directives:

After all replacements due to macro expansion and evaluations of defined-macro-expressions and has-include-expressions have been performed, all remaining identifiers and keywords, except for true and false, are replaced with the pp-number 0, and then each preprocessing token is converted into a token. [ Note: An alternative token (5.5) is not an identifier, even when its spelling consists entirely of letters and underscores. Therefore it is not subject to this replacement. — end note ]

(Emphasis added)

Thus, if TRUE or FALSE are not defined, these are replaced by 0 and the #if condition fails. The lowercase variants true and false are exceptions to this rule and can be used like the C++ language's keywords. To make the condition true, define the identifiers first, e.g.:

#define TRUE

or, if you want TRUE to be available as an integer value,

#define TRUE 1

This will work too, although it is not recommended:

#define TRUE 5
  • Note 1: that #define TRUE 0 is a definition of TRUE and thus makes the #if TRUE condition true.
  • Note 2: Some headers (e.g. WinDef.h apparently) include definitions for TRUE and FALSE.

Justification for late answer: This question is being referred to from later duplicate questions and would benefit from having a complete answer with references to the standard.

Upvotes: 0

Sani Huttunen
Sani Huttunen

Reputation: 24375

To see true or false printed you need to define them:

#define TRUE 1
#define FALSE 1

Now #if TRUE and #if FALSE will be the same as #if 1.

Upvotes: 1

josefx
josefx

Reputation: 15656

The preprocessor will include/exclude the contents of an #if #endif block depending on wether the expression after #if evaluates to true or false.

#if TRUE will only evaluate to true if

  • the macro TRUE is defined
  • the value of TRUE != 0

In your example neither TRUE nor FALSE are defined so both blocks are false and excluded.

Upvotes: 10

Luchian Grigore
Luchian Grigore

Reputation: 258548

TRUE and FALSE are macros in Windows, but declared in WinDef.h.

If you include the header, you'll see that TRUE is 1 and FALSE is 0. So, the first statement should execute, the second should not.

If you don't include the header, both will be undefined, and neither of the statements will be executed.

Upvotes: 5

Related Questions