YOUR C LEARNING COMPANION

Kahan se padhein?

Learning path
Language ki detailsChapter 17 of 73

Storage classes: duration, scope aur linkage

Names, object lifetimes, definitions, initialization aur shared declarations ke separate sawaal poochhein.

7 min readFoundationsC17 + C23 notes
Offline reading
Is chapter mein10 sections

Naam kahan use ho sakta hai? Uska object kitni der exist karta hai? Kya doosri source file usi entity ko naam se refer kar sakti hai? Yeh alag questions hain. Storage-class specifiers inke rules ka hissa hain.

1. Chaar alag sawaal poochhein

SawaalConceptExample
Source mein yeh name kahan use kar sakte hain?ScopeBlock-local identifier apne scope mein visible hai.
Kaunsi declarations same entity name karti hain?LinkageCompatible extern declarations ek shared object name kar sakti hain.
Execution ke kis period mein object exist karta hai?Lifetime / storage durationStatic local ek function call se zyada jeeta hai.
Shuru ki value kya hai?InitializationUninitialized static int zero se shuru hota hai.

Storage class specifier in declaration rules mein participate karta hai; iska naam literal hardware storage location ka promise nahi hai. Ek keyword multiple answers affect kare tab bhi sawaal alag rakhein.

2. Automatic locals restart; static locals state retain karte hain

Complete C17 program
#include <assert.h>

static int next_value(void)
{
    int fresh = 10;
    static int calls = 0;
    ++calls;
    return fresh + calls;
}

int main(void)
{
    int first = next_value();
    int second = next_value();
    assert(first == 11 && second == 12);
    return 0;
}

Har call fresh automatic fresh create karke 10 se initialize karti hai. Static calls object program startup se pehle ek baar initialize hota hai aur calls ke beech survive karta hai. Results 11 aur 12 hain. Uska name phir bhi block-scoped aur no-linkage hai; lambi lifetime name ko global nahi banati.

Ordinary non-VLA automatic object ki lifetime associated block mein entry se us block execution ke end tak hai. VLA ki lifetime declaration se shuru hokar declaration ke scope se execution bahar jaane par khatam hoti hai. Recursion mein simultaneous automatic instances ho sakti hain. Yeh language rules hain, physical “stack” use karne ki universal requirement nahi.

Explicit initializer ke bina static/thread-duration objects typed zero initialization paate hain: arithmetic zero, null pointers aur recursively initialized members. Ordinary uninitialized automatic objects ko usable zero values nahi milti. Initialization se pehle unhe read na karein.

C17 · illustrative fragment
static int total;
static int *target;

Yahan total zero aur target null pointer hai. Null pointer ka byte representation all-zero hona required nahi.

3. static aur extern ka linkage context se samjhein

File scope par static int total; total ko internal linkage aur static storage duration deta hai. Doosri translation units us identifier ki linkage se same object name nahi kar sakti, lekin valid pointer interface se object expose ho sakta hai. Block ke andar static int total; block-scope, no-linkage, static-duration object declare karta hai.

extern ka matlab “kahin aur create hua” ya “storage nahi” nahi. File-scope extern int total = 1; definition hai. Uninitialized extern int total; commonly ek baar kahin aur defined object ki declaration deta hai.

Complete C17 program
#include <assert.h>

static int total;

static void record(void)
{
    extern int total;
    ++total;
}

int main(void)
{
    record();
    record();
    assert(total == 2);
    return 0;
}

Block extern visible file-scope declaration ki internal linkage inherit karta hai. Yeh wahi total name karta hai; doosra object create ya pehle ko externally linked nahi banata. Linkage wali prior declaration visible na ho toh relevant extern rules normally external linkage establish karte hain. Clever redeclarations se simple consistent policy behtar hai.

C17 file-scope int total; Tentative definition hai, har file mein independent instance create karne ka instruction nahi. Headers mein object declarations distribute karne se pehle translation units aur linkage dekhein.

4. Optional: do source files mein shared single definition

Building programs unit ke saath padhein

Header shared object aur function declare karta hai. counter.c unko define karta hai. local_calls ki internal linkage hai; client.c shared declaration se codedile_total refer karta hai. Yeh separate translation units hain, ek pasted program nahi.

C17 · project file · counter.h
#ifndef CODEDILE_COUNTER_H
#define CODEDILE_COUNTER_H

extern unsigned codedile_total;
void codedile_record(void);

#endif
C17 · project file · counter.c
#include "counter.h"

unsigned codedile_total = 0;
static unsigned local_calls = 0;

void codedile_record(void)
{
    ++local_calls;
    codedile_total = local_calls;
}
C17 · project file · client.c
#include <assert.h>
#include "counter.h"

