--- id: c-arrays-loop title: "C Array Loop" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["loop through array", "adaptive loop length", "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", "arrays", "loops"] raw_sources: ["https://www.w3schools.com/c/c_arrays_loop.php"] applied_in: [] github_commit: "" --- # [[C Array Loop]] ## 🎯 한 줄 통찰 (One-line insight) A hardcoded loop bound (`i < 4`) silently breaks the moment the array's contents change size — the source explicitly flags this as "not ideal, since it will only work for arrays of a specified size" — while replacing it with `sizeof(arr)/sizeof(arr[0])` makes the SAME loop automatically correct for an array of ANY length, turning a brittle magic number into a self-maintaining computation. [S1] ## 🧠 핵심 개념 (Core concepts) - **Hardcoded loop bound (fragile)** — `for (i = 0; i < 4; i++)` only works correctly for exactly a 4-element array; adding/removing elements breaks it silently. [S1] - **`sizeof` formula-driven bound (robust)** — `int length = sizeof(arr) / sizeof(arr[0]); for (i = 0; i < length; i++)` automatically adapts to the array's actual current size. [S1] - **Standing rule** — the chapter's own summary: "Always use the sizeof formula when looping through arrays." [S1] ## 📖 세부 내용 (Details) - Fragile hardcoded-bound loop: `int myNumbers[] = {25, 50, 75, 100}; int i; for (i = 0; i < 4; i++) { printf("%d\n", myNumbers[i]); }`. [S1] - Robust size-adaptive loop: `int myNumbers[] = {25, 50, 75, 100}; int length = sizeof(myNumbers) / sizeof(myNumbers[0]); int i; for (i = 0; i < length; i++) { printf("%d\n", myNumbers[i]); }`. [S1] ## ⚖️ 모순 및 업데이트 (Contradictions & updates) - **하드코딩된 루프 경계의 취약성**: 배열 원소 개수가 바뀌면 i < 4 같은 고정된 조건이 더 이상 맞지 않게 된다는 점이 "이상적이지 않다"고 명시적으로 지적되며, sizeof 공식을 항상 쓰라는 규칙으로 이어짐. [S1] ## 🛠️ 적용 사례 (Applied in summary) 현재 발견된 실제 적용 사례가 없습니다 — sizeof 기반 반복문이 이후 Arrays RealLife 챕터의 평균 계산·최솟값 찾기 예제에서 그대로 재사용된다. [S1] ## 💻 코드 패턴 (Code patterns) A loop that automatically adapts to any array size using the sizeof formula (C): ```c int myNumbers[] = {25, 50, 75, 100}; int length = sizeof(myNumbers) / sizeof(myNumbers[0]); int i; for (i = 0; i < length; i++) { printf("%d\n", myNumbers[i]); } ``` ## ✅ 검증 상태 및 신뢰도 - **상태:** draft - **검증 단계:** conceptual - **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body) - **신뢰 점수:** 0.86 - **중복 검사 결과:** 신규 생성 (New discovery) ## 🔗 지식 그래프 (Knowledge Graph) - **상위/루트:** [[C Tutorial]] - **관련 개념:** [[C Arrays Size]], [[C Arrays Multi]], [[C Arrays RealLife]] - **참조 맥락:** 배열 반복문 — 다차원 배열(Arrays Multi) 챕터로 이어짐. ## 📚 출처 (Sources) - [S1] W3Schools — C Array Loop — https://www.w3schools.com/c/c_arrays_loop.php ## 📝 변경 이력 (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C Array Loop" page (Astra wiki-curation, P-Reinforce v3.1 format).