5.1.28 · HinglishC Programming

Macros vs inline functions

1,611 words7 min readRead in English

5.1.28 · Coding › C Programming


YEH HAIN KYA


FARK KYU MAAYANE RAKHTA HAI — classic trap


COMPILER DONO KO MACHINE CODE MEIN KAISE BANATA HAI

Figure — Macros vs inline functions

Side-by-side comparison

Aspect Macro (#define) Inline function
Handle karta hai Preprocessor Compiler
Type checking ❌ Nahi ✅ Poori
Argument evaluate hota hai Shayad kai baar Ek baar
Scope respect karta hai ❌ (global text)
Debuggable / breakpoints ❌ Mushkil ✅ Haan
inline ek... hai (n/a) request, compiler ignore kar sakta hai
Address hai / & le sakte hain
Types ke liye generic ✅ (kisi bhi type ke liye kaam karta hai) ❌ (ek type, jab tak overload na ho — C mein nahi)

Ek sahi macro vs ek clean inline



Recall Ek 12-saal ke bachche ko samjhao (hidden)

Socho ek recipe kehti hai "SQUARE(amount of sugar) daalo". Ek macro ek laparwah helper ki tarah hai jo literally aapke words dobara likhta hai — agar aapne "1+1 cups" kaha, toh woh likhta hai "1+1 × 1+1 cups" aur math galat kar deta hai. Ek inline function ek samajhdar helper hai jo pehle figure out karta hai ki aapka matlab 2 cups tha, phir use square karke 4 karta hai — har baar, sahi tarike se. Dono aapko door ki kitchen ka chakkar bachate hain (function call), lekin samajhdar helper galti nahi karta.


Recall checkpoint


Flashcards

Who handles macros, preprocessor or compiler?
Preprocessor — compilation se pehle pure text substitution.
Who handles inline functions?
Compiler, jo body ko call site pe expand karne ka choice kar sakta hai.
Why must macro parameters and the whole body be parenthesized?
Operator precedence preserve karne ke liye; text substitution warna SQ(2+3) jaisi expressions tod deta hai.
What does SQ(2+3) expand to with #define SQ(x) x*x?
2+3*2+3 = 11, 25 nahi.
Why is SQ(i++) unsafe as a macro?
i++ do baar substitute hota hai → (i++)*(i++), side effect do baar evaluate hota hai (undefined/unexpected).
How many times does an inline function evaluate each argument?
Bilkul ek baar — body chalne se pehle arguments compute ho jaate hain.
Is inline a command or a request?
Ek request/hint; compiler ise ignore kar sakta hai.
Give one thing macros can do that functions cannot.
Type-generic code, stringizing #x, token pasting ##, ya conditional compilation.
Which is easier to debug, macro or inline function?
Inline function — aap breakpoints set kar sakte ho aur yeh scope/types respect karta hai.
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 — macro parens kyun matter karte hain
  • Function Call Overhead — woh cost jo dono techniques remove karti hain
  • Undefined Behavior in C — double-evaluation side effects
  • const and constexpr — constant macros ke typed alternatives
  • Compiler Optimization — jab inline actually honor kiya jaata hai

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