int main(void)
{
    codedile_record();
    codedile_record();
    assert(codedile_total == 2);
    return 0;
}

Do calls ke baad shared total 2 hai. Bounded example single-threaded hai aur sirf do increments karta hai. Threads shared state access karein ya counter limit tak pahunch sake toh module ko explicit synchronization aur range policies chahiye. Private name akela dono provide nahi karta.

5. auto, register, typedef aur thread duration

  • C17 mein auto int value = 3; ordinary automatic local case explicitly request karta hai. Usually int value = 3; likhein.
  • register optimization suggestion hai; CPU-register placement ya speed guaranteed nahi. C17 aur C23 mein register-declared object ka address direct unary & se nahi le sakte. Register arrays par bhi special restrictions hain; ordinary teaching interfaces mein register omit karein.
  • typedef syntactic convenience ke liye grammar ki storage-class-specifier category mein hai. Yeh type alias deta hai, object ya new storage duration nahi.
  • _Thread_local har thread ke liye thread-duration object declare karta hai. Block scope par static ya extern saath hona chahiye; us object ki har declaration mein consistently yeh specifier hona chahiye. File scope par static/extern linkage bhi choose kar sakte hain.
Thread-duration declaration, multi-thread execution demo nahi
C17 · translation-unit fragment
static _Thread_local unsigned thread_visits;

unsigned visit_in_this_thread(void)
{
    return ++thread_visits;
}

Har thread ka apna thread_visits zero se initialize hota hai. Function calling thread ke instance par kaam karta hai. Doosre thread ke object ko indirectly access karna implementation-defined hai; thread duration pointer sharing ko automatically safe nahi banati. Thread support implementation capability hai jise check karna chahiye.

Intentional diagnostic: register object ka address
C17 · intentional build rejection
int main(void)
{
    register int value = 7;
    int *address = &value;
    return *address;
}

Diagnostic source-language constraint se aata hai, chahe compiler otherwise value ko addressable memory mein rakhta.

6. Same spelling ka doosra role ho sakta hai

Array parameter mein int items[static 4] har call ke liye minimum accessible-element contract hai; parameter pointer type mein adjust hota hai. Yeh static local array allocate nahi karta. Is form se pehle arrays aur pointers together padhein.

C23 supported initialized declarations mein auto se type inference bhi karta hai, jaise auto count = 3;. Primary spelling thread_local aur specifier constexpr add hote hain. Auto inference dynamic typing nahi: inferred type declaration se fixed hai. Har C++ auto/constexpr form ko valid C23 assume na karein.

Allocated storage chauthi storage-duration category hai. Yeh allocation facilities se milti hai; “heap” naam ka storage-class keyword nahi. Iski lifetime address rakhne wale pointer variable se independently end ho sakti hai. Dynamic memory chapter ownership aur release develop karta hai.

7. Thought check: lambi lifetime ka matlab wide scope?

Kya variable function call ke baad survive karke bhi local name rakh sakta hai?

Haan: static calls object wahi karta hai. Lifetime program execution tak rehti hai; identifier block-scoped aur no-linkage hai. Scope, linkage, duration aur initialization related declaration properties hain, synonyms nahi.

Sources aur aage ki reading

Primary references: WG14 N1570 6.2.1, 6.2.2, 6.2.4, 6.5.3.2, 6.7.1, 6.7.6.3, 6.7.9, and 6.9.2; WG14 N3096 (C23 notes). These are public committee drafts; the examples here use a C17 baseline.

Aage badhne se pehle

  • Scope, storage duration aur linkage different properties hain.
  • Static-storage objects program execution tak rehte hain; thread-duration objects thread se associated hain.
  • Storage-class keyword ko declaration context mein padhein; universal lifetime shortcut na banayein.
In ideas ko alag rakhein
Scope / Linkage
Scope naam ki visibility batata hai; linkage same entity identify karne wali declarations connect karti hai.
Apne words mein samjhaayein

Local static object ek block execution ke baad bhi kaise rehta hai jabki naam block-scoped hai?

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.

File-scope definition static int count; par kaunsi properties apply hoti hain?
Har choice ki explanation padhein
  1. Static storage duration aur internal linkage.

    ✓ Sahi reasoning. Yeh object duration aur identifier linkage batati hain; naam file scope par declared hai.

  2. Ek function call tak limited automatic duration.

    File-scope object ki ordinary automatic duration nahi hoti.

  3. Naam use hone par fresh dynamic allocation.

    Naam refer karna nayi allocation perform nahi karta.

C17

Agla idea padhne ke liye tayyar?

Mark this chapter as read whenever you feel ready.

CHAPTER 17 / 73

Is chapter mein

Storage classes: duration, scope aur linkage

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