File Inclusion Directive
#include एक File Inclusion Directive है। इस Directive का प्रयोग करके हम एक Source File में अन्य Source Files या Program File को जोडते हैं। जब Program काफी बडा होता है, तब हम Program को कई भागों में बांट कर, कई Source Files बना लेते हैं और आवश्यकतानुसार किसी भी File को #include Directive द्वारा Main() Source File में जोड लेते हैं। जैसे कि हम हमारे Program में Input Output से सम्बंधित Functions को प्रयोग कर सकें, इसलिए #include<stdio.h> नाम की Header File को main() Program File में Include करते हैं।
Conditional Compilations
कई बार हम चाहते हैं कि हमारी आवश्यकता के अनुसार कुछ Statement को Execute किया जाए और कुछ को छोड दिया जाए। ऐसी जरूरतों को पूरा करने के लिए “C” में कुछ अन्य Macros बनाए गए हैं। ये निम्नानुसार हैं-
#ifdef
#endif
ये Directive “C” Compiler को बताता है कि यदि Macro Define किया गया है तो Macro के बाद के Statements को भी Execute करे अन्यथा #ifdef व #endif के बीच के Statements को Compile ना करे व शेष Program को Compile कर दे। इसका syntax निम्नानुसार होता है-
#include<header file> #define Macro template name main() { Variables Declaration; clrscr(); Statement 1; Statement 2; Statement 3; #ifdef Macro template name Statement 4; Statement 5; Statement 6; #endif Statement 7; Statement 8; }
यदि Macro define किया गया है, तो Statement 4, 5 व 6 भी Compile होंगे अन्यथा “C” Compiler इन Statement को Compile नहीं करेगा। ये Macro बिल्कुल if Condition की तरह काम करता है। इसका प्रयोग हम निम्नानुसार किसी File में कर सकते हैं:
// Program #include <stdio.h> #define MAX 100 void main(void) { #if MAX > 99 printf("You have defined the MAX Macro Greater than 100"); #endif }
कई बार ऐसी जरूरत होती है कि हमने यदि Macro define किया है, तो Statements 4, 5 व 6 का Execution ना हो और यदि Macro define नहीं किया हो तो ये Statements 4, 5 व 6 Compile हो जाएं। ऐसी स्थिति में हम #ifndef ( if not defined ) Macro का प्रयोग करते हैं और इसका Syntax निम्नानुसार होता है-
#include<header file> #define Macro template name main() { Variables Declaration; clrscr(); Statement 1; Statement 2; Statement 3; #ifndef Macro template name Statement 4; Statement 5; Statement 6; #endif Statement 7; Statement 8; }
यदि हम इस Concept को भी Implement करना चाहें, तो निम्नानुसार कर सकते हैं:
// Program #include <stdio.h> #define MAX 100 void main(void) { #ifndef MAX printf("You have defined the MAX Macro Greater than 100"); #else printf("You have defined the MAX Macro Less than 100"); #endif }
यदि हम इस Program को Execute करें तो हमें Output में if() Statement का Message दिखाई देगा। लेकिन यदि हम MAX को Define ना करें, तो हमें Output में else Statement का Message दिखाई देगा।
इसी प्रकार से हम विभिन्न प्रकार से Compiler को अपनी Source File को Compile करने से पहले ही विभिन्न प्रकार के Macros द्वारा ये बता सकते हैं, कि उसे किस Code Block को Execute करना है और किसे नहीं करना है। जिस प्रकार से हम if व else का प्रयोग करते है, उसी प्रकार से हम #else Macro को भी प्रयोग कर सकते हैं। जैसे निम्न syntax देखें-
#include<header file> #define Macro template name main() { Variables Declaration; clrscr(); Statement 1; Statement 2; Statement 3; #ifdef Macro template name Statement 4; Statement 5; #else Statement 6; #endif Statement 7; Statement 8; }
यहां यदि Macro define किया गया हो तो Statement 4 व 5 का Compilation होगा और यदि Macro define ना किया गया हो तो Statement 6 का Compilation होगा। जिस प्रकार से हम if व else…if Condition को Use करते हैं उसी प्रकार से हम #if व #elseif Condition को भी use कर सकते हैं और इनके काम करने का तरीका भी बिल्कुल उसी प्रकार का है, जिस प्रकार का if व else if का है। निम्न syntax देखें-
#include<header file> #define Macro template name main() { Variables Declaration; clrscr(); Statement 1; Statement 2; #if Condition Statement 3; #else #if condition Statement 4; #else #if Condition Statement 5; #else Statement 6; #endif #endif #endif Statement 7; Statement 8; }
इसी Macro definition को हम दूसरे तरीके से निम्नानुसार भी लिख सकते हैं:
#include<header file> #define Macro template name main() { Variables Declaration; clrscr(); Statement 1; Statement 2; #if Condition Statement 3; #elif Condition Statement 4; #elif Condition Statement 5; #else Statement 6; #endif Statement 7; Statement 8; }
इस प्रकार से हम एक ही Program को ऐसा बना सकते हैं, कि वही Program किसी खास Macro template को define होने पर दूसरा काम करे और Macro के define ना होने पर कोई दूसरा।
हमें कई बार ऐसी जरूरतें पडती हैं, कि Program के कुछ Statements को बदलना पडता है, लेकिन किसी कारणवश हमें वापस वही पुराने Statements की जरूरत पड जाती है, जिन्हें हम Delete कर चुके होते हैं। ऐसे में वापस हमें वे सारे Statements लिखने पडते हैं, जो कि एक Boring काम होता है।
हम Macro का प्रयोग उस समय करके Statement को delete करने के बजाय ऐसा कर सकते हैं कि Statements तो Program में रहेंगे लेकिन वे Compiler ही नहीं होंगे। इस प्रकार से हमारे समय की काफी बचत हो सकती है और हम उच्च स्तरीय Program लिख सकते हैं। (Conditional Compilation and File Inclusion)
Buy this eBook for Complete Discussion about
#error
Function And Macros
Build Process
ये Article इस वेबसाईट पर Selling हेतु उपलब्ध EBook C Programming Language in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी रहा, तो निश्चित रूप से ये पुस्तक भी आपके लिए काफी उपयोगी साबित होगी।
C Programming Language in Hindi | Page: 477 + 265 | Format: PDF