Foundations — Preprocessor directives — #define, #ifdef, #ifndef, #include guards
5.1.27 · D1· Coding › C Programming › Preprocessor directives — #define, #ifdef, #ifndef, #include
Is page par assume kiya gaya hai ki tumhe kuch nahi pata. Hum har wo symbol, word, aur squiggle collect karte hain jis par parent note rely karta hai, har ek ko simple words mein define karte hain, use ek picture se jodte hain, aur batate hain ki topic uske bina kyun chal nahi sakta. Upar se neeche padho — har block mein sirf wahi cheezein use hoti hain jo uske upar define ho chuki hain.
0. "Source code" aur "file" kya hai?
Picture yeh hai: ek scroll of characters socho, left se right, top se bottom. Us scroll par kuch "run" nahi hota abhi. Tools use padhenge aur transform karenge.
Topic ko kyun chahiye: preprocessor ka poora kaam is text ko edit karna hai. Agar tum file ko raw text ke roop mein nahi socho, toh "text substitution" ka koi matlab nahi.
1. "Token" kya hota hai?
Figure dekho: wahi characters PI*r*r tokens mein slice ho jaate hain — PI, *, r, *, r. Preprocessor poore tokens replace karta hai, kabhi kisi token ka hissa nahi — yahi wajah hai ki #define PI 3.14 PIPE jaisi kisi word ke andar likhe PI ko kabhi nahi chhuta.
Topic ko kyun chahiye: #define NAME replacement token NAME ko match karke kaam karta hai. "Token" samajhne se pata chalta hai ki kya replace hoga aur kya nahi.
2. # symbol aur "directive"
Picture yeh hai: socho ki do coloured highlighters scroll par chal rahe hain. Har # line ko ek bright marker milta hai jiska matlab hai "helper, mujhe padh aur phir mujhe mita de." Jab helper ka kaam khatam ho jaata hai, koi bhi # line nahi bachti.
Topic ko kyun chahiye: parent page par jo bhi cheez hai (#define, #ifdef, #include) sab directive hai. Yeh ground floor hai.
3. "Substitution" — find-and-replace
Figure scroll ko pehle aur baad mein dikhata hai: har token PI ko tokens 3.14159 se swap kar diya jaata hai. Helper ko nahi pata ki PI "ek number" hai ya "ek constant" — use bas itna pata hai "is token ko match karo, woh text paste karo."
Topic ko kyun chahiye: #define hi substitution hai. Parent page par jo bhi macro trap hai woh ek substitution surprise hai.
4. "Defined" vs "not defined" — ek yes/no switch
Picture yeh hai: switches ki ek row, ek switch har naam ke liye. #define DEBUG ne DEBUG ko ON kar diya. Us switch ke saath on/off sense mein koi value attached nahi — #define FOO jisme baad mein kuch nahi hai woh bhi ON count hoti hai.
Topic ko kyun chahiye: #ifdef poochta hai "kya yeh switch ON hai?"; #ifndef poochta hai "kya yeh switch OFF hai?". Switch ki picture ke bina, conditionals ka koi matlab nahi.
5. "Conditional" — ek block ko rakhna ya delete karna
Figure ko upar se neeche follow karo: #ifdef DEBUG gate DEBUG switch check karta hai. Switch ON → enclosed lines compiler tak pahunch jaati hain (green path). Switch OFF → wahi exact lines cut out hokar phek di jaati hain (red path). Yeh ek compile-time decision hai, program ke kabhi run hone se pehle liya jaata hai — OS-selection use ke liye Conditional compilation for cross-platform code dekho.
Topic ko kyun chahiye: debug toggles, default values, aur — sabse zaroori — include guards sab "ek block ko rakhna ya delete karna" hain.
6. "Include" aur "ek file paste karna"
Picture yeh hai: helper scroll ko #include line par kaatta hai aur file.h ka poora content glue kar deta hai, phir aage badhta hai. Agar do alag #include lines usi file ko point karti hain, toh uska text do baar glue ho jaata hai — yahi exact problem hai jo include guards fix karte hain. Saare includes aur substitutions khatam hone ke baad jo text milta hai use translation unit kehte hain; dekho Header files and translation units.
Topic ko kyun chahiye: "ek poori file paste karna" ke bina, "do baar paste hua → redefinition error" phrase ka kuch describe karne ko nahi hai, aur include guards kuch solve nahi karte.
7. "Redefinition" — do baar paste karna kyun hurt karta hai
Picture yeh hai: tum apni notebook mein ek hi page do baar glue karte ho; ab "the definition of Point" ki do copies hain, aur compiler complain karta hai ki woh nahi bata sakta kaunsi real hai.
Topic ko kyun chahiye: yeh woh pain hai jise include guards (Section 5 + Section 4 combined) prevent karne ke liye exist karte hain. Guard ek switch (Section 4) use karta hai block (Section 5) ko sirf pehli baar paste hone par (Section 6) rakhne ke liye.
Foundations topic ko kaise feed karte hain
Isko aise padho: raw text tokens mein kaata jaata hai; # lines directives hain; directives substitution karte hain, switches flip karte hain, aur files paste karte hain; switches + block-keeping se conditionals milte hain; conditionals + pasting + redefinition pain se include guards milte hain. Saare raaste parent topic par milte hain.
Aage yahan connect hota hai
- Poora pipeline (preprocess → compile → assemble → link) Compilation pipeline — preprocess, compile, assemble, link mein hai.
- Macro use karna chahiye ya real function, yeh decide hota hai Macros vs inline functions mein.
- File ke bahar se ek naam ON karna Build flags -D and the command line se hota hai.
Equipment checklist
Tum parent topic ke liye ready ho agar pehle ye sab loud bol ke answer kar sako, phir reveal karo:
Ek .c file, koi bhi tool run hone se pehle, best picture ki jaati hai…
Ek "token" hota hai…
Ek line "directive" tab hoti hai jab…
# ho; yeh preprocessor ko diya gaya order hai, C statement nahi, isliye semicolon nahi lagta."Substitution" ka matlab hai…
"Blind" substitution hi wajah hai ki macros ko…
Ek naam ka "defined" hona hai…
#define run hone ke baad yaad rakhta hai.#ifdef vs #ifndef poochte hain…
"Conditional compilation" decide karta hai…
#include "x.h" karta hai…
Ek "redefinition error" cause hoti hai…
Recall Feynman: poora page ek saanss mein
Source file bas text hai. Tools use chote chunks (tokens) mein kaatate hain. # se shuru hone wali lines ek helper (preprocessor) ke liye notes hain. Helper ek chunk ko doosre text se replace kar sakta hai (#define), on/off switches yaad rakh sakta hai (defined), un switches ke basis par poore blocks ko rakh ya phek sakta hai (#ifdef/#ifndef), aur poori files glue kar sakta hai (#include). Usi file ko do baar glue karne se duplicate definitions create hoti hain, jo illegal hai — isliye hum ek switch use karte hain header ke block ko sirf pehli baar rakhne ke liye. Yahi har woh idea hai jo parent topic use karega.