Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🔵 Core C++  ·  Lesson 34

Preprocessor Directives

Preprocessor क्या है?

आपका code compiler तक पहुँचने से पहले preprocessor नाम का program उसे पढ़ता है और # से शुरू होने वाली हर line को follow करता है। वह header files जोड़ता है, macro नामों को उनके text से बदलता है, और जो code blocks आपने बंद किए हैं उन्हें हटा देता है। नतीजा साधारण C++ होता है, जिसे फिर compiler translate करता है।

Directives preprocessor के लिए निर्देश हैं, C++ statements नहीं। ये # से शुरू होते हैं, एक line में होते हैं, और semicolon पर ख़त्म नहीं होते।

सभी directives एक नज़र में

Directiveउद्देश्य
#includeदूसरी file का content जोड़ना
#defineMacro define करना (नाम जो text से बदलता है)
#undefMacro definition हटाना
#if, #elif, #else, #endifCondition के आधार पर code रखना या छोड़ना
#ifdef, #ifndefMacro defined हो (या न हो) तभी code रखना
#errorMessage के साथ compilation रोकना
#pragmaCompiler-specific निर्देश, जैसे #pragma once
#lineबताए जाने वाले line numbers बदलना (कम इस्तेमाल)

#include

C++
#include <iostream>     // standard header
#include "marks.h"      // अपना header

< > और " " का अंतर और include guards कैसे काम करते हैं, देखें header files में।

#define: object-like macros

C++
#include <iostream>
#define SCHOOL "Alpine Public School"
#define MAX_STUDENTS 40

int main() {
    std::cout << SCHOOL << " allows " << MAX_STUDENTS << " students per class.\n";
    return 0;
}
Alpine Public School allows 40 students per class.

Compilation से पहले हर MAX_STUDENTS की जगह 40 लिख दिया जाता है। C++ में constexpr constant आमतौर पर बेहतर है क्योंकि उसका type होता है — देखें constants और literals।

Function-like macros और उनके जाल

C++
#define SQUARE(x) x * x
int a = SQUARE(3 + 1);   // बनता है 3 + 1 * 3 + 1  ->  7, 16 नहीं!

Parameter और पूरे body को हमेशा brackets में रखें:

C++
#define SQUARE(x) ((x) * (x))

फिर भी SQUARE(i++) में i दो बार बढ़ता है। constexpr function में ऐसी कोई समस्या नहीं:

C++
constexpr int square(int x) { return x * x; }

Conditional compilation

#if/#ifdef और #endif के बीच का code तभी compile होता है जब condition true हो। आम इस्तेमाल: debug output जिसे एक जगह से बंद किया जा सके:

C++
#include <iostream>
#define DEBUG 1

int main() {
    int total = 0;
    for (int m : {78, 85, 69}) {
        total += m;
#if DEBUG
        std::cout << "[debug] added " << m << ", total = " << total << "\n";
#endif
    }
    std::cout << "Total: " << total << "\n";
    return 0;
}
[debug] added 78, total = 78 [debug] added 85, total = 163 [debug] added 69, total = 232 Total: 232

DEBUG को 0 करते ही debug lines compiled program से पूरी तरह गायब हो जाती हैं। Macro command line से भी define कर सकते हैं: g++ -DDEBUG=1 main.cpp।

Platform के हिसाब से अलग code के लिए predefined macros काम आते हैं:

C++
#ifdef _WIN32
    system("cls");
#else
    system("clear");
#endif

#ifndef include guards और #pragma once

C++
#ifndef MARKS_H
#define MARKS_H
// ... declarations ...
#endif

पहली बार header include होने पर MARKS_H defined नहीं होता, इसलिए content इस्तेमाल होता है और MARKS_H define हो जाता है। बाद के हर include में वह छूट जाता है। #pragma once सभी बड़े compilers पर यही काम एक line में करता है।

#undef और #error

C++
#define LIMIT 10
#undef LIMIT          // अब LIMIT मौजूद नहीं

#if __cplusplus < 201703L
#error "This program needs C++17 or newer"
#endif

#error से build शुरुआत में ही साफ़ message के साथ रुक जाता है, बाद की उलझी errors की जगह।

Predefined macros

MacroValue
__LINE__मौजूदा line number
__FILE__मौजूदा file का नाम
__DATE__, __TIME__Compilation की तारीख़ और समय
__cplusplusC++ standard version, जैसे C++17 के लिए 201703L
C++
std::cout << "Compiled on " << __DATE__ << " (line " << __LINE__ << ")\n";

आम गलतियाँ

  • #define में semicolon लगाना (#define MAX 100;)।
  • Function-like macros में brackets भूलना।
  • #endif भूलना, जिससे "unterminated #if" error आती है।
  • जहाँ constexpr, inline functions या templates ज़्यादा साफ़ और type-safe हों, वहाँ macros लेना।
  • Macro नाम और ( के बीच space: #define SQ (x) ((x)*(x)) function-like नहीं, object-like macro बनाता है।
🏋️ अभ्यास

DEBUG switch वाला program लिखें जो factorial loop का हर step सिर्फ DEBUG 1 होने पर print करे। फिर file से #define हटाकर एक बार -DDEBUG=0 और एक बार -DDEBUG=1 से compile करें और output की तुलना करें।

सारांश

  • Preprocessor compilation से पहले चलता है और # वाली lines संभालता है।
  • #include files जोड़ता है; #define text-replacement macros बनाता है।
  • #if/#ifdef/#ifndef ... #endif code रखते या हटाते हैं — debug code, platforms और include guards के लिए।
  • Directives कभी semicolon पर ख़त्म नहीं होते।
  • C++ में macros की जगह constexpr और inline functions बेहतर हैं।

संबंधित: header files, inline functions, constants और literals।

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

C++ में preprocessor directive क्या है?
Preprocessor directive # से शुरू होने वाली line है जिसे compilation से पहले process किया जाता है। Preprocessor इन lines से files include करता है, macro नामों को text से बदलता है, और code के blocks रखता या हटाता है। फिर compiler सिर्फ बचे हुए text को देखता है।
C++ में macro और function में क्या अंतर है?
Macro को preprocessor text की तरह फैलाता है, इसलिए उसमें type checking नहीं होती और उसके arguments एक से ज़्यादा बार evaluate हो सकते हैं। Function compiled code है जिसके parameters typed होते हैं और ठीक एक बार evaluate होते हैं। Modern C++ में function-like macros की जगह constexpr या inline functions लें।
Conditional compilation क्या है?
Conditional compilation में #if, #ifdef, #ifndef, #elif, #else और #endif से compile करने से पहले code रखा या हटाया जाता है। यह include guards, सिर्फ debug वाले code, और Windows/Linux में अलग code के लिए इस्तेमाल होता है।
क्या preprocessor directive semicolon पर ख़त्म होता है?
नहीं। Directive line के अंत पर ख़त्म होता है। #define MAX 100; लिखने से semicolon replacement text में चला जाता है, इसलिए int a[MAX]; बनता है int a[100;]; और compile नहीं होता।
← Back to C++ Tutorial
🔗

Share this topic with a friend

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

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

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

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicCommand Line Arguments आपकी प्रगति: 34 / 81 (42%) · Lesson 35 / 81 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें