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

C Preprocessor परिचय

Preprocessor एक helper है जो compile होने से पहले आपके code text को तैयार करता है, find-and-replace करने और दूसरी files paste करने वाले एक editor की तरह। # से शुरू होने वाली lines इसके लिए हैं।
Syntax
c
#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?

c
// 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;
}

Header Expansion

यह effectively एक copy-paste operation है: पूरी target header file का text #include line की जगह insert हो जाता है, यही वजह है कि same header को दो बार include करना (बिना protection के) duplicate-definition errors cause कर सकता है।

उदाहरण: Header Expansion

c
// 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;
}

Macro Substitution

क्योंकि यह एक function call की बजाय pure text substitution है, macros में कोई type checking नहीं होती और complex arguments के साथ unexpectedly behave कर सकते हैं -- एक actual function इस्तेमाल करने के मुकाबले यह subtle bugs का एक common source है।

उदाहरण: Macro Substitution

c
// 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;
}

Conditional Preparation

यह आपको अलग platforms या configurations के लिए same source file के अलग versions compile करने देता है, code की अलग copies maintain किए बिना, compile time पर कुछ macros define या omit करके।

उदाहरण: Conditional Preparation

c
// 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;
}

Compilation Flow

यह intermediate expanded output (कभी-कभी -E जैसे एक compiler flag से visible) में कोई macros, comments, या conditional blocks नहीं बचते -- बस plain C code जो compiler के lexer और parser के process करने के लिए ready है।

उदाहरण: The Compilation Flow

c
// 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;
}
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. यह सोचना कि preprocessor compilation के बाद चलता है।
  2. preprocessor directives के बाद semicolons लगाना।
  3. यह भूल जाना कि macros text substitution हैं।
🔒

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.