--- id: cpp-variables title: "C++ Variables" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["variable declaration", "cout chaining", "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: ["cpp", "programming-language", "w3schools", "variables"] raw_sources: ["https://www.w3schools.com/cpp/cpp_variables.asp"] applied_in: [] github_commit: "" --- # [[CPP Variables]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) Unlike C, where printing a variable requires a completely separate format-specifier mechanism (`printf("%d", myNum)`), C++'s `cout <<` can print a variable DIRECTLY and even CHAIN multiple variables and literal text together in one statement (`cout << name << " is " << age << " years old";`) β€” meaning C++'s output model eliminates the format-specifier/type-matching step entirely, since `<<` is overloaded to know how to print each type on its own. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Five introductory types** β€” `int` (whole numbers), `double` (decimals), `char` (single character, single quotes), `string` (text, double quotes), `bool` (true/false). [S1] - **Declaration syntax** β€” `type variableName = value;`, or declare first and assign later. [S1] - **Reassignment overwrites** β€” assigning a new value replaces the old one entirely. [S1] - **Direct variable printing via `cout <<`** β€” no format specifier needed, unlike C's `printf()`. [S1] - **Chaining text and variables** β€” `cout << "I am " << myAge << " years old.";` combines literal text and variable values in ONE statement using repeated `<<`. [S1] - **Arithmetic with variables** β€” `+` works between variables just as with literals: `int sum = x + y;`. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Declare-and-assign, then print directly (no format specifier): `int myNum = 15; cout << myNum;`. [S1] - Chaining multiple variables and text in one cout statement: `string name = "John"; int age = 35; double height = 6.1; cout << name << " is " << age << " years old and " << height << " feet tall.";`. [S1] - Adding two variables: `int x = 5; int y = 6; int sum = x + y; cout << sum;`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **C++λŠ” 포맷 μ§€μ •μž 없이 λ³€μˆ˜λ₯Ό λ°”λ‘œ 좜λ ₯**: C의 printf()와 달리 cout에 λ³€μˆ˜λ₯Ό 직접 λ„£κ³  μ—¬λŸ¬ 개λ₯Ό << 둜 이어뢙일 수 μžˆλ‹€λŠ” 점이, νƒ€μž…λ³„ 포맷 μ§€μ •μžλ₯Ό 맀번 λ§žμΆ°μ•Ό ν–ˆλ˜ Cμ™€μ˜ 핡심 차이둜 확인됨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) ν˜„μž¬ 발견된 μ‹€μ œ 적용 사둀가 μ—†μŠ΅λ‹ˆλ‹€ β€” λ¬Έμžμ—΄, μ •μˆ˜, μ‹€μˆ˜λ₯Ό ν•˜λ‚˜μ˜ cout 문에 이어뢙여 μ‚¬λžŒμ΄ 읽기 쒋은 λ¬Έμž₯을 λ§Œλ“œλŠ” νŒ¨ν„΄μ΄ μ‹€μ „ 좜λ ₯ ν¬λ§·νŒ…μ˜ λŒ€ν‘œ ν™œμš©μ΄λ‹€. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Chaining multiple variables and literal text in one cout statement (C++): ```cpp string name = "John"; int age = 35; double height = 6.1; cout << name << " is " << age << " years old and " << height << " feet tall."; ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.86 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[C++ Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[CPP New Lines]], [[CPP Variables Identifiers]], [[C Variables]] - **μ°Έμ‘° λ§₯락:** λ³€μˆ˜ μ„Ήμ…˜ 첫 챕터 β€” μ‹λ³„μž κ·œμΉ™(Variables Identifiers) μ±•ν„°λ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C++ Variables β€” https://www.w3schools.com/cpp/cpp_variables.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C++ Variables" page (Astra wiki-curation, P-Reinforce v3.1 format).