Macros
Introduction / Definition
Macros provide source-level code expansion or transformation. They can remove repetitive code or introduce specialized syntax. Macro systems differ across languages. Because expansion may happen before ordinary execution, macros can introduce subtle evaluation and debugging issues.
Syntax / Structure / Formula
macro(arguments) โ expanded/transformed code
Detailed Explanation
A textual macro substitutes code before compilation in C-like preprocessors. Safer abstractions such as functions or generics are often preferable when they express the same idea.
Example
Example: #define SQUARE(x) ((x)\*(x)).
Output / Result
SQUARE(5) expands to ((5)*(5)) and evaluates to 25.
Important Points / Notes
- Macro behavior is language dependent.
- Textual macros can have evaluation pitfalls.
Mini Practice Question
Give one risk of a textual macro.