Preprocessor Directives

Preprocessor directives कुछ विशेष प्रकार की instructions होती है जो compilation step के पहले पूर्ण होती है, preprocessor directive compilation का part नहीं है, preprocessor directive instruction # से शुरू होती है। preprocessor directive को हम text substitution tool भी कहते है।

  • सभी 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.2f\n",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 defined\n");
#endif

int main() {
    // code
    return 0;
}

#infdef directive

#ifndef preprocessor directive का उपयोग macro define नहीं है या है उसको check के लिए किया जाता है।

#include <stdio.h>

#ifndef PI
    printf("PI is not defined\n");
#endif

int main() {
    // code
    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *