--- id: c-organize-code title: "C Organize Code" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["header files", "include guard", "modular programming", "C 코드 조직화"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.85 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["c", "programming-language", "w3schools", "header-files", "modular-programming"] raw_sources: ["https://www.w3schools.com/c/c_organize_code.php"] applied_in: [] github_commit: "" --- # [[C Organize Code]] ## 🎯 한 줄 통찰 (One-line insight) The `#ifndef`/`#define`/`#endif` include guard exists to solve a problem that's invisible in small single-file programs but inevitable in multi-file ones — preventing the SAME header from being included twice (directly or transitively through other headers) which would otherwise cause duplicate-declaration compile errors, making the include guard a defensive pattern every C header should carry regardless of whether the problem has actually occurred yet. [S1] ## 🧠 핵심 개념 (Core concepts) - **Modular programming** — splitting code into smaller, reusable `.c`/`.h` file pairs for readability, maintainability, and debuggability; optional for small programs, valuable for larger ones. [S1] - **Header files (`.h`)** — declare functions, share variables/constants/macros across files, and organize code into logical modules; typically contain declarations, macros, and struct definitions (not full function bodies). [S1] - **Include guard (`#ifndef`/`#define`/`#endif`)** — prevents a header from being included more than once by accident, avoiding duplicate-declaration errors; a standard, recommended practice in every C header. [S1] - **Separation of declaration and definition** — the `.h` file declares function signatures; the corresponding `.c` file provides the actual function bodies. [S1] - **Multi-file compilation** — the compiler must be given ALL relevant `.c` files at once (e.g. `gcc main.c calc.c -o program`) to link them into one executable. [S1] ## 📖 세부 내용 (Details) - Header file with include guard: `#ifndef CALC_H #define CALC_H int add(int x, int y); int subtract(int x, int y); #endif` (calc.h). [S1] - Corresponding source file: `#include "calc.h" int add(int x, int y) { return x + y; } int subtract(int x, int y) { return x - y; }` (calc.c). [S1] - Consuming the module: `#include #include "calc.h" int main() { printf("5 + 5 = %d\n", add(5, 5)); printf("6 - 4 = %d\n", subtract(6, 4)); return 0; }` (main.c). [S1] - Compiling multiple files together: `gcc main.c calc.c -o program`. [S1] ## ⚖️ 모순 및 업데이트 (Contradictions & updates) - **include guard의 필요성**: 작은 프로그램에서는 문제가 드러나지 않지만, 헤더가 여러 파일에서 직간접적으로 중복 포함될 경우 중복 선언 에러가 발생할 수 있어 모든 C 헤더에 include guard를 넣는 것이 표준 관행으로 권장됨. [S1] ## 🛠️ 적용 사례 (Applied in summary) 현재 발견된 실제 적용 사례가 없습니다 — calc.h/calc.c/main.c로 나눈 계산기 모듈 예제가 여러 파일로 나뉜 프로그램을 gcc로 함께 컴파일하는 실전 워크플로우를 그대로 보여준다. [S1] ## 💻 코드 패턴 (Code patterns) A header file with an include guard, plus the gcc command to compile the multi-file project (C): ```c // calc.h #ifndef CALC_H #define CALC_H int add(int x, int y); int subtract(int x, int y); #endif ``` ```bash gcc main.c calc.c -o program ``` ## ✅ 검증 상태 및 신뢰도 - **상태:** draft - **검증 단계:** conceptual - **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body) - **신뢰 점수:** 0.85 - **중복 검사 결과:** 신규 생성 (New discovery) ## 🔗 지식 그래프 (Knowledge Graph) - **상위/루트:** [[C Tutorial]] - **관련 개념:** [[C Comments]], [[C Functions]], [[C Variables]] - **참조 맥락:** Basics 섹션 마지막 — 변수(Variables) 섹션으로 이어짐. ## 📚 출처 (Sources) - [S1] W3Schools — C Organize Code — https://www.w3schools.com/c/c_organize_code.php ## 📝 변경 이력 (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C Organize Code" page (Astra wiki-curation, P-Reinforce v3.1 format).