YOUR C LEARNING COMPANION

Kahan se padhein?

Learning path
Program bananaChapter 41 of 73

Headers: interface share karna

Self-contained header padhein, include guards samjhein aur shared declarations ko program definitions se alag karein.

5 min readIntermediateC17 baseline
Offline reading
Is chapter mein8 sections

Kai source files ko ek interface use karna ho toh matching declarations chahiye. Header woh declarations share karta hai. Header mein kya hona chahiye aur file include karna separately linked library kyun nahi banata, yeh samjhenge.

1. Interface ki description share karein

Header commonly function declarations, type definitions, macros aur selected inline helpers supply karta hai. Structure type definition type describe karti hai; khud global structure object allocate nahi karti. Function prototype compiler ko call check karne ki information deta hai; khud function body supply ya implementation link nahi karta.

Neeche ke three files milkar small C17 program banate hain. Public contract: null point origin nahi, non-null argument live readable struct CodedilePoint ko point kare, aur function kuch change nahi karega. Header <stdbool.h> include karta hai kyunki C17 interface bool use karta hai.

C17 · project file · point.h
#ifndef CODEDILE_POINT_H
#define CODEDILE_POINT_H

#include <stdbool.h>

struct CodedilePoint { int x; int y; };
bool codedile_is_origin(const struct CodedilePoint *point);

#endif

2. Guard ek preprocessing translation unit protect karta hai

Include guard first processing par unique macro define karta hai. Same preprocessing translation unit ki later inclusions guarded contents skip karti hain. Do headers ke through same third header ki indirect inclusion bhi handle hoti hai. Project-specific naam use karein aur complete interface guard ke andar rakhein.

Guard separately translated .c files ke across shared macro state nahi banata. Har file ka apna preprocessing context hai. Isliye header mein accidentally defined externally linked object ki multiple definitions guard fix nahi karta. #pragma once many implementations ka widely supported alternative hai, lekin portable include-guard semantics wala standard C directive nahi.

3. Implementation apna interface include kare

C17 · project file · point.c
#include "point.h"
#include <stddef.h>

bool codedile_is_origin(const struct CodedilePoint *point)
{
    return point != NULL && point->x == 0 && point->y == 0;
}

Apna header include karne se compiler function definition ko public declaration se compare kar sakta hai. Implementation apne NULL use ke liye <stddef.h> include karti hai. Self-contained header apni declarations ki dependencies provide karta hai; callers se include order guess nahi karwata. Implementation ke har used header ko public header mein include karna required nahi.

4. Har caller mein interface include karein

C17 · project file · main.c
#include "point.h"
#include "point.h"
#include <assert.h>
#include <stddef.h>

int main(void)
{
    struct CodedilePoint start = { .x = 0, .y = 0 };
    assert(codedile_is_origin(&start));
    start.x = 1;
    assert(!codedile_is_origin(&start));
    assert(!codedile_is_origin(NULL));
    return 0;
}

Repeated include deliberately guard check karta hai. Is source file aur point.c ko same type aur prototype milte hain; dono separately compile hokar link hote hain. Normally header include karein, #include "point.c" nahi. Implementation file include karne se definitions current translation unit mein insert hoti hain; build us implementation ko separately compile bhi kare toh definitions duplicate ho sakti hain.

5. Samjhein kaunsi definitions aise share ho sakti hain

Header contentsReason
extern int codedile_count;Object declare karta hai jiska one external definition elsewhere hai.
int codedile_count = 0;External object define karta hai; broadly included header mein usually conflicting program definitions banengi.
static const int codedile_limit = 10;Header use karne wali har translation unit mein separate internally linked object banata hai.
static inline helperHar translation unit ko apna internally linked function deta hai; call expansion optional hai.

C17 mein file-scope object declaration ka storage-class specifier na ho toh linkage external hoti hai; sirf const isko nahi badalta. Isliye shared header ka const int limit = 10; conflicting external definitions bana sakta hai. Plain inline external definitions repeat karne ki universal permission nahi; C ke specific inline/external-definition rules hain. Normal declaration aur one source definition, ya deliberately chosen static inline helper prefer karein.

#include <...> aur #include "..." ki search details standard implementation par chhodta hai. GCC quoted include ke liye normally including file ke relative pehle search karta hai, phir configured paths; flags searches change kar sakte hain. Standard header ordinary physical file hona required nahi. Ek tool ki folder conventions ko language guarantee na samjhein.

