Header Files
Header files क्या हैं?
Header file .h में खत्म होने वाली file है जो declarations रखती है — जैसे function prototypes, macros और type definitions। यहाँ आप पूरा चलने वाला code नहीं रखते; आप वे "घोषणाएँ" रखते हैं जो compiler को बताती हैं कि क्या मौजूद है।
Header को program में #include से लाते हैं। जब आप #include <stdio.h> लिखते हैं, आप printf, scanf आदि के declarations उधार ले रहे हैं ताकि उन्हें इस्तेमाल कर सकें। (#include कैसे काम करता है, देखें preprocessor directives।)
Standard header files
C कई तैयार headers के साथ आता है। कुछ जो आप लगातार इस्तेमाल करेंगे:
| Header | क्या देता है |
|---|---|
<stdio.h> | Input/output: printf, scanf, files |
<stdlib.h> | Memory, conversions: malloc, atoi |
<string.h> | String functions: strlen, strcpy |
<math.h> | गणित: sqrt, pow, sin |
अपनी header file बनाना
आप अपने functions साझा करने के लिए अपनी header बना सकते हैं। Prototypes को .h file में रखें।
#ifndef MYMATH_H #define MYMATH_H int add(int a, int b); // sirf prototype int multiply(int a, int b); #endif
फिर असली code मेल खाती .c file में लिखें, और उन्हें अपने main program से इस्तेमाल करें।
Program को कई files में बाँटना
#include "mymath.h"
int add(int a, int b) { return a + b; }
int multiply(int a, int b) { return a * b; }#include <stdio.h>
#include "mymath.h" // aapki apni header, quotes me
int main() {
printf("%d\n", add(3, 4));
printf("%d\n", multiply(3, 4));
return 0;
}7
12
दोनों को साथ compile करें, जैसे gcc main.c mymath.c -o app। Header main.c को ऐसे functions इस्तेमाल करने देता है जिनका code दूसरी file में है।
Include guards
जो #ifndef ... #define ... #endif lines आपने देखीं वे एक include guard हैं। ये header को दो बार पेस्ट होने से रोकती हैं, जो वरना "redefinition" errors देता।
आपकी लिखी हर header एक unique नाम वाले include guard से शुरू होनी चाहिए (अक्सर file नाम बड़े अक्षरों में, जैसे MYMATH_H)।
आम गलतियाँ
- Header में सिर्फ़ prototypes के बजाय पूरा function code रखना।
- अपनी header के लिए
" "के बजाय< >इस्तेमाल करना। - Include guard भूलना, जिससे redefinition errors होती हैं।
main.cके साथ मेल खाती.cfile compile करना भूलना।
int square(int) के prototype वाली utils.h, उसके code वाली utils.c, और उसे इस्तेमाल करने वाली main.c बनाएँ। Include guard जोड़ें और सब साथ compile करें।
सारांश
- Header file (
.h) declarations रखती है, पूरा code नहीं। - Standard headers के लिए
< >और अपनी के लिए" "इस्तेमाल करें। - Prototypes
.hमें, असली code मेल खाती.cमें रखें। - Double inclusion टालने के लिए हमेशा include guard जोड़ें।
- Program बनाने के लिए सभी
.cfiles साथ compile करें।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C में header file क्या है?
.h में खत्म होने वाली एक file है जिसमें declarations — function prototypes, macros, और type definitions — होते हैं जिन्हें आप कई source files में साझा करना चाहते हैं। इसकी सामग्री #include से program में लाते हैं, ताकि compiler को उन functions के इस्तेमाल से पहले उनकी जानकारी हो।#include में angle brackets और quotes में क्या अंतर है?
#include <file.h> compiler को standard system directories में देखने को कहता है, जो stdio.h जैसे library headers के लिए है। #include "file.h" पहले आपके अपने project folder में देखने को कहता है, जो आपके खुद लिखे headers के लिए है।हम declarations header में और code .c file में क्यों रखते हैं?
.h file कहती है "ये functions मौजूद हैं और ऐसे दिखते हैं," जबकि मेल खाती .c file असली code रखती है। इससे बड़े programs व्यवस्थित रहते हैं और duplication टलता है।Include guard क्या है और क्यों ज़रूरी है?
#ifndef / #define / #endif pattern है। यह एक ही header को एक compile में एक से ज़्यादा बार include होने से रोकता है, जो वरना duplicate-definition errors देता।क्या मैं C में अपनी header file बना सकता हूँ?
.h file में लिखें, एक include guard जोड़ें, फिर उन्हें चाहने वाली किसी भी source file में #include "yourfile.h" इस्तेमाल करें। बड़े C projects files के बीच code इसी तरह साझा करते हैं।