Preprocessor Directives in C++ प्रीप्रोसेसर डायरेक्टिव्स
Preprocessor directives are special instructions that are processed by the preprocessor before the actual compilation of a C++ program. These directives begin with the # symbol and are used to include header files, define macros, and control conditional compilation. Preprocessor directives are not terminated by a semicolon (;). They are generally written on separate lines. The # symbol must be the first non-whitespace character on the directive line.
प्रीप्रोसेसर डायरेक्टिव्स विशेष निर्देश होते हैं, जिन्हें C++ प्रोग्राम के वास्तविक कम्पाइलेशन से पहले प्रीप्रोसेसर द्वारा प्रोसेस किया जाता है। ये निर्देश # चिन्ह से शुरू होते हैं तथा इनका उपयोग Header Files को शामिल करने, Macros बनाने और Conditional Compilation को नियंत्रित करने के लिए किया जाता है। प्रीप्रोसेसर डायरेक्टिव्स के अंत में सेमीकोलन (;) नहीं लगाया जाता है। इन्हें सामान्यतः अलग-अलग लाइनों में लिखा जाता है। डायरेक्टिव वाली लाइन में # चिन्ह से पहले केवल Whitespace हो सकता है।
Important Preprocessor Directives (महत्वपूर्ण प्रीप्रोसेसर डायरेक्टिव्स):
1. #include Directive
The #include directive is used to include the contents of a header file in a C++ program. Header files provide declarations and functionality for input-output operations, mathematical calculations, string handling, and other operations.
#include डायरेक्टिव का उपयोग C++ प्रोग्राम में किसी Header File की सामग्री को शामिल करने के लिए किया जाता है। Header Files इनपुट-आउटपुट, गणितीय गणनाओं, स्ट्रिंग हैंडलिंग तथा अन्य कार्यों के लिए आवश्यक सुविधाएँ प्रदान करती हैं।
Syntax:
#include <header_file>OR
#include "header_file"There are two commonly used forms:
Angle brackets
< >: Generally used for standard library header files.Double quotation marks
" ": Generally used for user-defined header files. The compiler searches the specified file according to its include-search rules.
इन दोनों का उपयोग अलग-अलग प्रकार की Header Files को शामिल करने के लिए किया जाता है:
Angle Brackets
< >: सामान्यतः Standard Library की Header Files के लिए उपयोग किए जाते हैं।Double Quotation Marks
" ": सामान्यतः Programmer द्वारा बनाई गई Header Files के लिए उपयोग किए जाते हैं।
Example:
#include <iostream>int main(){std::cout << "Welcome to C++ Programming";return 0;}
Output:
Welcome to C++ ProgrammingHere, the iostream header file provides the declarations required for input and output operations using std::cin and std::cout.
यहाँ iostream Header File को शामिल किया गया है, जो std::cin और std::cout की सहायता से Input और Output operations करने के लिए आवश्यक declarations प्रदान करती है।
2. #define Directive
The #define directive is used to define macros. A macro is an identifier that is replaced by its specified replacement text before the actual compilation of the program.
#define डायरेक्टिव का उपयोग Macros बनाने के लिए किया जाता है। Macro एक ऐसा Identifier होता है, जिसे प्रोग्राम के वास्तविक कम्पाइलेशन से पहले उसके निर्धारित Replacement Text से बदल दिया जाता है।
Syntax:
#define macro_name replacement_textExample: Program to Calculate the Area of a Circle
#include <iostream>using namespace std;#define PI 3.14159int main(){float r, area;cout << "Enter radius: ";cin >> r;area = PI * r * r;cout << "Area of Circle is: " << area << endl;return 0;}
Output:
Enter radius: 5Area of Circle is: 78.5397
Explanation (व्याख्या):
In this program, PI is defined as a macro using the #define directive. Before compilation, the preprocessor replaces every occurrence of PI with 3.14159.
इस प्रोग्राम में #define डायरेक्टिव की सहायता से PI नाम का Macro बनाया गया है। कम्पाइलेशन से पहले प्रीप्रोसेसर प्रोग्राम में जहाँ भी PI लिखा होता है, उसे 3.14159 से बदल देता है।
Note: A #define macro performs text substitution. It is not a C++ variable or a typed constant. In modern C++, constexpr can also be used to define typed compile-time constants.
ध्यान दें: #define Macro केवल Text Substitution करता है। यह C++ Variable या निश्चित Data Type वाला Constant नहीं होता है। Modern C++ में Typed Compile-Time Constants बनाने के लिए constexpr का उपयोग भी किया जा सकता है।
3. Conditional Compilation Directives
Conditional compilation directives are used to compile a particular section of a program only when a specified condition is satisfied.
कंडीशनल कम्पाइलेशन डायरेक्टिव्स का उपयोग प्रोग्राम के किसी विशेष भाग को तभी कम्पाइल करने के लिए किया जाता है, जब दी गई Condition सत्य होती है।
Some important conditional directives are:
(a) #ifdef
The #ifdef directive checks whether a particular macro has been defined.
यह जाँचता है कि दिया गया Macro पहले से Define किया गया है या नहीं।
Syntax:
#ifdef macro_name// Statements#endif
(b) #ifndef
The #ifndef directive checks whether a particular macro has not been defined.
यह जाँचता है कि दिया गया Macro Define नहीं किया गया है या नहीं।
Syntax:
#ifndef macro_name// Statements#endif
(c) #if
The #if directive checks a constant expression. If the expression is true (non-zero), the specified section is included in compilation.
यह किसी Constant Expression की जाँच करता है। यदि Expression का परिणाम True (Non-zero) होता है, तो संबंधित कोड कम्पाइलेशन में शामिल किया जाता है।
Syntax:
#if condition// Statements#endif
(d) #else
The #else directive specifies an alternative section when the preceding condition is false.
जब पहले दी गई Condition False होती है, तब #else के अंतर्गत लिखा गया कोड शामिल किया जाता है।
(e) #endif
The #endif directive marks the end of a conditional compilation block.
यह Conditional Compilation Block के अंत को दर्शाता है।
Example:
#include <iostream>using namespace std;#define VERSION 1int main(){#if VERSION == 1cout << "Version 1 is selected";#elsecout << "Another version is selected";#endifreturn 0;}
Output:
Version 1 is selectedExplanation (व्याख्या):
In this program, VERSION is defined with the value 1. The #if directive checks whether VERSION == 1. Since the condition is true, the first message is included in the program.
इस प्रोग्राम में VERSION का मान 1 निर्धारित किया गया है। #if डायरेक्टिव जाँचता है कि VERSION == 1 है या नहीं। Condition True होने के कारण पहला Message प्रोग्राम में शामिल किया जाता है।
Other Useful Preprocessor Directives
Some other useful preprocessor directives are:
#undef: Used to undefine a previously defined macro.
#elif: Used to check another condition when the previous condition is false.
#pragma: Provides compiler-specific instructions.
कुछ अन्य उपयोगी प्रीप्रोसेसर डायरेक्टिव्स निम्न हैं:
#undef: पहले से Define किए गए Macro को Undefine करने के लिए।
#elif: पिछली Condition False होने पर दूसरी Condition की जाँच करने के लिए।
#pragma: Compiler को विशेष निर्देश देने के लिए।
Preprocessor directives are an important part of C++ programming. They help programmers include header files, define macros, and control conditional compilation. These directives make programs easier to organize and maintain.
प्रीप्रोसेसर डायरेक्टिव्स C++ प्रोग्रामिंग का एक महत्वपूर्ण भाग हैं। इनकी सहायता से Programmer Header Files को शामिल कर सकते हैं, Macros बना सकते हैं तथा Conditional Compilation को नियंत्रित कर सकते हैं। ये प्रोग्राम को व्यवस्थित और Maintain करने में सहायता करते हैं।
Comments
Post a Comment