5.1.28 · D1 · HinglishC Programming

FoundationsMacros vs inline functions

2,051 words9 min read↑ Read in English

5.1.28 · D1 · Coding › C Programming › Macros vs inline functions

Is ladai ko judge karne se pehle, tumhare paas har woh symbol hona chahiye jo parent note use karta hai. Neeche har cheez zero se build ki gayi hai: plain words → ek picture → kyun topic ko uski zaroorat hai. Upar se neeche padho; har rung neeche wale par khada hai.


1. "Source code ki ek line" actually kya hoti hai

Figure — Macros vs inline functions

Figure dekho: wahi line SQUARE(2 + 3) do baar dikhaayi gayi hai. Baayein ek haath sirf letters copy karta hai (yeh preprocessor ka nazariya hai). Daayein letters ko pehle numbers aur operations ki tarah samjha jaata hai (yeh compiler ka nazariya hai). Is picture ko poore topic ke liye dimag mein rakho — yeh sab kuch explain karta hai jo aage aata hai.


2. Do translators: preprocessor aur compiler

#define SQUARE(x) ((x) * (x))   // <- # ka matlab hai: preprocessor, mujhe handle karo

# decoration nahin hai. Yeh ek flag hai jo kehta hai: "yeh line pehle translator ke liye hai, doosre ke liye nahin." #define ke baad ki har cheez ek rule hai jise preprocessor literally maanta hai.


3. Symbol #define — ek find-and-replace rule

Figure — Macros vs inline functions

Figure mein arrows follow karo. x ek hole hai. Tum hole mein jo bhi text daalo woh body mein har jagah jahan x aata hai wahan stamp ho jaata hai — characters ki tarah, uncalculated. Isliye parent ka SQUARE(2 + 3) literal string ((2 + 3) * (2 + 3)) ban jaata hai: chaar characters 2 + 3 do baar, verbatim, paste ho jaate hain.


4. Parentheses ( ) aur operator precedence

Figure — Macros vs inline functions

Figure mein #define BAD(x) x*x ke BAD(2+3) ke liye use karne ke dono trees hain. Parens ke bina pasted text 2+3*2+3 hai; precedence * ko sirf adjacent 3 aur 2 pakadne deta hai, jo galat red subtree = 11 deta hai. Parens ke saath, deewarein (2+3) ko square karne se pehle ek unit ki tarah resolve hone par majboor karti hain, jo 25 deta hai. Trap macro mein nahin hai — woh precedence mein hai jo carelessly pasted text par act kar raha hai.


5. Symbol x++ aur "side effect"

#define SQ(x) ((x)*(x))
int i = 3;
int r = SQ(i++);   // ((i++)*(i++)) ban jaata hai -> i do baar change hua

Ek inline function yeh sidestep kar leta hai kyunki argument i++ body ke chalane se pehle ek baar ek parameter mein compute ho jaata hai — ek concept jo hum section 7 mein build karte hain.


6. Symbol inline aur "function call overhead"

Figure — Macros vs inline functions

Figure do paths contrast karti hai. Baayein path ek function call karta hai: control current spot se nikal jaata hai, function tak jaata hai, aur wapas aata hai — round-trip overhead hai. Daayein path mein function ki body in place paste ho jaati hai (inlined), toh koi trip hi nahin. Dono macros aur inline functions right-hand picture ke liye koshish karte hain; woh sirf pasting karne ke liye alag translators choose karte hain.

static inline int square(int x) { return x * x; }

7. "Type checking" aur "scope" tumhe kya dete hain


Prerequisite map

Source code is plain text

Preprocessor edits text

Compiler understands C

define replaces text

Function-like macro

Operator precedence

Parentheses force grouping

Side effect of plus plus

Double evaluation

Type checking and scope

Inline function

Function call overhead

Macros vs inline functions


Equipment checklist

Khud test karo — daayein side cover karo.

Kya ek .c file text hai ya translation se pehle machine code?
Sirf plain text — CPU ke liye meaningless jab tak translate na ho.
Kaun sa translator pehle chalta hai, preprocessor ya compiler?
Preprocessor, compiler se pehle pure text edits karta hua.
#define SQ(x) ((x)*(x)) literally SQ(5) ke saath kya karta hai?
Body paste karta hai x ki jagah 5 leke, jo text ki tarah ((5)*(5)) deta hai.
Kya preprocessor 2+3 calculate karta hai?
Nahin — woh sirf characters copy karta hai; compiler baad mein arithmetic karta hai.
* 2+3*2+3 ke pieces kyun pakadta hai?
Kyunki * ki higher precedence hai + se, toh woh addition se pehle bind karta hai.
Parentheses precedence ke saath kya karte hain?
Woh grouping force karte hain, andar wale ko pehle resolve karwa kar chahe baahri operators koi bhi hon.
((i++)*(i++)) mein i++ kitni baar chalta hai?
Do baari do baar increment hota hai, double-evaluation trap.
Function call overhead kya hai?
Jumping, registers save karne, aur return karne ki cost — woh round-trip jise dono techniques hatati hain.
Kya inline ek guarantee hai?
Nahin — ek request/hint jise compiler ignore kar sakta hai.
Ek real function (macro nahin) ka ek gift batao.
Type checking, single argument evaluation, scope, debuggability, ya ek address hona.

Connections

  • Parent topic ↑
  • C Preprocessor — pehla translator jo text pasting karta hai
  • Operator Precedence in C — kyun macros mein parentheses non-negotiable hain
  • Function Call Overhead — woh cost jise hatane ke liye yeh techniques exist karti hain
  • Undefined Behavior in C — jahan double evaluation le jaati hai
  • const and constexpr — constant macros ke typed alternatives
  • Compiler Optimization — jab inline honor hota hai