🟡 Intermediate  ·  Lesson 31

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 में रखें।

mymath.h
#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 में बाँटना

mymath.c
#include "mymath.h"
int add(int a, int b)      { return a + b; }
int multiply(int a, int b) { return a * b; }
main.c
#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;
}
Output:
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 देता।

💡 हमेशा guard जोड़ें

आपकी लिखी हर header एक unique नाम वाले include guard से शुरू होनी चाहिए (अक्सर file नाम बड़े अक्षरों में, जैसे MYMATH_H)।

आम गलतियाँ

  • Header में सिर्फ़ prototypes के बजाय पूरा function code रखना।
  • अपनी header के लिए " " के बजाय < > इस्तेमाल करना।
  • Include guard भूलना, जिससे redefinition errors होती हैं।
  • main.c के साथ मेल खाती .c file 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 बनाने के लिए सभी .c files साथ compile करें।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C में header file क्या है?
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 में क्यों रखते हैं?
इन्हें अलग करने से कई source files एक ही declarations को बिना copy किए साझा कर सकती हैं। .h file कहती है "ये functions मौजूद हैं और ऐसे दिखते हैं," जबकि मेल खाती .c file असली code रखती है। इससे बड़े programs व्यवस्थित रहते हैं और duplication टलता है।
Include guard क्या है और क्यों ज़रूरी है?
Include guard header के ऊपर #ifndef / #define / #endif pattern है। यह एक ही header को एक compile में एक से ज़्यादा बार include होने से रोकता है, जो वरना duplicate-definition errors देता।
क्या मैं C में अपनी header file बना सकता हूँ?
हाँ। आप अपने function prototypes और definitions एक .h file में लिखें, एक include guard जोड़ें, फिर उन्हें चाहने वाली किसी भी source file में #include "yourfile.h" इस्तेमाल करें। बड़े C projects files के बीच code इसी तरह साझा करते हैं।
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.