--- id: c-unions title: "C Unions" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["union keyword", "shared memory members", "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", "unions"] raw_sources: ["https://www.w3schools.com/c/c_unions.php"] applied_in: [] github_commit: "" --- # [[C Unions]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) Writing to ANY member of a union silently invalidates every other member's previously-stored value β€” the source's example shows `u1.myNum = 1000;` followed by `u1.myLetter = 'A';` leaves `myNum` printing "no longer reliable" garbage, because unlike a struct where each member owns separate memory, a union's members OVERLAP the exact same bytes, so the last write always wins and there's no compiler warning when you accidentally read a stale member. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **`union`** β€” syntactically similar to `struct` (same declaration/dot-access style), but semantically opposite: all members share ONE memory location instead of each having its own. [S1] - **Only one valid member at a time** β€” writing to any member overwrites the shared memory, silently invalidating whatever the previous member held. [S1] - **Size = largest member** β€” `sizeof(union)` equals the size of its biggest member, NOT the sum of all members (unlike a struct, whose size is roughly the sum of members plus padding). [S1] - **Use case** β€” when you only need to store ONE of several possible types at a time and memory savings matter; structs remain the default choice for storing/accessing multiple values simultaneously. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - The stale-value trap: `union myUnion { int myNum; char myLetter; char myString[30]; }; u1.myNum = 1000; u1.myLetter = 'A'; printf("myNum: %d\n", u1.myNum); // no longer reliable printf("myLetter: %c\n", u1.myLetter); // 'A'`. [S1] - Size determined by the largest member: `union myUnion { int myNum; char myString[36]; }; sizeof(u1); // 36 (the size of myString, the largest member)`. [S1] - Contrast with an equivalent struct's size: the same two members (`int` + `char[36]`) as a STRUCT would total ~40 bytes (4 + 36), versus the union's 36 bytes. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **μœ λ‹ˆμ˜¨μ˜ λ§ˆμ§€λ§‰ λŒ€μž…λ§Œ 유효**: μ„œλ‘œ λ‹€λ₯Έ 멀버에 μˆœμ„œλŒ€λ‘œ 값을 μ“°λ©΄ 이전에 μ“΄ λ©€λ²„μ˜ 값은 쑰용히 λ¬΄νš¨ν™”λœλ‹€λŠ” 점이 λͺ…μ‹œμ μœΌλ‘œ 경고됨 β€” μ»΄νŒŒμΌλŸ¬κ°€ 이λ₯Ό μ•Œλ €μ£Όμ§€ μ•ŠμœΌλ―€λ‘œ ν”„λ‘œκ·Έλž˜λ¨Έκ°€ 직접 관리해야 함. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) ν˜„μž¬ 발견된 μ‹€μ œ 적용 사둀가 μ—†μŠ΅λ‹ˆλ‹€ β€” μ—¬λŸ¬ νƒ€μž… 쀑 ν•œ λ²ˆμ— ν•˜λ‚˜λ§Œ μ €μž₯ν•˜λ©΄μ„œ λ©”λͺ¨λ¦¬λ₯Ό μ ˆμ•½ν•΄μ•Ό ν•˜λŠ” 상황(예: ν”„λ‘œν† μ½œ νŒŒμ‹±)이 μœ λ‹ˆμ˜¨μ˜ λŒ€ν‘œμ  ν™œμš© μ‚¬λ‘€λ‘œ 원문에 쑰건으둜만 언급됨. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Writing to one union member silently invalidates the previously-stored member (C): ```c union myUnion { int myNum; char myLetter; char myString[30]; }; int main() { union myUnion u1; u1.myNum = 1000; u1.myLetter = 'A'; // myNum is now invalid β€” same memory was overwritten printf("myLetter: %c\n", u1.myLetter); // Prints 'A' return 0; } ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.86 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[C Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[C Enums]], [[C Structs Padding]], [[C Typedef]] - **μ°Έμ‘° λ§₯락:** μœ λ‹ˆμ˜¨ β€” typedef μ±•ν„°λ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C Unions β€” https://www.w3schools.com/c/c_unions.php ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C Unions" page (Astra wiki-curation, P-Reinforce v3.1 format).