--- id: c-storage-classes title: "C Storage Classes" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["auto static register extern", "static local variable", "C 저장 클래스"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.86 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["c", "programming-language", "w3schools", "storage-classes", "static"] raw_sources: ["https://www.w3schools.com/c/c_storage_classes.php"] applied_in: [] github_commit: "" --- # [[C Storage Classes]] ## 🎯 한 줄 통찰 (One-line insight) A `static` local variable inside a function survives BETWEEN separate calls to that function — the source's `count()` example prints `1`, `2`, `3` across three separate calls specifically because `static` makes the variable retain its value instead of resetting to 0 each time, whereas an ordinary local variable would print `1`, `1`, `1` — demonstrating that `static` fundamentally changes a variable's LIFETIME, not just its scope. [S1] ## 🧠 핵심 개념 (Core concepts) - **Scope vs. storage class** — scope defines WHERE a variable can be used; storage class defines HOW LONG it lasts and WHERE it's stored — two independent concepts. [S1] - **`auto`** — the default storage class for local variables inside functions; rarely written explicitly since it's already the default. [S1] - **`static`** — a local variable keeps its value across separate function calls (rather than resetting each call); a global variable/function marked `static` becomes invisible outside its own file. [S1] - **`register`** — suggests storing a variable in a CPU register for faster access; its address cannot be taken with `&`; considered mostly obsolete since modern compilers auto-optimize register allocation. [S1] - **`extern`** — tells the compiler a variable/function is DEFINED in another file, used for sharing variables across multiple source files. [S1] ## 📖 세부 내용 (Details) - `static` preserving value across calls: `void count() { static int myNum = 0; myNum++; printf("num = %d\n", myNum); } int main() { count(); count(); count(); return 0; }` — outputs `num = 1`, `num = 2`, `num = 3` (removing `static` would reset to 1 each time). [S1] - `extern` sharing a variable across files: `main.c` declares `extern int shared;` while `data.c` defines `int shared = 50;`, compiled together via `gcc main.c data.c -o program`. [S1] ## ⚖️ 모순 및 업데이트 (Contradictions & updates) - **static은 값을 유지, register는 사실상 구식**: static이 함수 호출 간 값을 보존한다는 점과, register 키워드는 현대 컴파일러가 알아서 최적화하므로 명시적으로 쓸 필요가 거의 없다는 점이 함께 언급됨. [S1] ## 🛠️ 적용 사례 (Applied in summary) 현재 발견된 실제 적용 사례가 없습니다 — count() 함수가 호출될 때마다 값을 누적하는 static 카운터 패턴이 함수 호출 횟수를 추적하는 실전 활용의 대표 사례다. [S1] ## 💻 코드 패턴 (Code patterns) A static local variable retaining its value across separate function calls (C): ```c void count() { static int myNum = 0; // Keeps its value between calls myNum++; printf("num = %d\n", myNum); } int main() { count(); // num = 1 count(); // num = 2 count(); // num = 3 return 0; } ``` ## ✅ 검증 상태 및 신뢰도 - **상태:** draft - **검증 단계:** conceptual - **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body) - **신뢰 점수:** 0.86 - **중복 검사 결과:** 신규 생성 (New discovery) ## 🔗 지식 그래프 (Knowledge Graph) - **상위/루트:** [[C Tutorial]] - **관련 개념:** [[C Constants]], [[C Scope]], [[C Fixed Width Ints]] - **참조 맥락:** 저장 클래스와 변수 생명주기 — 고정폭 정수(Fixed Width Ints) 챕터로 이어짐. ## 📚 출처 (Sources) - [S1] W3Schools — C Storage Classes — https://www.w3schools.com/c/c_storage_classes.php ## 📝 변경 이력 (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C Storage Classes" page (Astra wiki-curation, P-Reinforce v3.1 format).