Wednesday, May 11, 2011

c preprocessor example

A simple c preprocessor example. Here #define, #undef, #ifdef, #ifndef, #else, #endif directives are explained.


[c preprocessor example]

#define DEBUG // ON
#undef DEBUG // OFF

#ifdef DEBUG    // Enable the block if DEBUG symbol is defined.   
puts("haha");
#endif

#ifndef DEBUG // Enable the block if DEBUG symbol is not defined.
puts("haha");
#endif

#ifdef DEBUG // Run puts("haha") if DEBUG symbol is defined.
puts("haha");
#else DEBUG // Otherwise, run puts("hoho")
puts("hoho");
#endif