C की Header Files
In this page:
#ifndef HEADER_NAME_H
#define HEADER_NAME_H
return_type function_name(parameter_type parameter); // declaration only
#endif
#include "header_name.h" // custom header in the .c file
Standard बनाम Custom Header Files
Standard headers, #include <stdio.h> जैसे angle brackets से include किए गए, compiler की अपनी library से आते हैं और इसके system include paths में search होते हैं, जबकि custom headers, #include "utils.h" जैसे double quotes से include किए गए, आपकी अपनी files हैं जो पहले current project के relative search होती हैं।
उदाहरण: Standard vs. Custom Header Files
// 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 is a standard header");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Header Guards
एक header guard एक header file के contents को #ifndef HEADER_NAME, #define HEADER_NAME, और एक closing #endif में wrap करता है, यह पक्का करते हुए कि अगर file current translation unit में पहले से एक बार include हो चुकी हो तो preprocessor इसके contents को पूरी तरह skip कर दे, duplicate-definition compile errors रोकते हुए।
उदाहरण: Header Guards
#ifndef MYHEADER_H
// Define the macro MYHEADER_H (replaced by the preprocessor)
#define MYHEADER_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 against duplicate inclusion");
// Return 0 to signal that the program finished successfully
return 0;
}
#endif
Login to try C/C++/Java/PHP code in the editor
Headers में Functions Declare करना
एक header file में declarations होने चाहिए — function prototypes, type definitions, macro constants — यह describe करते हुए कि क्या exist करता है, वह actual executable code नहीं जो उन functions को implement करता है; implementations की जगह एक corresponding .c source file में है।
उदाहरण: Declaring Functions in Headers
// Include standard input/output (printf, scanf)
#include <stdio.h>
int add(int a, int b);
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", add(2, 3));
// Return 0 to signal that the program finished successfully
return 0;
}
int add(int a, int b) {
// Send a + b back to the caller
return a + b;
}
Login to try C/C++/Java/PHP code in the editor
Headers में Variables और Macros
एक header में shared constants और macros declare करना इसे include करने वाली हर source file को exactly same values consistently इस्तेमाल करने देता है, इसलिए एक जगह (header में) एक shared setting बदलना automatically इस पर depend करने वाली हर file को update कर देता है, बिखरी duplicate definitions ढूंढने की बजाय।
उदाहरण: Variables and Macros in Headers
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro MAX_USERS (replaced by the preprocessor)
#define MAX_USERS 100
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", MAX_USERS);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Duplicate Definitions रोकना
अगर एक header में सिर्फ एक prototype की बजाय एक actual function body हो, और वह header एक से ज़्यादा .c file में include हो, linker same function को दो बार defined देखता है और एक duplicate-symbol error report करता है — यही exact failure है जिसे header guards और declaration-only headers avoid करने के लिए design किए गए हैं।
उदाहरण: Preventing Duplicate Definitions
// Include standard input/output (printf, scanf)
#include <stdio.h>
int square(int n) {
// Send n * n back to the caller
return n * n;
}
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", square(4));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- include guards भूल जाना, ताकि header दो बार process हो और redefinition errors cause करे।
- एक header में variables या functions define करना जिसे कई files include करती हैं, duplicate definitions cause करते हुए।
- अपने खुद के headers के लिए quotes की बजाय angle brackets इस्तेमाल करना।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: