Ari M
Ari M

Reputation: 1426

Preprocessor on C

I added this in my code:

#ifdef DEBUG_MODE
    printf("i=%d\n",i);
    fflush(stdout);
#endif

and my question is, if I'm not in DEBUG_MODE what the compiler does when compiling this?

Upvotes: 0

Views: 156

Answers (3)

Tony Delroy
Tony Delroy

Reputation: 106096

#ifdef and #endif control conditional compilation. This happens during an initial pass over the program, making dumb textual substitutions before the compiler even begins to consider the file to contain C code specifically. In this case, without the symbol defined only whitespace is left. The text is never even lexed into C tokens if the preprocessor define tested for isn't defined at that point.

You can see this for yourself: just invoke your compiler with whatever flag it uses to stop after preprocessing - e.g. gcc -E x.cc - and at that point in the output there will just be an empty line or two. This is also a very important technique for understanding macros, and a good thing to do when you just can't guess why some program's not working the way you expect - the compiler says some class or function doesn't exist and you've included its header - look at the preprocessed output to know what your compiler is really dealing with.

Upvotes: 2

vpit3833
vpit3833

Reputation: 7951

if DEBUG_MODE is not defined, the code under it will not be compiled.

Upvotes: 1

Christian Severin
Christian Severin

Reputation: 1831

The compiler will do nothing, because there will be nothing there when DEBUG_MODE is not defined.

Upvotes: 3

Related Questions