C का #include
In this page:
#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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Custom Headers Include करना
Double quotes preprocessor को system directories पर fall back करने से पहले पहले current file की अपनी directory के relative search करने को कहते हैं -- यह standard library की बजाय आपके अपने project का हिस्सा होने वाले headers के लिए convention है।
उदाहरण: Including Custom Headers
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
कई 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
// 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
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- अपने header के लिए quotes की बजाय angle brackets इस्तेमाल करना, ताकि compiler system folders search करे और इसे न ढूंढे।
- include guards भूल जाना, जो एक header को दो बार include होने पर redefinition errors cause करता है।
- एक 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: