/* ** $Id: macros.c 802 2008-03-01 19:26:43Z phf $ ** ** Some examples of preprocessor macros. Macros can ** be a pain to use correctly, and while they "look ** like" functions they don't behave anything like ** functions. Unless you desperately need them, I'd ** stay away. But you must be able to *read* macros ** in other people's code of course... :-/ */ #include #include #define ONE 1 #define TWO 2 #define PLUSONE(x) (x+1) #define MAX(x,y) (x>y ? x : y) // often it is a good idea to surround each // parameter with parenthesis to make sure // nothing goes wrong... #define BMAX(x,y) ((x)>(y) ? (x) : (y)) #define VOODOO(x) (x++) // unless NDEBUG is set, print debugging messages // (NDEBUG is also used by assert.h and assert()) #ifndef NDEBUG #define DEBUG(x) printf("DEBUG: %s\n", x); #else #define DEBUG(x) #endif int main( void ) { int a = ONE; int b = PLUSONE(a); printf("a=%d, b=%d\n", a, b); printf("%d\n", a (a++