Repository navigation
Expand file tree
/
Copy pathstage2.html
More file actions
212 lines (200 loc) · 13.2 KB
/
Copy pathstage2.html
File metadata and controls
212 lines (200 loc) · 13.2 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
209
210
211
212
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2단계 - 변수와 주소 | 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; }
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; }
code { font-family: 'Consolas', 'Monaco', monospace; }
.try-box { background: #f0fdf4; border: 2px solid #22c55e; border-radius: 10px; padding: 1rem; margin-top: 1rem; }
.try-box strong { color: #166534; }
.nav-links { display: flex; justify-content: space-between; margin-top: 1.5rem; }
.nav-links a { color: var(--accent); text-decoration: none; font-weight: 700; }
.mem-picture { display: flex; gap: 0.5rem; margin: 1rem 0; flex-wrap: wrap; }
.mem-cell { border: 2px solid #333; border-radius: 8px; padding: 0.5rem 0.75rem; font-family: monospace; background: #fff8e1; }
.mem-cell .name { color: #1565c0; font-weight: bold; }
.mem-cell .addr { font-size: 0.8rem; color: #666; }
.mem-cell .val { color: #c62828; }
.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; }
.deep-section { border: 2px solid #7c3aed; background: #f5f3ff; }
.deep-section h2 { color: #5b21b6; }
pre[class*="language-"] { margin: 1rem 0; border-radius: 10px; }
code[class*="language-"] { font-size: 0.9rem; }
.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: 200px; 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/prism/1.29.0/themes/prism-tomorrow.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/line-numbers/prism-line-numbers.min.css">
<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>📦 2단계: 변수와 주소</h1>
<div class="section">
<h2>변수는 메모리 한 칸에 있어요</h2>
<p><strong>변수</strong>는 “이름이 붙은 메모리 한 칸”이에요. <code>int a = 10;</code>이라고 쓰면, 컴퓨터는 메모리 어딘가에 방 하나를 잡고, 그 방의 이름을 <strong>a</strong>(변수 이름)로 부르고, 그 안에 <strong>10</strong>을 넣어요. 그 방에는 반드시 <strong>주소</strong>가 있어요.</p>
<div class="mem-picture">
<div class="mem-cell"><span class="name">변수 이름: a</span><br><span class="addr">주소: 0x1000</span><br><span class="val">값: 10</span></div>
</div>
<p>코드에서 <code>a</code>만 쓰면 → 그 변수(방)에 있는 <strong>값(10)</strong>을 쓰는 거고,<br>
<strong>주소를 알고 싶다면</strong> → <code>&a</code>라고 쓰면 돼요. <code>&</code>는 “그 변수의 주소를 달라”라는 뜻이에요.</p>
</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">
<h2>✅ 2단계 정리</h2>
<ul>
<li>변수는 메모리 한 칸을 차지하고, 그 칸에는 <strong>주소</strong>와 <strong>값</strong>이 있어요.</li>
<li><code>&변수이름</code> → 그 변수가 있는 <strong>주소</strong>를 가져와요.</li>
<li>주소를 출력할 때는 <code>printf</code>에서 <code>%p</code>를 써요.</li>
</ul>
</div>
<div class="section practice-section">
<h2>📋 실습 과제</h2>
<div class="task">
<strong>과제 1</strong>
변수 <code>a</code>, <code>b</code>, <code>c</code>를 각각 10, 20, 30으로 선언한 뒤, 세 변수의 <strong>값</strong>과 <strong>주소</strong>를 모두 출력하는 프로그램을 작성하세요.
</div>
<div class="practice-editor-section">
<h3>✏️ 여기서 직접 실습해 보세요</h3>
<p style="font-size:0.9rem; color:#555; margin-bottom:0.5rem;">모바일에서도 이 창에서 수정·포맷·실행·복사할 수 있어요. [실행]으로 바로 결과를 볼 수 있습니다.</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() {
// 기능 추가: 변수 a, b, c를 각각 10, 20, 30으로 선언하세요.
// 기능 추가: 세 변수의 값과 주소를 printf로 출력하세요. (값=%d, 주소=%p)
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, b = 20, c = 30;
printf("a: 값=%d, 주소=%p\n", a, (void*)&a);
printf("b: 값=%d, 주소=%p\n", b, (void*)&b);
printf("c: 값=%d, 주소=%p\n", c, (void*)&c);
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>과제 2</strong>
위 프로그램에서 <code>a</code>와 <code>b</code>의 주소가 같은지 다른지 <code>if</code>로 비교해 보세요. (<code>&a == &b</code>를 사용하면 됩니다.)
</div>
<div class="practice-editor-section">
<p style="font-size:0.9rem; color:#555; margin-bottom:0.5rem;">여기서 직접 작성·실행해 보세요. [실행]으로 결과를 확인할 수 있어요.</p>
<div class="editor-toolbar">
<button type="button" id="practice-2-format">포맷</button>
<button type="button" id="practice-2-run">실행</button>
<button type="button" id="practice-2-copy">복사</button>
</div>
<div class="editor-wrap">
<textarea id="practice-code-2">#include <stdio.h>
int main() {
int a = 10, b = 20;
// if (&a == &b) 로 주소가 같은지 다른지 비교해 출력하세요.
return 0;
}</textarea>
</div>
<div id="practice-2-result" class="practice-result" aria-live="polite"></div>
<details style="margin-top:0.75rem;">
<summary style="cursor:pointer; font-weight:700; color:#0369a1;">과제 2 참고 정답 코드 (클릭하여 펼치기)</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, b = 20;
if (&a == &b)
printf("a와 b의 주소가 같습니다.\n");
else
printf("a와 b의 주소가 다릅니다.\n");
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>
<div class="section deep-section">
<h2>🔬 심화 학습</h2>
<p><strong>sizeof로 변수 크기 알아보기</strong></p>
<p><code>sizeof(변수)</code>는 그 변수가 메모리에서 차지하는 <strong>바이트 수</strong>를 알려 줘요. <code>int</code>는 보통 4바이트예요. 주소끼리 빼면 “몇 칸 떨어져 있는지”를 알 수 있어요.</p>
<details style="margin-top:0.5rem;">
<summary style="cursor:pointer; font-weight:700; color:#5b21b6;">심화 참고 코드 (클릭하여 펼치기)</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, b = 20;
printf("int 크기: %zu 바이트\n", sizeof(int));
printf("a 주소: %p, b 주소: %p\n", (void*)&a, (void*)&b);
/* (b 주소 - 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 #7c3aed; background:#fff; color:#5b21b6; font-weight:700; cursor:pointer;">에디터로 복사</button>
</details>
<p style="margin-top:0.5rem;"><strong>정리:</strong> 변수마다 고유한 주소가 있고, <code>%p</code>로 그 주소를 볼 수 있어요. 다음 단계에서는 이 주소를 저장하는 “포인터”를 배워요.</p>
</div>
<div class="nav-links">
<a href="stage1.html">← 1단계</a>
<a href="stage3.html">3단계: 포인터 변수 →</a>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-c.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/line-numbers/prism-line-numbers.min.js"></script>
<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'); window.PracticeEditor.init('practice-code-2', 'practice-2'); });</script>
</body>
</html>