...
Sign in
Computer Science

Macros

Macros

Share:

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.


CheckpointTest Yourself

In compiler design and programming language semantics, what is Macro Hygiene, and what defect does it prevent?

Lesson Milestone

Finished Reading This Lesson?

Mark this lesson complete. A chapter counts as done once all its lessons are, and that adds to your course progress.

TH

Written by

ThinkWisely Team

Have a Question or Suggestion?

We'd love to hear from you. Drop your thoughts below to help us improve ThinkWisely!