Preprocessor commands in C

Introduction

C language में preprocessor सिर्फ #define, #include और #ifdef तक ही सीमित नहीं है।
इसके अलावा भी कई महत्वपूर्ण commands होते हैं जो program को compile होने से पहले modify करते हैं।

ये commands code को flexible, controlled और efficient बनाने में मदद करते हैं।

1. #undef Directive

#undef का उपयोग किसी पहले से defined macro को हटाने (undefine) के लिए किया जाता है।

Syntax:

#undef MACRO_NAME

Example Program:

#include <stdio.h>
#define VALUE 10

int main()
{
printf("Before undef: %d\n", VALUE);

#undef VALUE
// अब VALUE undefined हो गया है
// printf("%d", VALUE); ❌ error देगा

printf("VALUE is undefined now\n");
return 0;
}

Explanation

  • पहले VALUE define किया गया है (10)
  • printf() में VALUE की value print होती है
  • फिर #undef VALUE से macro हटा दिया जाता है
  • उसके बाद VALUE का use करने पर error आएगा

2. #if, #elif, #else Directive

इनका उपयोग conditions के आधार पर code compile करने के लिए किया जाता है।

Syntax:

#if condition
// code
#elif condition
// code
#else
// code
#endif

Example Program:

#include <stdio.h>
#define NUM 10int main() {#if NUM > 0
printf("Number is positive\n");
#elif NUM == 0
printf("Number is zero\n");
#else
printf("Number is negative\n");
#endif return 0;
}

Explanation

  • NUM = 10 define किया गया है
  • #if NUM > 0 true है
  • इसलिए “Number is positive” print होगा
  • बाकी conditions ignore हो जाएँगी

Output:

Number is positive

3. #pragma Directive

#pragma compiler को special instructions देने के लिए उपयोग किया जाता है।
यह compiler-specific होता है (हर compiler में अलग behavior हो सकता है)।

Example Program:

#include <stdio.h>

#pragma warning(disable:4996)

int main() {
printf("Pragma example\n");
return 0;
}

Explanation

  • #pragma warning(disable:4996) compiler warning को disable करता है
  • यह mostly specific compilers (जैसे MSVC) में use होता है

4. #error Directive

अगर program में कोई condition गलत हो, तो हम compile time पर error generate कर सकते हैं।

Example Program:

#define AGE 15

#if AGE < 18
#error "Age must be 18 or above"
#endif

int main() {
return 0;
}

Explanation

  • AGE = 15 है
  • condition AGE < 18 true है
  • इसलिए compiler error देगा और program compile नहीं होगा

यह validation के लिए useful है

5. #line Directive

यह compiler को line number और file name बदलने की अनुमति देता है।

Example Program:

#include <stdio.h>

int main() {

printf("Line number before: %d\n", __LINE__);

#line 100

printf("Line number after: %d\n", __LINE__);

return 0;
}

Explanation

  • पहले actual line number print होगा
  • #line 100 के बाद compiler line count को 100 मान लेगा
  • अगली line 100 के रूप में दिखेगी

Summary of Other Commands

DirectiveUse
#undefMacro हटाने के लिए
#ifCondition check करने के लिए
#elifAlternative condition
#elseDefault case
#pragmaCompiler-specific instruction
#errorCustom error generate करना
#lineLine number control करना

C Preprocessor

Introduction

C Preprocessor एक ऐसा tool है जो compiler से पहले source code को process करता है।

Flow:

Source Code → Preprocessor → Compiler → Output

यह program को compile होने से पहले modify करता है।

Preprocessor ke Main Features

1. Macro Expansion

#define के through values replace करना

2. File Inclusion

#include के द्वारा files जोड़ना

3. Conditional Compilation

#ifdef, #if के द्वारा code control करना

4. Code Control

Compilation के दौरान code include/skip करना

Example: Preprocessor ka Real Work

#define A 10

int main() {
int x = A;
}

Preprocessor इसे ऐसे बदल देगा:

int main() {
int x = 10;
}

Important Points

  • Preprocessor compilation से पहले काम करता है
  • यह text replacement करता है
  • इसमें type checking नहीं होती
  • यह C language का powerful feature है

Conclusion

Other preprocessor commands जैसे #undef, #if, #pragma, #error आदि program को अधिक powerful और flexible बनाते हैं।

साथ ही, C Preprocessor पूरे compilation process का एक महत्वपूर्ण हिस्सा है, जो code को modify करके compiler के लिए तैयार करता है।

Leave a Comment

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

Scroll to Top