Search code examples
c++c-preprocessorpreprocessor-directive

Comparison of preprocessor variables


What is the reason why this code does not compile?

#define THREADMODEL ASC 

#if THREADMODEL==NOASC
// This block should be ignored and the code should compile
#error
#endif

int main() {
}

Solution

  • When the preprocessor interprets

    #if THREADMODEL==NOASC
    

    it will replace THREADMODEL with ASC:

    #if ASC==NOASC
    

    Unless you have #defined ASC and NOASC to have numeric values, the preprocessor will replace them with 0 values (it takes any undefined symbols and replaces them with 0):

    #if 0==0
    

    This then evaluates to 1 and so the preprocessor will evaluate the block.

    To fix this, try giving different numeric values to ASC and NOASC, like this:

    #define ASC    0
    #define NOASC  (1 + (ASC))
    

    Hope this helps!