Primary references aur aage ki reading

Aage badhne se pehle

  • Headers translation ke liye interface information share karte hain.
  • Guards translation unit mein repeated inclusion rok sakte hain.
  • Header declarations aur linkage rules se allowed definitions ke around design hona chahiye.
In ideas ko alag rakhein
Include guard / One program-wide definition
Guard preprocessing ke andar apply hota hai; different translation units ki ordinary external definitions merge nahi karta.
Apne words mein samjhaayein

Header ko independently include karne layak rakhne ke liye use kya include karna chahiye?

Optional: bolkar samjhein ya note rakhein. Tayyar hon toh aage badh sakte hain.

Apni understanding check karein

Optional hai. Answers save ya send nahi hote; chapter kabhi lock nahi hota.

Shared function declarations header mein kyun rakhein?
Har choice ki explanation padhein
  1. Header include karna hamesha library link karta hai.

    Header inclusion aur compiled definitions supply karna alag build concerns hain.

  2. Header alag running program hai.

    Headers inclusion se source dete hain; independent programs ki tarah run nahi hote.

  3. Translation units ke beech consistent interface share karne ke liye.

    ✓ Sahi reasoning. Declarations functions ke uses aur definitions check karne wali files mein include ho sakti hain.

C17

Earlier edition ka quick reference

Existing section links aur saved passages ke liye retained. Expanded explanation upar hai; documented corrections source review mein recorded hain.

Headers declarations, type definitions, macros aur interfaces share karte hain. Header mein extern int count; aur ek source file mein int count = 0; jaisi single definition rakhein. C mein sirf const file-scope object ko internal linkage nahi deta: header mein const int limit = 10; multiple external definitions bana sakta hai. static const se har translation unit ko apna object milta hai; static inline helpers common header pattern hain. Plain inline ke additional external-definition rules hain; yeh blanket exception nahi.

Include Guards

Include guard guarded header contents ko ek preprocessing translation unit mein baar-baar process hone se rokta hai, doosre headers ke through indirect inclusion mein bhi. Yeh separately compiled source files ke external-definition conflicts ko link time par nahi rokta.

C17 · illustrative fragment
#ifndef MYHEADER_H
#define MYHEADER_H
/* yahan declarations aayenge */
#endif

ya phir chhota, non-standard lekin almost universally supported #pragma once.

System vs. User Headers

C standard header-search locations ko implementation-defined chhodta hai. GCC mein quoted includes normally including file ki directory pehle search karte hain (zaroori nahi shell ki current working directory ho), phir configured quote/include paths. Angle-bracket includes configured include paths use karte hain. Compiler options dono searches badal sakte hain. Standard header ka physical .h file hona bhi zaroori nahi.

Standard Headers ka Overview

Header Purpose
<stdio.h> Standard input/output
<stdlib.h> General utilities: memory management, conversions, process control
<string.h> String aur raw-memory manipulation
<math.h> Floating-point mathematics
<ctype.h> Character classification aur case conversion
<time.h> Date aur time
<assert.h> Diagnostic assertions
<limits.h> Integer type ke size limits
<float.h> Floating-point type ke limits
<stddef.h> Common definitions (size_t, ptrdiff_t, NULL)
<stdbool.h> Boolean type aur values (C99)
<stdint.h> Fixed-width integer types (C99)
<stdarg.h> Variadic function support
<setjmp.h> Non-local jumps
<signal.h> Signal handling
<locale.h> Locale-specific formatting
<errno.h> Error codes
<threads.h> Multithreading (C11, optional)
<stdatomic.h> Atomic operations (C11, optional)

Agla idea padhne ke liye tayyar?

Mark this chapter as read whenever you feel ready.

CHAPTER 41 / 73

Is chapter mein

Headers: interface share karna

Search all 73 chapters in both languages.EN + HG

Try “pointer”, “why int”, or “undefined behavior”.

Concepts connect. Follow your curiosity.↑ ↓ to explore · Enter to open
MAKE YOURSELF COMFORTABLE

Your reading space

Appearance

A clear canvas with balanced contrast.

Text size

100%
AA

Reading tools

Your data stays in this browser

Export a backup before changing browsers or replacing an older version of the reader.

YOUR PERSONAL MARGINS

My notebook

Saved on this device
A LITTLE CLARITY

The glossary

A starting meaning, then a chapter to go deeper.

A QUICK DEFINITION

A little clarity

Read the full explanation →

A thought worth keeping