--- id: c-input-validation title: "C Input Validation" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["validate number range", "sscanf validation", "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", "input-validation", "scanf", "sscanf"] raw_sources: ["https://www.w3schools.com/c/c_input_validation.php"] applied_in: [] github_commit: "" --- # [[C Input Validation]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) The integer-validation example doesn't use `scanf()` directly to detect bad input β€” it reads the RAW LINE with `fgets()` first, then tries to parse it with `sscanf()` and checks whether that parse SUCCEEDED (`== 1`), meaning robust validation in C separates "getting text from the user" from "trying to interpret that text as a number," rather than trusting `scanf()`'s own format-matching to gracefully reject non-numeric input. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Range validation via a do-while loop** β€” repeatedly prompt until the entered value falls within an allowed range; `while (getchar() != '\n');` clears any leftover characters in the input buffer after `scanf()`. [S1] - **Empty-text validation** β€” read with `fgets()`, strip the trailing newline (`name[strcspn(name, "\n")] = 0;`), then loop while the resulting string length is 0. [S1] - **Integer-format validation** β€” read the raw line as a STRING via `fgets()`, then attempt `sscanf(input, "%d", &number)`; if it returns `1` (one successful conversion), the input was a valid integer β€” otherwise, prompt again. [S1] - **Buffer-clearing necessity** β€” leftover characters (like an unconsumed newline) in the input stream after `scanf()` can silently corrupt the NEXT read unless explicitly cleared. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Range-bounded input with buffer clearing: `do { printf("Choose a number between 1 and 5: "); scanf("%d", &number); while (getchar() != '\n'); } while (number < 1 || number > 5);`. [S1] - Rejecting empty text input: `do { fgets(name, sizeof(name), stdin); name[strcspn(name, "\n")] = 0; } while (strlen(name) == 0);`. [S1] - Validating that input is actually an integer via sscanf's return value: `while (fgets(input, sizeof(input), stdin)) { if (sscanf(input, "%d", &number) == 1) { break; } else { printf("Invalid input. Try again: "); } }`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **λ¬Έμžμ—΄λ‘œ λ¨Όμ € 읽고 sscanf둜 νŒŒμ‹±μ„ κ²€μ¦ν•˜λŠ” μ „λž΅**: scanf()둜 직접 숫자λ₯Ό λ°›λŠ” λŒ€μ‹  fgets()둜 원문을 ν†΅μ§Έλ‘œ 받은 λ’€ sscanf()의 λ°˜ν™˜κ°’(μ„±κ³΅ν•œ λ³€ν™˜ 개수)을 ν™•μΈν•˜λŠ” 방식이 잘λͺ»λœ μž…λ ₯을 더 μ•ˆμ „ν•˜κ²Œ κ±ΈλŸ¬λ‚Έλ‹€λŠ” 점이 확인됨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) 1~5 λ²”μœ„μ˜ 숫자 선택, 빈 이름 κ±°λΆ€, λ¬Έμžκ°€ μ•„λ‹Œ μ •μˆ˜λ§Œ ν—ˆμš©ν•˜λŠ” μ„Έ κ°€μ§€ 검증 μ˜ˆμ œκ°€ μ›λ¬Έμ—μ„œ 직접 μ‹€μ „ ν™œμš© μ‚¬λ‘€λ‘œ μ œμ‹œλ¨. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Validating that user input is actually an integer using fgets() + sscanf()'s return value (C): ```c int number; char input[100]; printf("Enter a number: "); while (fgets(input, sizeof(input), stdin)) { if (sscanf(input, "%d", &number) == 1) { break; // valid integer } else { printf("Invalid input. Try again: "); } } ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.86 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[C Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[C Error Handling]], [[C User Input]], [[C Debugging]] - **μ°Έμ‘° λ§₯락:** μž…λ ₯ 검증 β€” 디버깅(Debugging) μ±•ν„°λ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C Input Validation β€” https://www.w3schools.com/c/c_input_validation.php ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C Input Validation" page (Astra wiki-curation, P-Reinforce v3.1 format).