C Preprocessor
All preprocessor commands begin with a hash mask (#) in column one and terminate at the end-of-line. A line may be continued by putting a backslash () at the end.
define statement
#define name substitute-text
In most case, a const statement is preferred over #define.
The #define directive can only define simple constants. The const statement can define almost any type of C constant, including things like structure classes.
The #define directive is, however, essential for things like conditional compilation and other specialized uses.
Conditional Compilation
#define DEBUG
#ifdef DEBUG
printf("%d", some_value);
#endif
#undef DEBUG
#ifndef DEBUG
printf("production code.");
#endif
#ifdef DEBUG
printf("Test version. Debugging is on\n");
#else DEBUG
printf("Production version\n");
#endif /* DEBUG */
The compiler switch -Dsymbol allows symbols to be defined on the command line.
cc -DDEBUG -g -o prog prog.c
cc -DMAX=10 -o prog prog.c
include files
The #include directive allows the program to use source code from another file.
Parameterized Macros
#define SQR(x) ((x) * (x))
No spaces must exist between the macro name (SQR) and the parenthesis. And always put parentheses, ( ), around the parameters of a macro.
the #if directive for conditional compilations
the #pragma directive for inserting compiler-dependent commands into a file.