Macros
Macros are text replacements created with #define that insert code into your program. Macros may take parameters like a function but the macro code and parameters are always inserted into code by text substitution.

Macros:

Simple Macros

Syntax

#define label text

Example

1 #define Fosc 4000000
2 #define Tcy (4 * (1/Fosc))
3 #define Setup InitSystem(Fosc, 250, 0x5A)

Argument Macros

Syntax

#define label(arg1,…, argn) code

Example

1 #define min(x, y) ((x)<(y)?(x):(y))
2 #define square(x) ((x)*(x))
3 #define swap(x, y) { x ^= y; y ^= x; x ^= y; }

Extreme care must be exercised when using macros. Consider the following use of the below macro:

1 #define square(a) ((a)*(a))
1 i = 5;
2 x = square(i++);

The result will be the following:

x = 30
i = 7

Wrong Answers!
x = square(i++);
expands to:
x = ((i++)*(i++));
So i gets incremented twice, not once at the end as expected.