🟡 Intermediate  ·  Lesson 30

Preprocessor Directives

Preprocessor क्या है?

C preprocessor compilation से पहले run होता है। यह # से start होने वाली lines handle करता है जैसे #include, #define, #ifdef आदि।

💡 याद रखें

Preprocessor directives के अंत में semicolon नहीं लगाया जाता।

#include

#include header file का content program में add करता है।

C Language
#include <stdio.h>
#include "myheader.h"

#define और Macros

C Language
#define PI 3.14159
#define SQUARE(x) ((x) * (x))
#define MAX(a,b) ((a) > (b) ? (a) : (b))
⚠️ ध्यान दें

Macros में parentheses जरूर लगाएं, वरना calculation गलत हो सकती है।

Conditional Compilation

C Language
#define DEBUG

#ifdef DEBUG
printf("Debug mode on\n");
#endif

Include Guards

C Language
#ifndef MYHEADER_H
#define MYHEADER_H

int add(int a, int b);

#endif

आम preprocessor directives

ये directives आपको सबसे ज़्यादा मिलेंगी, सभी # से शुरू और compilation से पहले संभाली जाती हैं:

Directiveउद्देश्य
#includeHeader file की सामग्री डालना
#defineConstant या macro बनाना
#undefपहले define किया macro हटाना
#ifdef / #ifndefMacro define होने (न होने) पर ही block compile करना
#if / #else / #endifConstant expression पर आधारित conditional compilation
#pragmaCompiler को विशेष निर्देश देना

आम गलतियाँ

  • Directive के बाद semicolon लगाना, जैसे #define PI 3.14;; value का हिस्सा बन जाता है।
  • Macro में parentheses भूलना, तो SQUARE(a+b) गलत expand होता है।
  • Macro वहाँ इस्तेमाल करना जहाँ const या असली function ज़्यादा सुरक्षित और type-checked होता।
  • Include guards छोड़ देना, जिससे वही header दो बार include हो जाता है।

सारांश

  • Preprocessor compiler से पहले run होता है
  • #include header file जोड़ता है
  • #define constants/macros बनाता है
  • #ifdef conditional compilation करता है
  • Include guards duplicate include रोकते हैं

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C में preprocessor क्या है?
Preprocessor एक step है जो असली compilation से पहले चलता है। यह हर उस line को संभालता है जो # से शुरू होती है — जैसे #include और #define — और compiler के देखने से पहले ही आपके source code को text रूप में बदल देता है (headers पेस्ट करना, constants बदलना)।
Preprocessor directives semicolon से खत्म क्यों नहीं होतीं?
क्योंकि ये C statements नहीं हैं — ये compiler को नहीं, preprocessor को निर्देश हैं। #define PI 3.14 जैसी line एक text-substitution नियम है, इसलिए यह line break पर खत्म होती है, और semicolon लगाना गलती से replacement text का हिस्सा बन जाएगा।
#include <file> और #include "file" में क्या अंतर है?
Angle brackets < > preprocessor को header standard system directories में ढूँढने को कहते हैं, जो stdio.h जैसे library headers के लिए है। Double quotes पहले आपके अपने project folder में ढूँढने को कहते हैं, जो आपके खुद लिखे headers के लिए है।
Include guards क्या हैं और क्यों ज़रूरी हैं?
Include guards वह #ifndef / #define / #endif pattern है जो एक ही header को एक से ज़्यादा बार पेस्ट होने से रोकता है। इनके बिना, किसी header को दो बार include करने से duplicate-definition errors हो सकती हैं, इसलिए guards headers को बार-बार include करने के लिए सुरक्षित बनाते हैं।
Conditional compilation क्या है?
Conditional compilation #ifdef और #endif जैसी directives से code के हिस्सों को इस आधार पर include या छोड़ती है कि कोई macro defined है या नहीं। यह आमतौर पर सिर्फ़ debug वाला code जोड़ने या अलग platforms के लिए अनुकूल code लिखने में इस्तेमाल होती है।
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।