A macro is a named set of instructions that you define once and reuse. In Office apps such as Excel and Word, a macro is a recorded or written sequence of actions you can run on demand; in C and C++, a macro is a name the preprocessor replaces with other code before compiling.
The word covers several different things, so the meaning depends on where you met it. The two most common are below.
| Kind | Where | What it does | When it happens |
|---|---|---|---|
| Office macro | Excel, Word, PowerPoint, Access and others | Repeats a set of actions you recorded or wrote | When you run it |
| Preprocessor macro | C and C++ source code | Replaces a name with a fragment of code | Before compilation |
What are macros in Excel and Word?
Microsoft defines a macro as "an action or a set of actions that you can run as many times as you want." If you repeat the same formatting or clean-up steps every week, a macro does them in one step.
Macros are recorded in the Visual Basic programming language, so you don't have to write code to create one. You can open the recorded code in the Visual Basic Editor and change it.
How to record a macro
- Turn on the Developer tab. It is hidden by default.
- On the Developer tab, choose Record Macro.
- Give the macro a name. You can also add a shortcut key and a description.
- Perform the actions you want to repeat.
- Choose Stop Recording.
The exact ribbon layout varies by Office version and platform. See the Microsoft quick start in the sources for your version.
What are macros in C and C++?
The GCC documentation defines a macro as "a fragment of code which has been given a name." When the name appears in the source, the preprocessor replaces it with the macro's contents. This is plain text substitution, and it happens before the compiler sees your code.
Object-like macros
An object-like macro is used like a constant. The GCC manual's example:
#define BUFFER_SIZE 1024
foo = (char *) malloc (BUFFER_SIZE);
The preprocessor expands BUFFER_SIZE, so the compiler sees foo = (char *) malloc (1024);. By convention macro names are written in uppercase, so you can tell at a glance which names are macros.
Function-like macros
A function-like macro looks like a function call. The opening parenthesis of the parameter list must come directly after the name, with no space. The manual's example:
#define lang_init() c_init()
lang_init()
→ c_init()
Common macro mistakes in C
Because a macro is text substitution and not a function call, it behaves differently from a function in two well-documented ways.
Missing parentheses change the meaning
The GCC manual shows a macro that does not do what was intended:
#define ceil_div(x, y) (x + y - 1) / y
a = ceil_div (b & c, sizeof (int));
→ a = (b & c + sizeof (int) - 1) / sizeof (int);
Operator precedence in C makes that equivalent to (b & (c + sizeof (int) - 1)) / sizeof (int). The addition groups with c instead of with b & c. The usual protection is to wrap each parameter, and the whole expansion, in parentheses.
Arguments can be evaluated more than once
If a macro uses an argument twice, an argument with a side effect, such as a function call, can run twice. The GCC manual notes that in a naive min macro, the function foo "might be called two times when the statement is executed." For GNU C it gives this version, which evaluates each argument exactly once:
#define min(X, Y) \
({ typeof (X) x_ = (X); \
typeof (Y) y_ = (Y); \
(x_ < y_) ? x_ : y_; })
typeof and the ({ ... }) statement-expression form are GNU extensions, so this exact macro is not portable standard C.
Are macros dangerous?
Office macros can be. Microsoft states that "VBA macros are a common way for malicious actors to gain access to deploy malware and ransomware." For that reason, Office on Windows now blocks macros in files from the internet, such as email attachments, and shows a Security Risk banner without an Enable content button.
Per Microsoft's documentation, this applies to Access, Excel, PowerPoint, Project, Publisher, Visio and Word on Windows. It does not apply to Office on Mac, Android, iOS or the web.
Allowing macros in a file you trust
Windows marks downloaded files with Mark of the Web, and that mark is what triggers the block. For a file you trust, right-click it, choose Properties, and tick Unblock on the General tab. The PowerShell equivalent, per Microsoft, is the Unblock-File cmdlet:
Unblock-File -Path C:\work\report.xlsm
Only do this for files from a source you know. The command removes the protection; it does not check what the macro does.
Frequently asked questions
- What is a macro in simple terms?
- A macro is a name attached to a set of instructions. When you use the name, the instructions run (in Excel or Word) or are substituted into your code (in C). Either way, you write the steps once and reuse them.
- Are macros safe to enable?
- Only if you trust the file. Microsoft states that VBA macros are a common way for attackers to deploy malware and ransomware, which is why Office on Windows blocks macros in files from the internet by default. Enable macros only in files from a source you know.
- Do I need to know programming to use macros?
- Not for Office. You can record a macro by performing the actions once, and Office stores them as Visual Basic code. Writing macros for a language such as C does require programming.
- Is a macro the same as a function?
- No. A C function is compiled code that is called at run time. A C macro is text replacement done by the preprocessor before compilation, so it has no type checking and can evaluate its arguments more than once.
Sources
- Microsoft Support — Quick start: Create a macro
- Microsoft Learn — Macros from the internet are blocked by default in Office
- GCC documentation — The C Preprocessor: Macros
- GCC documentation — Object-like Macros
- GCC documentation — Function-like Macros
- GCC documentation — Operator Precedence Problems
- GCC documentation — Duplication of Side Effects