A macro associates a name, and optionally parameters, with replacement text through a preprocessor #define. The preprocessor expands that text early in compilation, before ordinary compiler analysis. A definition contains #, define, an identifier, replacement text, and a terminating newline. section 5.6.3 section 17.1
Named constant
# define M_PI 3.141'592'653'589'793'238'46 Every later occurrence of M_PI is replaced by the specified double constant. Macro names conventionally appear in all capitals, making textual replacement visually distinct from ordinary identifiers. section 5.6.3
Use macros deliberately because replacement text can contain almost any C text:
- Reserve them for initialization constants or compiler magic when a macro is appropriate. section 10.2.1
- Keep macro control flow visible and unsurprising; hidden return statements and dangling-else interactions can mislead readers. section 10.2.1
- For function-like macros, match arguments to identifier parameters, expand arguments recursively, and substitute them into copied replacement text. section 17.1
- Parenthesize arguments and evaluate each argument only once when designing function-like macros. section 17.1
#define BYTECOPY(T, S) memcpy(&(T), &(S), MINSIZE(T, S))
This compact function-like macro shows parameters substituted into a memcpy-based replacement expression. section 17.1








