5.1.28C Programming

Macros vs inline functions

1,771 words8 min readdifficulty · medium1 backlinks

WHAT they are


WHY the difference matters — the classic trap


HOW the compiler turns each into machine code

Figure — Macros vs inline functions

Side-by-side comparison

Aspect Macro (#define) Inline function
Handled by Preprocessor Compiler
Type checking ❌ None ✅ Full
Argument evaluated Possibly multiple times Once
Respects scope ❌ (global text)
Debuggable / breakpoints ❌ Hard ✅ Yes
inline is a... (n/a) request, compiler may ignore
Has an address / can take &
Generic over types ✅ (works for any type) ❌ (one type, unless overloaded — not in C)

A correct macro vs a clean inline



Recall Explain it to a 12-year-old (hidden)

Imagine a recipe says "add SQUARE(amount of sugar)". A macro is like a careless helper who literally writes your words again — if you said "1+1 cups", he writes "1+1 × 1+1 cups" and gets the math wrong. An inline function is a smart helper who first figures out you meant 2 cups, then squares it to 4 — every time, correctly. Both save you a trip to a faraway kitchen (the function call), but the smart helper doesn't mess up.


Recall checkpoint


Flashcards

Who handles macros, preprocessor or compiler?
The preprocessor — pure text substitution before compilation.
Who handles inline functions?
The compiler, which may expand the body at the call site.
Why must macro parameters and the whole body be parenthesized?
To preserve operator precedence; text substitution otherwise breaks expressions like SQ(2+3).
What does SQ(2+3) expand to with #define SQ(x) x*x?
2+3*2+3 = 11, not 25.
Why is SQ(i++) unsafe as a macro?
i++ is substituted twice → (i++)*(i++), evaluating the side effect twice (undefined/unexpected).
How many times does an inline function evaluate each argument?
Exactly once — arguments are computed before the body runs.
Is inline a command or a request?
A request/hint; the compiler may ignore it.
Give one thing macros can do that functions cannot.
Type-generic code, stringizing #x, token pasting ##, or conditional compilation.
Which is easier to debug, macro or inline function?
Inline function — you can set breakpoints and it respects scope/types.
What idiom makes a multi-statement macro a single safe statement?
do { ... } while(0).

Connections

  • C Preprocessor#define, #include, conditional compilation
  • Operator Precedence in C — why macro parens matter
  • Function Call Overhead — the cost both techniques remove
  • Undefined Behavior in C — double-evaluation side effects
  • const and constexpr — typed alternatives to constant macros
  • Compiler Optimization — when inline is actually honored

Concept Map

both avoid

both avoid

handled by

handled by

does

keeps

causes

causes

fixed by

avoided by

guarantees

belongs to

Function call overhead

Macro

Inline function

Preprocessor

Compiler

Blind text substitution

Type checking and scope

Precedence trap

Double-evaluation trap

Parenthesize everything

Arg evaluated once

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, dono cheezein — macros aur inline functions — ek hi problem solve karti hain: chhote-chhote operations ke liye normal function call ka overhead bachana. Lekin kaam karne ka tareeka bilkul alag hai. Macro ko preprocessor handle karta hai, matlab woh sirf text copy-paste karta hai, usse C ke types ya brackets ki koi samajh nahi. Isliye #define SQ(x) x*x likho aur SQ(2+3) call karo, to woh ban jaata hai 2+3*2+3 = 11, na ki 25. Solution: har cheez ko bracket mein lapeto — ((x)*(x)).

Inline function ko compiler handle karta hai. Yeh ek asli function hai jisme type checking hoti hai, scope respect hota hai, aur sabse important — har argument sirf ek baar evaluate hota hai. Macro mein SQ(i++) likho to i do baar badh jaata hai (bug!), par inline function mein i ek hi baar badhega. Isi wajah se modern C mein default choice inline function hai — safe, debuggable, predictable.

Ek important baat: inline keyword sirf ek request hai, guarantee nahi. Compiler chaahe to ignore kar de (jaise recursive ya bade function mein). Toh inline ka matlab "main chahta hoon yeh inline ho" — control compiler ke paas hai. Macro tab use karo jab function se kuch na ho paaye, jaise type-generic code, #x stringizing, ya a##b token pasting.

Yaad rakhne ka simple rule: "Pre-Paste, Com-Check" — Macro = Preprocessor sirf paste karta hai (parentheses aur double-evaluation ke traps ke saath), Inline = Compiler types check karta hai aur har argument ek baar ginta hai. Exam aur real coding dono mein yeh difference bahut puchha jaata hai.

Go deeper — visual, from zero

Test yourself — C Programming

Connections