Macros vs functions
8 min lesson
- A macro is a text replacement created with #define; a function is real compiled code you call. Both can do similar jobs but behave very differently.
- The choice matters because macros can be faster but are harder to debug, while functions are safer and easier to read.
- Think of a macro as a find-and-replace in a document, and a function as a recipe card the cook actually follows step by step.
- Function-like macros take arguments: #define SQUARE(x) ((x) * (x)). They are pasted in at every use site.
- Functions have a return type and real parameters, get type-checked, and can be stepped through in a debugger.
- Macros have no type checking, can evaluate arguments more than once, and can behave surprisingly with side effects like i++.
- The computer expands macros before compiling, so the compiler never sees the macro name, only the pasted text.
- The syntax is: #define NAME(args) (expression) with parentheses around the whole body and around each argument.
- A realistic use: a tiny SQUARE or MAX macro for speed, and a function for anything with loops, complex logic or debugging needs.
- A surprising detail: SQUARE(i++) increments i twice, because the argument is pasted twice; a function would increment once.
- The example program compares a SQUARE macro with a square function and shows the same result for simple values.
- Prefer functions for anything with logic, side effects or debugging needs.
- Use macros only for simple, safe expressions, with full parentheses.
- Remember that macros paste text and can evaluate arguments more than once.
- Name macros in ALL_CAPS and keep them short.
- You now know when each tool shines. Great judgment!
#include <stdio.h>
// A function-like macro: parentheses around the body and the argument
#define SQUARE(x) ((x) * (x))
// A function that does the same job with real type checking
int square(int x) {
return x * x;
}
int main(void) {
// Use the macro with a simple value
int a = 5;
printf("Macro SQUARE(5) = %d\n", SQUARE(a));
// Use the function with the same value
printf("Function square(5) = %d\n", square(a));
// A small value to show they agree
int b = 3;
printf("Macro SQUARE(3) = %d\n", SQUARE(b));
printf("Function square(3) = %d\n", square(b));
return 0;
}What happens: Macro SQUARE(5) = 25 Function square(5) = 25 Macro SQUARE(3) = 9 Function square(3) = 9
Breaking it down
Macro definition
#define SQUARE(x) ((x) * (x))Defines a text replacement. Every SQUARE(a) is pasted as ((a) * (a)). The parentheses protect against precedence surprises.
Function definition
int square(int x) { return x * x; }Compiles to real code with a parameter and a return value. The compiler checks the type of x.
Macro use
SQUARE(a)Becomes ((a) * (a)) at compile time. There is no function call and no type checking.
Function use
square(a)Calls the function at runtime. The argument is evaluated once and type-checked against int.
Macros vs functions
| Feature | Macro | Function |
|---|---|---|
| Type checking | None | Yes |
| Argument evaluation | Can repeat | Exactly once |
| Debugging | Hard to step | Easy to step |
| Speed | No call overhead | Small call overhead |
| Code size | Grows at each use | Single copy |
A helpful analogy
A macro is like a find-and-replace in a document: whatever text you match is pasted in, exactly as written. A function is like a recipe card the cook follows: the ingredients are measured once, and the steps are the same every time. Macros are fast but careless; functions are careful but slightly slower.
Nesting rules
- 1.Wrap the whole macro body and each argument in parentheses to avoid precedence bugs.
- 2.Never pass an expression with side effects like i++ to a macro that uses its argument more than once.
- 3.Use a function instead of a macro when you need type checking or debugging.
- 4.Keep macros short; a long macro is a sign it should be a function.
Common mistakes
Missing parentheses around the macro body
Avoid
#define SQUARE(x) x * x
SQUARE(1 + 2) // becomes 1 + 2 * 1 + 2Use instead
#define SQUARE(x) ((x) * (x))Without parentheses, precedence rules apply to the pasted text and produce the wrong result. Always wrap the body.
Passing a side-effect argument to a macro
Avoid
#define DOUBLE(x) ((x) + (x))
DOUBLE(i++) // i incremented twiceUse instead
int t = i; DOUBLE(t); i++;The macro pastes i++ twice, so i increases by 2. A function would evaluate its argument once.
Using a macro where a function is clearer
Avoid
#define MAX(a,b) ((a)>(b)?(a):(b))
MAX(slow(), fast()) // slow may run twiceUse instead
int max(int a, int b) { return a > b ? a : b; }Macros can call an expensive function twice because the argument is pasted twice. Functions evaluate arguments once.
Quick tips
- •Use ALL_CAPS for macros so readers know they are not functions.
- •Prefer static inline functions in modern C when you want speed and safety.
- •Test a macro with expressions like 1 + 2, not just simple numbers.
- •Try SQUARE(1 + 2) with and without parentheses to see the difference.
- •Read compiler warnings; some catch suspicious macro use.