Preprocessor and Macros

suggest change

Introduction

All preprocessor directives begin with the hash (pound) symbol #. A C macro is a preprocessor directive defined with #define. During preprocessing, the preprocessor substitutes the macro’s replacement list wherever its name appears. Function-like macros also substitute arguments, and the # and ## operators stringify and paste tokens.

Remarks

Macro replacement does not respect C scope. A macro definition is not limited to a block, so it is unaffected by a } that ends a block statement.

The preprocessor is, by design, not Turing-complete. There are several kinds of computation that it cannot do alone.

Usually compilers have a command line flag (or configuration setting) that allows us to stop compilation after the preprocessing phase and to inspect the result. On POSIX platforms this flag is -E. So, running gcc with this flag prints the expanded source to stdout:

$ gcc -E cprog.c

Often the preprocessor is implemented as a separate program, which is invoked by the compiler. The common name for that program is cpp. A number of preprocessors emit supporting information, such as line numbers, which later phases use for diagnostics and debugging information. If the preprocessor is based on GCC, the -P option suppresses that information.

$ cpp -P cprog.c

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents