Skip to content
StudyFame

Macro vs Function in C

A function-like macro such as MAX(a, b) looks just like a function call. But inside, they work in completely different ways: a macro is copied into your code before compilation, while a function is called while the program runs.

In this lesson you will compare macros and functions on speed, program size, type checking, side effects and debugging, see a program where a macro gives a surprising answer, and learn when to use each one, including inline functions.

What You Will Learn

  • How a macro call and a function call are processed differently.
  • The full difference table between macro and function.
  • Why a macro can evaluate an argument twice (side effects).
  • Why functions are type-safe and macros are not.
  • What an inline function is.
  • When to choose a macro and when to choose a function.

Macro vs Function in C Explained

Exam definition: A macro is expanded by the preprocessor before compilation: its body is substituted at every place it is used, with no type checking and no function-call overhead. A function is compiled once; each call transfers control to it at run time, its arguments are type-checked and evaluated exactly once, and it returns a value through the call stack.

Memory trick: A macro is a photocopy: every place that needs the notes gets its own copy (fast to read, but uses more paper). A function is the library book: one copy, and everyone walks to the library to use it (less paper, but the walk takes a little time).

Difference Between Macro and Function

PointMacroFunction
Processed byPreprocessor (before compilation)Compiler; runs at run time
How it worksText is copied at every useControl jumps to one compiled copy
SpeedNo call overhead, slightly fasterSmall overhead for each call
Program sizeGrows with every useCode exists only once
Type checkingNoneArguments checked and converted
Argument evaluationCan be evaluated more than onceExactly once
Works with any typeYes (int, double, …)Only the declared parameter types
DebuggingHard (the debugger never sees the macro)Easy (you can step into it)
RecursionNot possiblePossible
Return statementNo return; the expression is the resultUses return

Syntax: The Same Task Written Both Ways

/* Macro: copied wherever MAX(...) appears */
#define MAX(a, b) ((a) > (b) ? (a) : (b))

/* Function: compiled once, called at run time */
int max_int(int a, int b)
{
    return a > b ? a : b;
}

/* Inline function: a function the compiler MAY copy like a macro */
static inline int max_inline(int a, int b)
{
    return a > b ? a : b;
}

Example 1: The Side-Effect Trap

#include <stdio.h>

#define MAX(a, b) ((a) > (b) ? (a) : (b))

int max_int(int a, int b)
{
    return a > b ? a : b;
}

int main(void)
{
    int i = 5, j = 3, result;

    result = max_int(i++, j);
    printf("Function: result = %d, i = %d\n", result, i);

    i = 5;
    result = MAX(i++, j);
    printf("Macro   : result = %d, i = %d\n", result, i);

    return 0;
}

Output

Function: result = 5, i = 6
Macro   : result = 6, i = 7

Code Explanation

  1. max_int(i++, j): the argument i++ is evaluated once. The function receives 5 and 3, returns 5, and i becomes 6. That is what we expect.
  2. MAX(i++, j) expands to ((i++) > (j) ? (i++) : (j)). Now i++ appears twice.
  3. First, the condition 5 > 3 is true and i becomes 6.
  4. Because the condition was true, (i++) runs again: it gives 6 and i becomes 7.
  5. So the macro returns 6 and increments i twice. This is called a side-effect problem, and it can't happen with a function.

How Each One Works

MACRO  MAX(x, y)                     FUNCTION  max_int(x, y)
--------------------------------     ----------------------------------
Before compiling:                    At run time:
  MAX(x, y) is replaced by             1. x and y are evaluated (once)
  ((x) > (y) ? (x) : (y))              2. values are copied to a and b
                                       3. control jumps to max_int
The compiler sees the full             4. return sends the answer back
expression at every place.             5. control comes back to the caller
No jump, no copy, no type check.     One copy of the code, type-checked.

Modern compilers are very good at making small functions fast, often by copying (inlining) them automatically. So the speed benefit of macros is usually tiny today, while their risks stay the same.

Example 2: Type Checking and Generic Macros

#include <stdio.h>

#define MAX(a, b) ((a) > (b) ? (a) : (b))

int max_int(int a, int b)
{
    return a > b ? a : b;
}

static inline double max_double(double a, double b)
{
    return a > b ? a : b;
}

int main(void)
{
    printf("MAX(2.5, 1.5)        = %.1f\n", MAX(2.5, 1.5));
    printf("max_int(2.5, 1.5)    = %d\n", max_int(2.5, 1.5));
    printf("max_double(2.5, 1.5) = %.1f\n", max_double(2.5, 1.5));
    printf("MAX('a', 'z')        = %c\n", MAX('a', 'z'));
    return 0;
}

Output

MAX(2.5, 1.5)        = 2.5
max_int(2.5, 1.5)    = 2
max_double(2.5, 1.5) = 2.5
MAX('a', 'z')        = z
  • The macro works with any type: doubles, ints, even characters. That is its real advantage.
  • max_int accepts only int. The compiler converts 2.5 and 1.5 to 2 and 1, so the answer is 2. The function is strict about types (good for safety), but you need one function per type.
  • max_double is an inline function: type-safe like a function, and the compiler may copy it into the caller like a macro.

When to Use a Macro vs a Function

Use a macro when…Use a function when…
You need a named constant (#define SIZE 100)The code is more than one short expression
You need #, ##, __FILE__ or __LINE__Arguments may have side effects (i++, function calls)
You need conditional compilationYou want type checking and easy debugging
It must work for many types (very small expressions only)You need recursion or local variables

Important Points to Remember

  • Macro = expanded before compilation; function = called at run time.
  • Macros are faster but make the program bigger; functions are smaller but have call overhead.
  • Macros do no type checking; functions check and convert types.
  • A macro may evaluate an argument more than once; never pass i++ to a macro.
  • Macros can't be recursive and are hard to debug.
  • Inline functions give most of a macro's speed with a function's safety.

Common Mistakes Beginners Make

MistakeWhat happensFix
MAX(i++, j)i may increase twiceUse a function, or increment separately
MAX(get_input(), 10)get_input() may be called twiceStore the result in a variable first
Writing a 20-line macroHard to read and debug, bigger programWrite a normal function
Expecting a macro to check typesWrong types pass silentlyUse a function for type safety
Trying a recursive macroIt is not expanded again; compile errorRecursion needs a function

Macro vs Function Practice Questions

  1. Give three differences between a macro and a function.
  2. Why is a macro faster than a function?
  3. Why does a program become bigger when a macro is used many times?
  4. What is a side effect problem in macros? Give an example.
  5. What is an inline function?
Show answers
  1. Macro is expanded by the preprocessor, function is called at run time; macro has no type checking, function does; macro can evaluate arguments twice, function evaluates once (also: size, debugging, recursion).
  2. There is no function call overhead: no jump, no argument copying, no return.
  3. The macro body is copied at every place it is used.
  4. An argument with a side effect is evaluated more than once, e.g. MAX(i++, j) increments i twice when i is larger.
  5. A function declared with inline, which the compiler may expand at the call site like a macro while keeping type checking.

Coding Exercise

Write a macro ABS_M(x) and a function abs_f(int x) that return the absolute value. Call both with n-- (starting with n = -4 each time) and print the result and the final value of n. Explain the difference.

Show solution
#include <stdio.h>

#define ABS_M(x) ((x) < 0 ? -(x) : (x))

int abs_f(int x)
{
    return x < 0 ? -x : x;
}

int main(void)
{
    int n = -4, r;

    r = abs_f(n--);
    printf("Function: result = %d, n = %d\n", r, n);

    n = -4;
    r = ABS_M(n--);
    printf("Macro   : result = %d, n = %d\n", r, n);
    return 0;
}
Function: result = 4, n = -5
Macro   : result = 5, n = -6

The macro expands to ((n--) < 0 ? -(n--) : (n--)): the condition uses −4 (n becomes −5), then -(n--) gives 5 and n becomes −6. The function evaluates n-- only once.

Quick Quiz: Macro vs Function (MCQ)

  1. Which one is type-checked by the compiler?

    A) Macro   B) Function   C) Both   D) Neither

  2. Which usually makes the executable larger when used many times?

    A) Function   B) Macro   C) Both equally   D) Neither

  3. For #define MAX(a,b) ((a)>(b)?(a):(b)), i = 5, j = 3, what is i after MAX(i++, j)?

    A) 5   B) 6   C) 7   D) 8

  4. Which can be recursive?

    A) Macro   B) Function   C) Both   D) Neither

  5. A function that the compiler may expand at the call site is called:

    A) static   B) extern   C) inline   D) volatile

Show answers
  1. B: function.
  2. B: macro.
  3. C: 7 (incremented twice).
  4. B: function.
  5. C: inline.

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

Processed byMacro: preprocessor; function: compiler / run time
SpeedMacro faster (no call overhead)
SizeMacro makes code bigger
Type checkingOnly functions
Side effectsMacro may evaluate arguments twice
Recursion / debuggingOnly functions handle these well
Best of bothInline function

Frequently asked questions

What is the main difference between a macro and a function?
A macro is replaced by text before compilation; a function is compiled once and called at run time.
Which is faster, a macro or a function?
A macro, because it has no call overhead; but it increases code size and has no type checking.
Why should you not pass i++ to a macro?
The macro may use the argument more than once, so i can be incremented twice.

Remember

  • Macro = photocopy; function = library book.
  • Small constant or trick: macro. Real logic: function.

Next Topic

Next lesson: Header Files in C. You will learn how header files work and how to create your own header with include guards.