prev

More Preprocessor Directives

next

In addition to define and include there are a handful of other directives that may come in handy. The most useful is probably the conditionals.
      #ifdef SOMETHING
      #else
      #endif

      #ifndef SOMETHING
      #endif
    
The value in the conditional is either set up using a define statement or can be set during compilation. Unfortunately conditionals like this tend to require a recompilation to change the value.

Example:
         
     #include <stdio.h>
     #include <stdlib.h>
     
     int main ( void ) {
       int i = 5;
     
       int j = i + 5;
     
     /* The following will only execute if this program is compiled to set DEBUG
        to something non-zero or if DEBUG is defined in a header */
     
     #ifdef DEBUG
        printf ( "i = %d\n", i );
     #endif
     
        printf ( "j = %d\n", j );
        exit ( 0 );
     }

    

If you want to set the DEBUG flag at compile time then you can do something like this
    cc -Wall -DDEBUG -o test test.c