← Back to C Course | Chapter 14: Additional Topics | Lesson 2 of 9

C की Header Files

एक header file एक अलग file है जो बताती है कि code क्या offer करता है, एक table of contents की तरह। दूसरी files उस code को इस्तेमाल करने के लिए इसे include करती हैं, और guards इसे दो बार include होने से रोकते हैं।
Syntax
c
#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

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 is a standard header");
	// Return 0 to signal that the program finished successfully
	return 0;
}

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

c
#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

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

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

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

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

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

c
// 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;
}
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. include guards भूल जाना, ताकि header दो बार process हो और redefinition errors cause करे।
  2. एक header में variables या functions define करना जिसे कई files include करती हैं, duplicate definitions cause करते हुए।
  3. अपने खुद के headers के लिए quotes की बजाय angle brackets इस्तेमाल करना।

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.