⚙️
Preprocessor

Preprocessor Directives

#include, #ifdef, #pragma
💡 Preprocessor ek editor hai jo tumhara code compile hone SE PEHLE padh leta hai aur usme changes karta hai — jaise koi tumhara draft essay submit karne se pehle text find-replace kar de, ya kuch paragraphs conditionally add/hata de.

Preprocessor directives # se shuru hote hain aur compilation se pehle process hote hain — ye C language ka part nahi hain, balki ek separate text-processing step hai. #include kisi file ka poora content copy-paste kar deta hai (jaise <stdio.h> ki saari declarations).

#ifdef/#ifndef/#endif conditional compilation ke liye use hote hain — kuch code sirf tab include karo agar koi condition true ho (jaise ek specific platform ke liye alag code, ya debug-only code). #pragma compiler-specific instructions dete hain (jaise warnings ko suppress karna).

#ifndef ka ek bahut common use hai "include guards" — same header file ko galti se do baar include hone se rokna, jo compile errors deta.

#include <stdio.h>     // file content copy-paste ho jaata hai

#define DEBUG 1

#ifdef DEBUG
  #define LOG(msg) printf("DEBUG: %s\n", msg)
#else
  #define LOG(msg)   // production mein kuch nahi hota
#endif

int main() {
  LOG("Program shuru hua");  // sirf DEBUG defined hone par print hoga
  return 0;
}
⚙️
Preprocessor ek editor hai jo tumhara code compile hone SE PEHLE padh leta hai aur usme changes karta hai — jaise koi tumhara draft essay submit karne se pehle text find-replace kar de, ya kuch paragraphs conditionally add/hata de.
1 / 6
⚡ झट से Recap
  • # se shuru — compilation se PEHLE process hota hai
  • #include = file content copy-paste
  • #ifdef/#ifndef = conditional compilation
इस page में (2 subtopics)

#ifndef/#define/#endif traditional include guards hain (poore C standard mein support hain). #pragma once ek shorter, modern alternative hai jo same kaam karta hai (ek header ko double-include hone se rokta hai), lekin ye standard C nahi hai — sirf usually-supported compiler extension hai (almost saare modern compilers support karte hain, lekin technically guarantee nahi hai).

// Traditional (guaranteed portable):
#ifndef MYHEADER_H
#define MYHEADER_H
// ... header content ...
#endif

// Modern (compiler extension, widely supported):
#pragma once
// ... header content ...
💡Tip: Production codebases mein #pragma once bahut common hai (kam typing, kam error-prone — naam clash ka risk nahi), lekin agar maximum portability chahiye (obscure/old compilers), traditional guards safer hain.

C compiler kuch macros automatically define karta hai jo debugging mein useful hain: __FILE__ (current file ka naam), __LINE__ (current line number), __DATE__/__TIME__ (compile hone ka date/time). Ye error messages aur logging mein bahut use hote hain.

printf("Error in %s at line %d\n", __FILE__, __LINE__);
printf("Compiled on: %s at %s\n", __DATE__, __TIME__);

// Custom assert-jaisa macro banane mein bhi use hota hai:
#define DEBUG_PRINT(msg) \
  printf("[%s:%d] %s\n", __FILE__, __LINE__, msg)