Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding preprocessor directives

Why this code isn't compiling? If I understand right this should compile. Where I'm wrong?

#define THREADMODEL ASC 

#if THREADMODEL==NOASC
THIS BLOCK SHOULDN'T BE COMPILED
#endif

int main() {
}
like image 557
Sukhanov Niсkolay Avatar asked Aug 14 '13 18:08

Sukhanov Niсkolay


People also ask

What are the different preprocessor directives?

There are 4 Main Types of Preprocessor Directives:Macros. File Inclusion. Conditional Compilation. Other directives.


1 Answers

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!

like image 119
templatetypedef Avatar answered Sep 18 '22 01:09

templatetypedef