Conditional Compilation using #ifdef

Introduction

C language में कभी-कभी ऐसी situation आती है जहाँ program का कुछ हिस्सा कभी compile करना होता है और कभी नहीं
ऐसे cases में conditional compilation का use किया जाता है।

#ifdef एक preprocessor directive है जो check करता है कि कोई macro defined है या नहीं
अगर macro defined है, तो उसके नीचे लिखा code compile होगा।
अगर macro defined नहीं है, तो वह code skip कर दिया जाएगा।

इसका use खासकर इन situations में होता है:

  • debugging code को temporarily enable/disable करने में
  • different versions of program manage करने में
  • platform specific code लिखने में
  • optional features को control करने में

Meaning of #ifdef

#ifdef का पूरा अर्थ है:

if defined

यानी:

अगर दिया गया macro defined है, तो नीचे का code include करो।

Syntax

#ifdef MACRO_NAME
// code
#endif

Working:

  • अगर MACRO_NAME defined है → code compile होगा
  • अगर MACRO_NAME defined नहीं है → code compile नहीं होगा

Example Program 1: Simple #ifdef

#include <stdio.h>
#define MESSAGE
int main()
{#ifdef MESSAGE
printf("Macro MESSAGE is defined\n");
#endif
printf("Program ended\n");

return 0;
}

Program Explanation

इस program में:

#define MESSAGE

का मतलब है कि MESSAGE macro define कर दिया गया है।

अब preprocessor जब यह देखता है:

#ifdef MESSAGE
printf("Macro MESSAGE is defined\n");
#endif

तो वह check करता है कि MESSAGE defined है या नहीं।

क्योंकि MESSAGE defined है, इसलिए यह line compile होगी:

printf("Macro MESSAGE is defined\n");

इसके बाद normal statement भी चलेगी:

printf("Program ended\n");

Output:

Macro MESSAGE is defined
Program ended

Example Program 2: Macro Defined न हो तो क्या होगा

#include <stdio.h>

int main() {

#ifdef TEST

printf("TEST is defined\n");
#endif

printf("Program ended\n");

return 0;
}

Program Explanation

इस program में TEST नाम का macro कहीं define नहीं किया गया है।

इसलिए preprocessor जब यह check करता है:

#ifdef TEST
printf("TEST is defined\n");
#endif

तो उसे पता चलता है कि TEST defined नहीं है।
इस वजह से यह पूरा block skip कर दिया जाता है।

केवल यह line compile होगी:

printf("Program ended\n");

Output:

Program ended

Example Program 3: Uses of Debugging me #ifdef

#include <stdio.h>
#define DEBUG

int main() {
int a = 10, b = 20, sum;

sum = a + b;

#ifdef DEBUG
printf("Debug: a = %d, b = %d, sum = %d\n", a, b, sum);

#endif
printf("Sum = %d\n", sum);

return 0;
}

Program Explanation

इस program में:

#define DEBUG

define किया गया है।
इसका मतलब debugging mode ON है।

Program पहले:

  • a = 10
  • b = 20
  • sum = 30

calculate करता है।

फिर #ifdef DEBUG check करता है कि DEBUG defined है या नहीं।
क्योंकि DEBUG defined है, इसलिए debug वाली line भी compile होगी।

Output:

Debug: a = 10, b = 20, sum = 30
Sum = 30

अगर DEBUG define न हो

अगर program में यह line हटा दें:

#define DEBUG

तो #ifdef DEBUG वाला block compile नहीं होगा।

तब output होगा:

Sum = 30

इससे debugging statements को easily on/off किया जा सकता है।

#ifdef with #else

हम #else का भी use कर सकते हैं।

Example Program 4:

#include <stdio.h>
#define MODE

int main()
{
#ifdef MODE
printf("Mode is ON\n");
#else
printf("Mode is OFF\n");
#endif

return 0;
}

Program Explanation

इस program में MODE defined है, इसलिए #ifdef MODE वाला भाग चलेगा और #else वाला skip होगा।

Output:

Mode is ON

अगर #define MODE हटा दिया जाए, तो output होगा:

Mode is OFF

#ifdef ka Practical Use

1. Debugging

Testing के समय extra information show करने के लिए

2. Feature Control

कुछ features को enable/disable करने के लिए

3. Platform Specific Code

Different systems के लिए अलग code compile करने के लिए

4. Development and Release Version

Development version में extra code compile कर सकते हैं, release version में skip कर सकते हैं

Related Directives

#ifdef के साथ ये directives भी important हैं:

#ifndef

अगर macro defined न हो, तब code compile करता है

#else

alternative code देता है

#endif

conditional block को end करता है

Important Points

  • #ifdef एक preprocessor directive है
  • यह compile होने से पहले काम करता है
  • इसका use macro defined होने की condition check करने के लिए होता है
  • यह runtime decision नहीं है, compile-time decision है

#ifdef vs if

Point#ifdefif
Work TimeCompile timeRun time
PurposeMacro check करनाCondition check करना
Checked ByPreprocessorCompiler / Program
UseCode include/skipProgram flow control

Conclusion

Conditional compilation using #ifdef C language का बहुत useful feature है।
यह programmer को यह control देता है कि program का कौन-सा हिस्सा compile होना चाहिए और कौन-सा नहीं।

यह खासकर debugging, feature control और platform specific programming में बहुत काम आता है।
अगर किसी macro के defined होने पर code चलाना हो, तो #ifdef सबसे आसान और useful तरीका है।

Leave a Comment

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

Scroll to Top