Macros

The preprocessor allows you to define macros, which are used rather like functions, except that they directly insert text into the source that the compiler sees.

The #define Directive

The #define directive changes the meaning of a word, or token, so that it is replaced by substitute text. This token is called a C macro. In this example, I have created three macros, PI, IF, and THEN:

#define PI 3.1412
#define IF if(
#define THEN )
#define TWOPI 2*PI

After these definitions, you can type IF x > PI THEN, and the preprocessor replaces the macros with their substitute text; this is called macro expansion. The compiler then actually sees if (PI > 3.1312). If the expanded text itself contains a macro, that will be further expanded. The preprocessor ...

Get C++ By Example: UnderC Learning Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.