Preprocessor Directives
Preprocessor Directives कुछ विशेष प्रकार की Instructions होती है जो Compilation Step के पहले पूर्ण होती है, Preprocessor Directive Compilation का Part नहीं है, Preprocessor Directive Instruction # से शुरू होती है। Preprocessor Directive को हम Text Substitution Tool भी कहते है।
- Preprocessor एक प्रोसेस है जो Compilation के पहले होते है।
- सभी Preprocessor Directives # Symbol से ही शुरू होती है।
जब File को या Header File को प्रोग्राम में Include करते है तो #include लिखते है, यही तो एक Preprocessor Directive का उदाहरण है।
// preprocessor directive
#include <stdio.h>
int main() {
printf("Hi");
return 0;
}
#include directive
#include Preprocessor Directive का उपयोग File को प्रोग्राम में Include करने के लिए किया जाता है अब वह Normal File या Header File दोनों हो सकती है।
Header file include करने के लिए <stdio.h> या “stdio.h” लिख सकते है , लेकिन Normal Include करने के लिए केवल “filename.c” => Double Quotes में लिखना पड़ता है।
// preprocessor directives
// #include <stdio.h>
// same as
#include "stdio.h"
#include "myheaderfile.c"
int main(int argc, char const *argv[])
{
printf("Hi");
return 0;
}
#define directive
#define Directive एक प्रकार का Preprocessor Directive है, जिसे Macro भी कहते है। #define Directive में Macro का नाम एवं Value रहती है, प्रोग्राम में जहाँ-जहाँ Macro उपयोग किया है गया होगा वह Macro को उसकी Value द्वारा Replace कर दिया जाता है, Preprocessing के दौरान।
#include <stdio.h>
// macro
#define PI 3.14
int main() {
// area of circle pi*r*r
float r = 5;
float area = PI*r*r;
printf("%0.2fn",area); // two decimal place
return 0;
}
#pragma directive
#pragma Preprocessor Directive का उपयोग Compiler या Linker को Special Instruction देने के लिए किया जाता है।
#pragma GCC optimize("O3")
int main() {
// code
}
#ifdef directive
#ifdef Preprocessor Directive का उपयोग Macro Define है की नहीं उसको Check के लिए किया जाता है।
#include <stdio.h>
// macro
#define PI 3.14
#ifdef PI
printf("PI is definedn");
#endif
int main() {
// code
return 0;
}
#infdef directive
#ifndef Preprocessor Directive का उपयोग Macro Define नहीं है या है उसको Check के लिए किया जाता है।
#include <stdio.h>
#ifndef PI
printf("PI is not definedn");
#endif
int main() {
// code
return 0;
}
Summary
- File Inclusion:
#include
किसी दूसरी फाइल के कोड Current फाइल में सम्मलित करता है। - Macro Creation:
#define
and#undef
इन Directives का उपयोग Macro Create एवं Remove करने के लिए करते है। - Conditional Compilation:
#if
,#ifdef
, etc., Condition के अनुसार Compilation करने के लिए उपयोग होता है। - Error Handling:
- Line Control & Compiler Hints:
#line
and#pragma
अतिरिक्त Compile-Time निर्देश दे सकते सकते है।