Search code examples
c++macrosc-preprocessorconditional-compilationpreprocessor-directive

What does #ifdef 1 in C++


in C++, I know that programmers use #ifdef 0 to block out code from running, but in this same project I see a lot of #ifdef 1. Does this mean that the code always runs? Unfortunately the code does not compile so I can't just run and test!


Solution

  • #ifdef 1 is ill-formed. The #ifdef directive requires a single identifier; 1 is not an identifier.

    #ifdef x is equivalent to #if defined(x). The defined preprocessing operator yields true if the identifier names a defined macro (i.e., a macro that has been defined with #define and not yet undefined via #undef) and false otherwise.

    The #if directive enables or disables compilation of the lines between it and the corresponding #else, #elif, or #endif directive that follows it (the directives nest).

    Chances are, what you are really looking for is #if 1 (or #if 0), which is valid.