C में Conditional Compilation
In this page:
#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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
#ifndef इस्तेमाल करना
यह include guards के पीछे का core mechanism है -- एक header अपने contents को #ifndef HEADER_NAME में wrap करता है ताकि same file में दूसरा (और कोई भी बाद का) inclusion guard macro को already defined देखे और content को पूरी तरह skip कर दे।
उदाहरण: Using #ifndef
#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
Login to try C/C++/Java/PHP code in the editor
#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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Logic Flows में #undef इस्तेमाल करना
यह आपको testing purposes के लिए mid-file behavior toggle करने देता है -- same macro को अलग points पर undefine और redefine करना बदलता है कि same file में बाद के conditional blocks क्या include करेंगे।
उदाहरण: Using #undef in Logic Flows
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Header Include Guards
एक पूरे header के contents के चारों ओर wrapped three-directive pattern (#ifndef, #define, #endif) C में इतना universal convention है कि ज़्यादातर editors और IDEs एक नई header file बनाते समय इसे आपके लिए auto-generate कर सकते हैं।
उदाहरण: Header Include Guards
#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
Login to try C/C++/Java/PHP code in the editor
- एक
#ifdefया#ifके लिए closing#endifभूल जाना, जो एक compile error cause करता है। #ifdefमें एक macro नाम गलत spell करना, ताकि block चुपचाप skip हो जाए।- एक ऐसे macro पर
#ifइस्तेमाल करना जो defined नहीं है, जहाँ इसे बिना किसी warning के 0 माना जाता है।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: