C Preprocessor परिचय
In this page:
#include <header.h>
#define NAME value
#ifdef NAME
// compiled only if NAME is defined
#endif
Preprocessor क्या है?
यह purely textual process है -- preprocessor को C syntax या semantics की कोई समझ नहीं है, यह बस असली compiler के code देखने से पहले pattern-based find-and-replace और file-inclusion operations perform करता है।
उदाहरण: What is the Preprocessor?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro GREETING (replaced by the preprocessor)
#define GREETING "Hello"
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf(GREETING);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Header Expansion
यह effectively एक copy-paste operation है: पूरी target header file का text #include line की जगह insert हो जाता है, यही वजह है कि same header को दो बार include करना (बिना protection के) duplicate-definition errors cause कर सकता है।
उदाहरण: Header Expansion
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("stdio.h contents are copied in before compiling");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Macro Substitution
क्योंकि यह एक function call की बजाय pure text substitution है, macros में कोई type checking नहीं होती और complex arguments के साथ unexpectedly behave कर सकते हैं -- एक actual function इस्तेमाल करने के मुकाबले यह subtle bugs का एक common source है।
उदाहरण: Macro Substitution
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro PI (replaced by the preprocessor)
#define PI 3.14
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%.2f", PI);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Conditional Preparation
यह आपको अलग platforms या configurations के लिए same source file के अलग versions compile करने देता है, code की अलग copies maintain किए बिना, compile time पर कुछ macros define या omit करके।
उदाहरण: Conditional Preparation
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro DEBUG (replaced by the preprocessor)
#define DEBUG 1
// Program execution starts in main()
int main() {
#if DEBUG
// Print formatted text to the screen
printf("Debug mode");
#endif
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Compilation Flow
यह intermediate expanded output (कभी-कभी -E जैसे एक compiler flag से visible) में कोई macros, comments, या conditional blocks नहीं बचते -- बस plain C code जो compiler के lexer और parser के process करने के लिए ready है।
उदाहरण: The Compilation Flow
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro VALUE (replaced by the preprocessor)
#define VALUE 5
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", VALUE);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- यह सोचना कि preprocessor compilation के बाद चलता है।
- preprocessor directives के बाद semicolons लगाना।
- यह भूल जाना कि macros text substitution हैं।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: