← Back to C Course | Chapter 11: Preprocessor | Lesson 4 of 5

C में Conditional Compilation

Conditional compilation आपको एक setting के आधार पर code के pieces include या skip करने देता है, optional steps वाली एक recipe की तरह। यह अलग systems या debug modes के लिए इस्तेमाल होता है।
Syntax
c
#ifdef MACRO_NAME
// compiled if MACRO_NAME is defined
#endif

#ifndef MACRO_NAME
#define MACRO_NAME
// compiled if MACRO_NAME is not defined
#endif

#ifdef इस्तेमाल करना

यह आमतौर पर debug-only code शामिल करने के लिए इस्तेमाल होता है, जैसे #ifdef DEBUG ... #endif blocks जो extra diagnostic information print करते हैं, जो release builds में (सिर्फ runtime पर skip नहीं, बल्कि) पूरी तरह compile out हो जाते हैं।

उदाहरण: Using #ifdef

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro DEBUG (replaced by the preprocessor)
#define DEBUG
// Program execution starts in main()
int main() {
	#ifdef DEBUG
	// Print formatted text to the screen
	printf("Debug info enabled");
	#endif
	// Return 0 to signal that the program finished successfully
	return 0;
}

#ifndef इस्तेमाल करना

यह include guards के पीछे का core mechanism है -- एक header अपने contents को #ifndef HEADER_NAME में wrap करता है ताकि same file में दूसरा (और कोई भी बाद का) inclusion guard macro को already defined देखे और content को पूरी तरह skip कर दे।

उदाहरण: Using #ifndef

c
#ifndef MY_HEADER_H
// Define the macro MY_HEADER_H (replaced by the preprocessor)
#define MY_HEADER_H
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("Guarded content");
	// Return 0 to signal that the program finished successfully
	return 0;
}
#endif

#if, #elif, और #else इस्तेमाल करना

यह runtime if/else if/else structure जैसा दिखता है लेकिन पूरी तरह compile time पर operate करता है, यह decide करने के लिए constant expressions evaluate करते हुए (अक्सर defined() checks शामिल) कि actually कौन सा code path final binary में compile होता है।

उदाहरण: Using #if, #elif, and #else

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro VERSION (replaced by the preprocessor)
#define VERSION 2
// Program execution starts in main()
int main() {
	#if VERSION == 1
	// Print formatted text to the screen
	printf("Version 1");
	#elif VERSION == 2
	// Print formatted text to the screen
	printf("Version 2");
	#else
	printf("Unknown");
	#endif
	// Return 0 to signal that the program finished successfully
	return 0;
}

Logic Flows में #undef इस्तेमाल करना

यह आपको testing purposes के लिए mid-file behavior toggle करने देता है -- same macro को अलग points पर undefine और redefine करना बदलता है कि same file में बाद के conditional blocks क्या include करेंगे।

उदाहरण: Using #undef in Logic Flows

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro MODE (replaced by the preprocessor)
#define MODE 1
// Program execution starts in main()
int main() {
	#if MODE
	// Print formatted text to the screen
	printf("Mode active ");
	#endif
	#undef MODE
	// Define the macro MODE (replaced by the preprocessor)
	#define MODE 0
	#if MODE
	// Print formatted text to the screen
	printf("unreachable");
	#else
	printf("Mode toggled off");
	#endif
	// Return 0 to signal that the program finished successfully
	return 0;
}

Header Include Guards

एक पूरे header के contents के चारों ओर wrapped three-directive pattern (#ifndef, #define, #endif) C में इतना universal convention है कि ज़्यादातर editors और IDEs एक नई header file बनाते समय इसे आपके लिए auto-generate कर सकते हैं।

उदाहरण: Header Include Guards

c
#ifndef POINT_H
// Define the macro POINT_H (replaced by the preprocessor)
#define POINT_H
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("Standard three-directive guard pattern");
	// Return 0 to signal that the program finished successfully
	return 0;
}
#endif
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक #ifdef या #if के लिए closing #endif भूल जाना, जो एक compile error cause करता है।
  2. #ifdef में एक macro नाम गलत spell करना, ताकि block चुपचाप skip हो जाए।
  3. एक ऐसे macro पर #if इस्तेमाल करना जो defined नहीं है, जहाँ इसे बिना किसी warning के 0 माना जाता है।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.