Repository navigation
Expand file tree
/
Copy pathstage1.html
More file actions
208 lines (194 loc) · 14.5 KB
/
Copy pathstage1.html
File metadata and controls
208 lines (194 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>1단계 - 주소가 뭐예요? | C 포인터 쉽게 배우기</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Jua&family=Noto+Sans+KR:wght@400;700&display=swap" rel="stylesheet">
<style>
:root { --bg: #f0f4ff; --card: #fff; --accent: #5b7cff; --text: #2d3748; --shadow: 0 4px 20px rgba(91, 124, 255, 0.15); }
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: 'Noto Sans KR', sans-serif; background: var(--bg); min-height: 100vh; color: var(--text); padding: 2rem; }
.container { max-width: 700px; margin: 0 auto; }
.back { margin-bottom: 1rem; }
.back a { color: var(--accent); text-decoration: none; font-size: 0.95rem; }
.back a:hover { text-decoration: underline; }
h1 { font-family: 'Jua', sans-serif; font-size: 1.8rem; color: var(--accent); margin-bottom: 1rem; }
.section { background: var(--card); border-radius: 16px; padding: 1.5rem; box-shadow: var(--shadow); margin-bottom: 1.5rem; }
.section h2 { font-size: 1.15rem; margin-bottom: 0.75rem; color: #444; }
.section p, .section li { line-height: 1.75; margin-bottom: 0.5rem; }
.mailbox { display: flex; gap: 0.5rem; flex-wrap: wrap; justify-content: center; margin: 1rem 0; }
.box { width: 60px; height: 50px; border: 3px solid #333; border-radius: 8px; display: flex; flex-direction: column; align-items: center; justify-content: center; font-family: monospace; font-size: 0.75rem; background: #fff8e1; }
.box .addr { color: #666; }
.box .val { font-weight: bold; color: #c62828; }
.highlight { background: #e3f2fd; padding: 0.15rem 0.4rem; border-radius: 4px; }
.nav-links { display: flex; justify-content: space-between; margin-top: 1.5rem; }
.nav-links a { color: var(--accent); text-decoration: none; font-weight: 700; }
.nav-links a:hover { text-decoration: underline; }
.practice-section { border: 2px solid #22c55e; background: #f0fdf4; }
.practice-section h2 { color: #166534; }
.practice-section .task { margin: 0.75rem 0; padding-left: 1rem; border-left: 4px solid #22c55e; }
.practice-section .task strong { display: block; margin-bottom: 0.25rem; }
.deep-section { border: 2px solid #7c3aed; background: #f5f3ff; }
.deep-section h2 { color: #5b21b6; }
.practice-editor-section { border: 2px solid #0ea5e9; background: #f0f9ff; border-radius: 12px; padding: 1rem; margin: 1rem 0; }
.practice-editor-section h3 { font-size: 1rem; color: #0369a1; margin-bottom: 0.5rem; }
.editor-toolbar { display: flex; gap: 0.5rem; margin-bottom: 0.5rem; flex-wrap: wrap; }
.editor-toolbar button { padding: 0.4rem 0.8rem; border-radius: 8px; border: 1px solid #0ea5e9; background: #fff; color: #0369a1; font-weight: 700; cursor: pointer; }
.editor-toolbar button:hover { background: #e0f2fe; }
.editor-wrap { border: 1px solid #94a3b8; border-radius: 8px; overflow: hidden; }
.editor-wrap .CodeMirror { min-height: 180px; font-size: 14px; }
.practice-result { margin-top: 0.5rem; padding: 0.75rem; border-radius: 8px; font-family: monospace; font-size: 13px; white-space: pre-wrap; word-break: break-all; min-height: 2.5rem; }
.practice-result.running { background: #fef3c7; color: #92400e; }
.practice-result.ok { background: #d1fae5; color: #065f46; }
.practice-result.error { background: #fee2e2; color: #991b1b; }
</style>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/codemirror.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/theme/tomorrow-night-eighties.min.css">
</head>
<body>
<div class="container">
<div class="back"><a href="index.html">← 목록으로</a></div>
<h1>📬 1단계: 주소가 뭐예요?</h1>
<div class="section">
<h2>먼저: 변수가 뭔가요?</h2>
<p><strong>변수</strong>는 프로그램에서 “값을 넣어 둘 수 있는, 이름이 붙은 공간”이에요. C에서는 <code>int a = 10;</code>처럼 쓰면, <strong>a</strong>가 변수 <em>이름</em>이고, <strong>10</strong>이 그 공간에 들어 있는 <em>값</em>이에요. 즉 <strong>a</strong>는 글자 ‘a’가 아니라 “이름이 a인 변수(저장 공간)”를 말하는 거예요.</p>
</div>
<div class="section">
<h2>우편함으로 생각해 보기</h2>
<p>아파트에 우편함이 여러 개 있다고 생각해 보세요. 각 우편함에는 <span class="highlight">번호(주소)</span>가 있고, 그 안에는 <span class="highlight">편지나 소포(값)</span>가 들어 있어요. C에서 변수는 이런 “우편함 한 칸”과 같고, 변수 이름(예: a, b)은 그 칸을 부르는 이름이에요.</p>
<div class="mailbox">
<div class="box"><span class="addr">101호 (주소)</span><span class="val">10 (값)</span></div>
<div class="box"><span class="addr">102호 (주소)</span><span class="val">20 (값)</span></div>
<div class="box"><span class="addr">103호 (주소)</span><span class="val">30 (값)</span></div>
<div class="box"><span class="addr">104호 (주소)</span><span class="val">40 (값)</span></div>
</div>
<p>“103호에 뭐가 있어?” → <strong>30</strong>이 있어요. 여기서 <strong>103호</strong>가 <strong>주소</strong>, <strong>30</strong>이 그 안의 <strong>값</strong>이에요. C에서 <code>int a = 10;</code>이면, 변수 <strong>a</strong>도 “어떤 번지(주소)”에 있고, 그 안에 <strong>10</strong>(값)이 들어 있어요.</p>
</div>
<div class="section">
<h2>컴퓨터 메모리도 비슷해요</h2>
<p>컴퓨터도 데이터를 저장할 때 이런 ‘방’들이 많이 있어요. 각 방마다 <strong>주소(번지)</strong>가 있고, 그 안에 숫자나 문자 같은 <strong>값</strong>이 들어 있어요.</p>
<ul>
<li><strong>주소</strong> = “몇 번 방이에요?” (예: 0x1000, 0x1004)</li>
<li><strong>값</strong> = “그 방에 들어 있는 내용이 뭐예요?” (예: 10, 20, 'A')</li>
</ul>
<p>포인터는 바로 이 <strong>주소를 기억하는 것</strong>이에요. “그 값이 어디 있는지”를 가리키는 거예요.</p>
</div>
<div class="section">
<h2>✅ 1단계 정리</h2>
<ul>
<li><strong>변수</strong> = 이름이 붙은 저장 공간 (예: a, b). 그 공간에는 <strong>주소</strong>와 <strong>값</strong>이 있어요.</li>
<li>우편함 <strong>번호</strong> = 주소, 우편함 <strong>안의 것</strong> = 값. C에서 변수 이름(예: a)은 그 “방”을 부르는 이름이에요.</li>
<li>포인터는 그 <strong>주소를 가리키는 도구</strong>예요.</li>
</ul>
</div>
<div class="section" style="background:#f8fafc; border:1px solid #e2e8f0;">
<h2>📌 이 강의에서 쓰는 C 함수 (간략 설명)</h2>
<p>아래 실습 코드에 나오는 것만 짧게 정리해요.</p>
<ul style="margin-top:0.5rem;">
<li><code>#include <stdio.h></code> — 화면에 출력하는 함수(<code>printf</code>)를 쓰기 위해 맨 위에 한 번 적어요.</li>
<li><code>int main() { ... }</code> — C 프로그램이 실행될 때 <strong>제일 먼저</strong> 불리는 함수예요. 여기 안에 실행할 내용을 넣어요. <code>return 0;</code>은 “정상 종료”를 알리는 뜻이에요.</li>
<li><code>printf("문자열");</code> — 화면(표준 출력)에 글자를 <strong>출력</strong>해요. <code>%d</code>는 정수, <code>%p</code>는 주소를 넣을 자리이고, <code>\n</code>은 줄바꿈이에요. 예: <code>printf("a의 값: %d\n", a);</code> → “a의 값: ” 다음에 변수 a의 값이 나오고 줄이 바뀌어요.</li>
</ul>
</div>
<div class="section">
<h2>주소를 출력해 보기</h2>
<p>아래 코드에서 <strong>a</strong>는 변수 이름(정수 하나를 담는 공간)이에요. <code>a</code>는 그 안의 값, <code>&a</code>는 그 변수가 있는 주소예요. [실행]으로 바로 결과를 볼 수 있어요.</p>
<div class="practice-editor-section">
<div class="editor-toolbar">
<button type="button" id="practice-demo-format">포맷</button>
<button type="button" id="practice-demo-run">실행</button>
<button type="button" id="practice-demo-copy">복사</button>
</div>
<div class="editor-wrap">
<textarea id="practice-code-demo">#include <stdio.h>
int main() {
int a = 10; /* 변수 a에 10 저장 */
printf("변수 a에 들어 있는 값: %d\n", a);
printf("변수 a가 있는 곳의 주소: %p\n", (void*)&a);
return 0;
}</textarea>
</div>
<div id="practice-demo-result" class="practice-result" aria-live="polite"></div>
</div>
<p><code>%d</code>는 숫자(값), <code>%p</code>는 주소를 출력할 때 써요. 실행할 때마다 주소가 다르게 나올 수 있어요. 그게 정상이에요!</p>
</div>
<div class="section practice-section">
<h2>📋 실습 과제</h2>
<p>아래 과제를 순서대로 해 보세요. 답은 다음 단계에서 C 코드로 확인할 수 있어요.</p>
<div class="practice-editor-section">
<h3>✏️ 여기서 직접 실습해 보세요 (2단계 예습 코드)</h3>
<p style="font-size:0.9rem; color:#555; margin-bottom:0.5rem;">아래 코드에서 <strong>a</strong>는 <em>변수 이름</em>(정수 하나를 담는 공간의 이름)이에요. <code>int a = 10;</code>은 “변수 a에 10을 넣는다”는 뜻이고, 그다음 줄은 “변수 a에 들어 있는 값”과 “변수 a가 있는 위치(주소)”를 출력해요. 모바일에서도 수정·포맷·실행·복사 가능해요. [실행]으로 바로 결과를 볼 수 있습니다.</p>
<div class="editor-toolbar">
<button type="button" id="practice-format">포맷</button>
<button type="button" id="practice-run">실행</button>
<button type="button" id="practice-copy">복사</button>
</div>
<div class="editor-wrap">
<textarea id="practice-code">#include <stdio.h>
int main() {
int a = 10;
printf("a의 값: %d\n", a);
// 기능 추가: a의 주소를 출력하는 printf 한 줄을 작성하세요. (%p와 (void*)&a 사용)
return 0;
}</textarea>
</div>
<div id="practice-result" class="practice-result" aria-live="polite"></div>
<details style="margin-top:0.75rem;">
<summary style="cursor:pointer; font-weight:700; color:#0369a1;">참고 정답 코드 (클릭하여 펼치기)</summary>
<pre class="answer-code-block line-numbers" style="margin-top:0.5rem;"><code class="language-c">#include <stdio.h>
int main() {
int a = 10;
printf("a의 값: %d\n", a);
printf("a의 주소: %p\n", (void*)&a);
return 0;
}</code></pre>
<button type="button" class="copy-answer-to-editor" style="margin-top:0.5rem; padding:0.4rem 0.8rem; border-radius:8px; border:1px solid #0ea5e9; background:#fff; color:#0369a1; font-weight:700; cursor:pointer;">에디터로 복사</button>
</details>
</div>
<div class="task">
<strong>과제 1</strong>
다음 표에서 빈칸을 채우세요. (우편함 비유)
</div>
<table style="width:100%; border-collapse: collapse; margin: 0.5rem 0;">
<tr style="background:#e0f2fe;"><th style="border:1px solid #333; padding:6px;">우편함 번호</th><th style="border:1px solid #333; padding:6px;">주소인가요? (O/X)</th><th style="border:1px solid #333; padding:6px;">그 안에 들어 있는 것</th><th style="border:1px solid #333; padding:6px;">값인가요? (O/X)</th></tr>
<tr><td style="border:1px solid #333; padding:6px;">101호</td><td style="border:1px solid #333; padding:6px;">O</td><td style="border:1px solid #333; padding:6px;">사과 3개</td><td style="border:1px solid #333; padding:6px;">O</td></tr>
<tr><td style="border:1px solid #333; padding:6px;">( )</td><td style="border:1px solid #333; padding:6px;">O</td><td style="border:1px solid #333; padding:6px;">숫자 50</td><td style="border:1px solid #333; padding:6px;">( )</td></tr>
</table>
<p><strong>답:</strong> 우편함 번호 → 102호(예시), 값인가요? → O</p>
<div class="task">
<strong>과제 2</strong>
“컴퓨터 메모리에서 0x1000번지에 100이 저장되어 있다”고 할 때, 여기서 <strong>주소</strong>는 무엇이고 <strong>값</strong>은 무엇인지 써 보세요.
</div>
<p><strong>답:</strong> 주소 = 0x1000, 값 = 100</p>
<div class="task">
<strong>과제 3</strong>
포인터는 “주소”를 저장하는가, “값”을 저장하는가? 한 줄로 답하세요.
</div>
<p><strong>답:</strong> 주소를 저장(기억)합니다.</p>
</div>
<div class="section deep-section">
<h2>🔬 심화 학습</h2>
<p><strong>메모리 주소는 왜 16진수로 보일까?</strong></p>
<p>컴퓨터는 모든 것을 0과 1(2진수)로 저장해요. 하지만 주소를 사람이 볼 때는 2진수로 쓰면 너무 길어지니까, 짧게 나타내기 위해 16진수(0~9, A~F)를 많이 써요. C에서 주소를 출력하면 <code>0x7fff5fb...</code>처럼 0x로 시작하는 16진수로 나와요.</p>
<p><strong>바이트란?</strong></p>
<p>메모리의 “방 한 칸”을 보통 <strong>1바이트</strong>라고 해요. 정수 하나(int)는 보통 4바이트를 씁니다. 그래서 변수 a 다음에 변수 b가 있으면, a의 주소에 4를 더한 곳 근처에 b가 있을 수 있어요. (2단계에서 실습으로 확인해 보세요.)</p>
</div>
<div class="nav-links">
<a href="index.html">목록</a>
<a href="stage2.html">2단계: 변수와 주소 →</a>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/codemirror.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/mode/clike/clike.min.js"></script>
<script src="practice-editor.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
window.PracticeEditor.init('practice-code-demo', 'practice-demo');
window.PracticeEditor.init('practice-code', 'practice');
});
</script>
</body>
</html>