Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 247
Hieli Avatar asked Jun 13 '12 13:06

Hieli


2 Answers

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.

like image 160
josefx Avatar answered Oct 02 '22 19:10

josefx


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.

like image 27
Luchian Grigore Avatar answered Oct 02 '22 20:10

Luchian Grigore