C ++ using conditional directive #if TRUE

When using type expressions #if TRUE, what should I expect? The explanation will be very appreciated! I understand how it works #if 1, but it gives a TOTALLY different result in my code than when using it #if TRUE. I understand that #ifis a conditional directive, and what that means; it is just a part TRUE(or FALSE) that I do not understand. It seems that using this method in this way never executes the code following the statement. Here is an example:

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

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

I never see the screen displaying “true” or “false,” and with Visual Studio, the internal statement is automatically grayed out.

+5
source share
3 answers

The preprocessor will include / exclude the contents of the block #if #endifdepending on whether the expression after #ifis true or false.

#if TRUE will evaluate only true if

  • defined macro TRUE
  • value TRUE! = 0

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

+5
source

TRUEand FALSEare macros on Windows, but declared on WinDef.h.

If you include the title, you will see that TRUE- 1and FALSE- 0. So, the first statement must execute, the second should not.

If you do not include the header, both will be undefined and none of the statements will be executed.

+4

true false, :

#define TRUE 1
#define FALSE 1

#if TRUE #if FALSE , #if 1.

0
source

All Articles