Understanding Preprocessor Directives

Why is this code not compiling? If I understand correctly, this should compile. Where am I mistaken?

#define THREADMODEL ASC #if THREADMODEL==NOASC THIS BLOCK SHOULDN'T BE COMPILED #endif int main() { } 
+6
source share
1 answer

When the preprocessor interprets

 #if THREADMODEL==NOASC 

he will replace THREADMODEL with ASC :

 #if ASC==NOASC 

If you have #define d ASC and NOASC to have numerical values, the preprocessor will replace them with 0 values ​​(it takes any undefined characters and replaces them with 0):

 #if 0==0 

Then it evaluates to 1 , and so the preprocessor will evaluate the block.

To fix this, try specifying different ASC and NOASC numerical values, for example:

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

Hope this helps!

+13
source

Source: https://habr.com/ru/post/951751/


All Articles