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

C का #include

#include दूसरी file के contents को आपकी में paste करता है, दूसरी book से एक chapter add करने जैसा। इसी तरह आप stdio.h जैसी libraries इस्तेमाल करते हैं।
Syntax
c
#include <standard_header.h>   // system headers
#include "custom_header.h"     // your own headers

Standard Headers Include करना

Angle brackets compiler को पहले इसकी predefined system include directories (जैसे /usr/include) में देखने को कहते हैं, जहाँ standard library के headers, जैसे stdio.h, actually आपके system पर रहते हैं।

उदाहरण: Including Standard Headers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("Uses angle brackets for system headers");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Custom Headers Include करना

Double quotes preprocessor को system directories पर fall back करने से पहले पहले current file की अपनी directory के relative search करने को कहते हैं -- यह standard library की बजाय आपके अपने project का हिस्सा होने वाले headers के लिए convention है।

उदाहरण: Including Custom Headers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("Custom headers use double quotes");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Nested Header Expansion

यह nested expansion गहरी chains बना सकता है -- एक header include करना कई और खींच सकता है, जो normal है, लेकिन यह भी है कि कैसे accidental circular includes (A, B include करे, जो A include करे) बिना proper guards के infinite expansion cause कर सकते हैं।

उदाहरण: Nested 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 itself may include other headers");
	// Return 0 to signal that the program finished successfully
	return 0;
}

कई Includes रोकना

Include guards के बिना, same struct या function को दो अलग include chains से दो बार define करना जो दोनों same header तक पहुँचती हैं एक redefinition compiler error cause करता है -- multi-file C projects में यह सबसे common beginner compilation errors में से एक है।

उदाहरण: Preventing Multiple Includes

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

Headers के लिए Best Practices

एक well-organized header आमतौर पर सिर्फ function prototypes, type definitions, और macro constants रखता है -- एक header में actual function code रखना 'multiple definition' linker errors cause करता है अगर वह header एक से ज़्यादा .c files में include हो।

उदाहरण: Best Practices for Headers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("Headers should hold prototypes, not function bodies");
	// 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. अपने header के लिए quotes की बजाय angle brackets इस्तेमाल करना, ताकि compiler system folders search करे और इसे न ढूंढे।
  2. include guards भूल जाना, जो एक header को दो बार include होने पर redefinition errors cause करता है।
  3. एक header में function definitions रखना जिसे कई files include करती हैं, link time पर duplicate definitions cause करते हुए।
🔒

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.