Macros are used by the compiler to perform a simple search and replace for two specified values.
When the program code is actually compiled, the compiler will replace all of the specified source
strings with the specified target strings. Macros are defined using the
#define directive,
and take the following form:
For example, macros can be used to centralize certain variables that do not need the
error-checking of ordinary variables, such as static array sizes or other numerical constants.
The following are examples:
In the above examples, whenever the compiler encounters the source tag, say, ARRAY_LENGTH, it
replaces it with the target value - in this case, 100. This transforms the typical declaration
char myArray[ARRAY_LENGTH] into
char myArray[100]. These macros can be used anywhere
in a program, such as in loops, dclarations, or anywhere else, and can even be used to define
which function call should be executed at a particular location. In essence, macros allow the
centralized location of information that varies in a program, but does not require error-checking.