c - Preprocessor directives define and ifdef do not work as I imagined? -


i have 3 files:

main.c

#include <stdio.h> #include <stdlib.h> #include "test.h"  #define debug  int main() {   testfunction();     return 0; } 

test.h

#ifndef test_h #define test_h #include <stdio.h> #include <stdlib.h>  void testfunction();  #endif // test_h_included 

test.c

#include "test.h"  void testfunction(){   #ifdef debug     printf("i'm inside testfunction\n");   #endif } 

the question: why program not print stuff in #ifdef debug block? if write #define debug in test.h or test.c fine. what's problem #define debug in main.c? thanks.

preprocessor directives define , ifdef not work imagined?

no, not quite. seem believe preprocessor directives traverse file boundaries, don't. the scope of #defined preprocessor macro single file it's defined in, or other files if other files #include file containing macro definition.

perhaps imagine run compiler (and preprocessor) on each file separately (which do, if don't realize it). there's no way preprocessor tell debug has been defined in file doesn't operate on.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -