1.7.2 #define, #undef, #ifdef, #ifndef
The preprocessing directives #define and #undef allow the definition of identifiers which hold a certain value. These identifiers can simply be constants or a macro function. The directives #ifdef and #ifndef allow conditional compiling of certain lines of code based on whether or not an identifier has been defined.
Syntax:
#defineidentifier replacement-code
#undefidentifier
#ifdefidentifier
#elseor#elif
#endif
#ifndefidentifier
#elseor#elif
#endif
#ifdefidentifier is the same is#if defined(identifier).
#ifndefidentifier is the same as#if !defined(identifier).
An identifier defined with#defineis available anywhere in the source code until a#undefis reached.
A function macro can be defined with#definein the following manner:
#defineidentifier(parameter-list) (replacement-text)The values in the parameter-list are replaced in the replacement-text.
Examples:
#define PI 3.141
printf("%f",PI);
#define DEBUG
#ifdef DEBUG
printf("This is a debug message.");
#endif
#define QUICK(x) printf("%s\n",x);
QUICK("Hi!")
#define ADD(x, y) x + y
z=3 * ADD(5,6)This evaluates to 21 due to the fact that multiplication takes precedence over addition.
#define ADD(x,y) (x + y) z=3 * ADD(5,6)
This evaluates to 33 due to the fact that the summation is encapsulated in parenthesis which takes precedence over multiplication.