-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.xml
557 lines (267 loc) · 914 KB
/
search.xml
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>数据结构-线性表(顺序表)</title>
<link href="/posts/20241024/"/>
<url>/posts/20241024/</url>
<content type="html"><![CDATA[<h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>本篇文章将详细介绍顺序表的基本搭建过程。<br>我们都知道顺序表的底层其实就是数组,但是既然有了数组为什么还要有顺序表呢?</p><p>其实相比如数组,顺序表还是有很多优势的。比如动态扩容、增删查改效率高、支持动态元素类型、停供更多的操作方法等。顺序表相对于数组具有更高的灵活性和功能性,可以更方便地对数据进行操作和管理。</p><h2 id="数据结构"><a href="#数据结构" class="headerlink" title="数据结构"></a>数据结构</h2><p>数据结构是由“数据”和“结构”两词组成。<br>什么是数据?数据是记录事实、观察结果或描述信息的集合,通常以数字、文字、图像或声音的形式存在。<br>什么是结构?简单来说结构就是组织数据的方式。</p><blockquote><p>数据结构是指计算机存储、组织和管理数据的方式。</p></blockquote><h2 id="线性表"><a href="#线性表" class="headerlink" title="线性表"></a>线性表</h2><p>线性表是n个具有相同特性的数据元素的有限序列,线性表是一种在实际中广泛使用的数据结构,常见的线性表有:==顺序表、链表、栈、队列、字符串==……</p><p>线性表在逻辑上是线性结构,也就是连续的一条直线,但物理上并不一定连续,线性表在物理上存储时, 通常以数组和链式结构的形式存储。</p><h2 id="顺序表"><a href="#顺序表" class="headerlink" title="顺序表"></a>顺序表</h2><h3 id="为什么要有顺序表?"><a href="#为什么要有顺序表?" class="headerlink" title="为什么要有顺序表?"></a>为什么要有顺序表?</h3><blockquote><p>==顺序表的底层其实就是<code>数组</code>==。</p></blockquote><p>顺序表是线性表的一种,并且顺序表在逻辑上和物理上都是线性的。</p><p>数组就可以管理数据,为什么还要有顺序表呢?数组也可以实现一些增删查改的操作,但是实现起来比较麻烦,于是顺序表就把这些比较麻烦的操作封装好,使我们使用起来更加方便。</p><h3 id="概念及结构"><a href="#概念及结构" class="headerlink" title="概念及结构"></a>概念及结构</h3><p>顺序表是用一段<code>物理地址连续</code>的存储单元依次存储数据元素的线性结构,一般情况下采用<code>数组存储</code>。在数组上完成数据的增删查改。</p><blockquote><p>顺序表也分<code>静态顺序</code>表和<code>动态顺序表</code>。</p></blockquote><ol><li><p>静态顺序表:使用定长数组存储。</p></li><li><p>动态顺序表:使用动态开辟的数组存储。</p></li></ol><p>静态顺序表底层是<code>定长数组</code>,==空间给大了浪费,给小了不够,有缺陷==。</p><p>动态顺序表的<code>空间大小是可变的</code>,是由动态内存函数<code>realloc</code>对开辟的动态内存空间进行调整。</p><blockquote><p>我们通常使用动态顺序表。</p></blockquote><h3 id="接口实现:"><a href="#接口实现:" class="headerlink" title="接口实现:"></a>接口实现:</h3><blockquote><p>静态顺序表只适用于确定知道需要存多少数据的场景。静态顺序表的<code>定长数组导致N定大了</code>,<code>空间开多了浪费,开少了不够用</code>。所以现实中基本都是使用动态顺序表,根据需要<code>动态的分配空间大小</code>,所以下面我们<code>实现动态顺序表</code>。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 顺序表的动态存储</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">int</span> sl_data_type</span><br><span class="line">{</span><br><span class="line"> sl_data_type* arr; <span class="comment">// 指向动态开辟的数组 </span></span><br><span class="line"> <span class="type">int</span> size; <span class="comment">// 表示数组中存储了多少个数据</span></span><br><span class="line"> <span class="type">int</span> capacity; <span class="comment">// 数组实际能存数据的空间容量是多大</span></span><br><span class="line">}SL;</span><br><span class="line"></span><br><span class="line"><span class="comment">//顺序表初始化</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">sl_init</span><span class="params">(SL* ps)</span>;</span><br><span class="line"><span class="comment">//头插</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">sl_push_front</span><span class="params">(SL* ps, sl_data_type x)</span>;</span><br><span class="line"><span class="comment">//尾插</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">sl_push_back</span><span class="params">(SL* ps, sl_data_type x)</span>;</span><br><span class="line"><span class="comment">//在指定位置之前插入数据</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">sl_insert</span><span class="params">(SL* ps, <span class="type">int</span> pos, sl_data_type x)</span>;</span><br><span class="line"><span class="comment">//头删</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">sl_pop_front</span><span class="params">(SL* ps)</span>;</span><br><span class="line"><span class="comment">//尾删</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">sl_pop_back</span><span class="params">(SL* ps)</span>;</span><br><span class="line"><span class="comment">//删除指定位置的数据</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">sl_erase</span><span class="params">(SL* ps, <span class="type">int</span> pos)</span>;</span><br><span class="line"><span class="comment">//顺序表的查找</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">sl_find</span><span class="params">(SL* ps, sl_data_type x)</span>;</span><br><span class="line"><span class="comment">//顺序表打印</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">sl_print</span><span class="params">(<span class="type">const</span> SL sl)</span>;</span><br><span class="line"><span class="comment">//顺序表销毁</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">sl_destroy</span><span class="params">(SL* ps)</span>;</span><br></pre></td></tr></table></figure><h3 id="创建和初始化"><a href="#创建和初始化" class="headerlink" title="创建和初始化"></a>创建和初始化</h3><p>首先我们需要一个指针来接收由动态内存函数开辟的空间,还需要一个变量记录当前顺序表内数据个数,因为我们创建的是动态顺序表,大小经常变化,所以我们还需要一个变量来记录当前空间的大小。最后我们再把这些值封装到一个结构体中,这个结构体就是我们要创建的动态顺序表。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//顺序表管理数据的类型</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">int</span> sl_data_type;</span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">seqlist</span></span></span><br><span class="line"><span class="class">{</span></span><br><span class="line">sl_data_type* arr;</span><br><span class="line"><span class="type">int</span> size;<span class="comment">//表示数组中存储了多少个数据</span></span><br><span class="line"><span class="type">int</span> capacity;<span class="comment">//空间大小</span></span><br><span class="line">}SL;</span><br></pre></td></tr></table></figure><p>我们希望创建的顺序表能管理多种类型的数据,所以使用类型重定义标识符typedef。<br>创建好顺序表后,为了以后使用方便我们将它的初始化步骤也分装成一个函数:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">sl_init</span><span class="params">(SL* ps)</span></span><br><span class="line">{</span><br><span class="line">ps->arr = <span class="literal">NULL</span>;</span><br><span class="line">ps->size = <span class="number">0</span>;</span><br><span class="line">ps->capacity = <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>注意指针要赋<code>NULL</code>。</p><h3 id="头插"><a href="#头插" class="headerlink" title="头插"></a>头插</h3><p>创建和初始化顺序表后,我们来实现在顺序表头部插入数据。</p><p>插入数据是直接插吗?不是的,我们还需要判断当前顺序表中是否有足够的空间让我们插入数据,因为不管是哪种插入的方式都要进行判断,所以我们干脆把这一步骤分装成一个函数,方便后续使用。</p><p>如何判断当前顺序表是否有足够的空间呢?是ps->capacity>0吗?<br>不是的,因为有一种特殊情况是当前顺序表的空间刚好被使用完,合理的判断条件是当ps->size == ps->capacity时,我们申请空间。<br>但是申请空间又有一个问题摆在我们面前:申请多大?仿佛又回到了定长数组的问题。<br>不过不要慌,由数学推理得出,一次申请空间大小是原空间大小2倍最合理。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//检查是否有空间允许插入数据</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">check_capacity</span><span class="params">(SL* ps)</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span> (ps->size == ps->capacity)</span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> newcapacity = ps->capacity == <span class="number">0</span> ? <span class="number">4</span> : <span class="number">2</span> * ps->capacity;<span class="comment">//为了处理capacity为0的问题</span></span><br><span class="line">sl_data_type* tmp = (sl_data_type*)<span class="built_in">realloc</span>(ps->arr, newcapacity * <span class="keyword">sizeof</span>(sl_data_type));</span><br><span class="line"><span class="keyword">if</span> (tmp == <span class="literal">NULL</span>)</span><br><span class="line">{</span><br><span class="line">perror(<span class="string">"realloc fail!"</span>);</span><br><span class="line"><span class="built_in">exit</span>(<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line">ps->arr = tmp;</span><br><span class="line">tmp = <span class="literal">NULL</span>;</span><br><span class="line">ps->capacity = newcapacity;<span class="comment">//及时更新空间大小</span></span><br><span class="line">}</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>空间大小需要乘以相应类型的大小。<br>在使用realloc函数时不要忘了其返回值也有为NULL的可能,所以需要一个临时指针过渡,这个临时指针使用完也不要忘了赋NULL,以防止其成为野指针。<br>顺序表的空间大小最后也不要忘了及时更新。<br>判断是否有足够的空间后,接下来就是在顺序表的头部插入数据。<br>我们先要将原先的数据向后挪动一位,将顺序表的第一位空出来,插入我们想插入的数据。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">sl_push_front</span><span class="params">(SL* ps, sl_data_type x)</span></span><br><span class="line">{</span><br><span class="line">assert(ps != <span class="literal">NULL</span>);</span><br><span class="line">check_capacity(ps);</span><br><span class="line"><span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">for</span> (i = ps->size; i > <span class="number">0</span>; i--)</span><br><span class="line">{</span><br><span class="line">ps->arr[i] = ps->arr[i - <span class="number">1</span>];</span><br><span class="line">}</span><br><span class="line">ps->arr[<span class="number">0</span>] = x;</span><br><span class="line">ps->size++;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>开始插入操作之前,为了防止意外我们先判断一下传过来的顺序表的指针是否为空。<br>最后不要忘了给顺序表内记录数据个数的变量<code>++</code>。</p><h3 id="尾插"><a href="#尾插" class="headerlink" title="尾插"></a>尾插</h3><p>相比于头插,尾插没有挪动原有数据的操作,在判断完空间大小和数据个数后直接在数据末尾插入就行,同样也不要忘了让记录数据个数的变量<code>++</code></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">sl_push_back</span><span class="params">(SL* ps, sl_data_type x)</span></span><br><span class="line">{</span><br><span class="line">assert(ps != <span class="literal">NULL</span>);</span><br><span class="line">check_capacity(ps);</span><br><span class="line">ps->arr[ps->size++] = x;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h3 id="在指定位置前插入"><a href="#在指定位置前插入" class="headerlink" title="在指定位置前插入"></a>在指定位置前插入</h3><p>顺序表中不止在头部和尾部插入数据,也可在指定的任意有效位置插入数据,所以我们的函数就要多一个指定位置的参数。<br>指定的位置还必须要有效,因为顺序表中的数据一定是连续的。<br>插入之前,我们需要将指定位置后面的数据往后挪动一位,给要插入的数据留出空间。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">sl_insert</span><span class="params">(SL* ps, <span class="type">int</span> pos, sl_data_type x)</span></span><br><span class="line">{</span><br><span class="line">assert(ps != <span class="literal">NULL</span>);</span><br><span class="line">assert(pos >= <span class="number">0</span> && pos <= ps->size);<span class="comment">//确保指定的位置是有效的</span></span><br><span class="line">check_capacity(ps);</span><br><span class="line"><span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">for</span> (i = ps->size; i > pos; i--)</span><br><span class="line">{</span><br><span class="line">ps->arr[i] = ps->arr[i - <span class="number">1</span>];</span><br><span class="line">}</span><br><span class="line">ps->arr[pos] = x;</span><br><span class="line">ps->size++;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>插入数据后,记录数据个数的变量不要忘了<code>++</code>。</p><h3 id="头删"><a href="#头删" class="headerlink" title="头删"></a>头删</h3><p>在删除完顺序表的第一个数据后,也需要将剩余的数据向前挪动一位,以确保数据是在下标为0处开始。<br>在删除数据之前,我们还要考虑到一种特殊情况,就是当前顺序表中没有数据,那没有数据肯定是不能进行删除操作的。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">sl_pop_front</span><span class="params">(SL* ps)</span></span><br><span class="line">{</span><br><span class="line">assert(ps != <span class="literal">NULL</span>);</span><br><span class="line">assert(ps->size != <span class="number">0</span>);<span class="comment">//顺序表为空不能删除</span></span><br><span class="line"><span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">for</span> (i = <span class="number">0</span>; i < ps->size - <span class="number">1</span>; i++)</span><br><span class="line">{</span><br><span class="line">ps->arr[i] = ps->arr[i + <span class="number">1</span>];</span><br><span class="line">}</span><br><span class="line">ps->size--;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>删除操作完成后,数据个数<code>–</code>;</p><h3 id="尾删"><a href="#尾删" class="headerlink" title="尾删"></a>尾删</h3><p>尾删则比较简单,因为我们只需要让记录数据个数的变量-1,在访问顺序表的时候访问不到这个数据,就相当于删除了这个数据<br>同样的,尾删也需要考虑当前数据个数是否为0的情况。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">sl_pop_back</span><span class="params">(SL* ps)</span></span><br><span class="line">{</span><br><span class="line">assert(ps != <span class="literal">NULL</span>);</span><br><span class="line">assert(ps->size != <span class="number">0</span>);<span class="comment">//顺序表为空不能删除</span></span><br><span class="line">ps->size--;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h3 id="删除指定位置的数据"><a href="#删除指定位置的数据" class="headerlink" title="删除指定位置的数据"></a>删除指定位置的数据</h3><p>这个指定的位置也必须是有效的。<br>同时也要保证当前顺序表中的数据个数不为0。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">sl_erase</span><span class="params">(SL* ps, <span class="type">int</span> pos)</span></span><br><span class="line">{</span><br><span class="line">assert(ps != <span class="literal">NULL</span>);</span><br><span class="line">assert(ps->size != <span class="number">0</span>);<span class="comment">//实际下面的断言侧面完成了这句代码</span></span><br><span class="line">assert(pos >= <span class="number">0</span> && pos < ps->size);<span class="comment">//确保指定的位置是有效的</span></span><br><span class="line"><span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">for</span> (i = pos; i < ps->size - <span class="number">1</span>; i++)</span><br><span class="line">{</span><br><span class="line">ps->arr[i] = ps->arr[i + <span class="number">1</span>];</span><br><span class="line">}</span><br><span class="line">ps->size--;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h3 id="顺序表的查找"><a href="#顺序表的查找" class="headerlink" title="顺序表的查找"></a>顺序表的查找</h3><p>想要在顺序表中查找一个数据,只需要像遍历数组一样遍历顺序表就行。<br>找到则返回数据对应的下标,找不到则返回-1。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">sl_find</span><span class="params">(SL* ps, sl_data_type x)</span></span><br><span class="line">{</span><br><span class="line">assert(ps != <span class="literal">NULL</span>);</span><br><span class="line"><span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">for</span> (i = <span class="number">0</span>; i < ps->size; i++)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span> (ps->arr[i] == x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">return</span> i;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h3 id="顺序表销毁"><a href="#顺序表销毁" class="headerlink" title="顺序表销毁"></a>顺序表销毁</h3><p>我们向内存申请了一块空间使用完成后,还要归还给操作系统<br>在上面调整顺序表大小的操作中,我们使用的是动态内存函数realloc,因此还要使用函数free释放掉动态开辟的空间,也不要忘了给指针赋NULL</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">sl_destroy</span><span class="params">(SL* ps)</span></span><br><span class="line">{</span><br><span class="line">assert(ps != <span class="literal">NULL</span>);</span><br><span class="line"><span class="keyword">if</span> (ps->arr != <span class="literal">NULL</span>)<span class="comment">//动态内存函数开辟了空间</span></span><br><span class="line">{</span><br><span class="line"><span class="built_in">free</span>(ps->arr);</span><br><span class="line">}</span><br><span class="line">ps->arr = <span class="literal">NULL</span>;</span><br><span class="line">ps->size = <span class="number">0</span>;</span><br><span class="line">ps->capacity = <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h3 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h3><p>为了对我们创建的顺序表进行测试,方便起见再写一个打印函数。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">sl_print</span><span class="params">(<span class="type">const</span> SL sl)</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">for</span> (i = <span class="number">0</span>; i < sl.size; i++)</span><br><span class="line">{</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d "</span>, sl.arr[i]);</span><br><span class="line">}</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>测试代码如下:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">test</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line">SL sl;</span><br><span class="line">sl_init(&sl);</span><br><span class="line">sl_push_front(&sl, <span class="number">1</span>);</span><br><span class="line">sl_push_front(&sl, <span class="number">2</span>);</span><br><span class="line">sl_push_front(&sl, <span class="number">3</span>);</span><br><span class="line">sl_print(sl);</span><br><span class="line"></span><br><span class="line">sl_push_back(&sl, <span class="number">4</span>);</span><br><span class="line">sl_push_back(&sl, <span class="number">5</span>);</span><br><span class="line">sl_push_back(&sl, <span class="number">6</span>);</span><br><span class="line">sl_print(sl);</span><br><span class="line"></span><br><span class="line">sl_pop_front(&sl);</span><br><span class="line">sl_print(sl);</span><br><span class="line"></span><br><span class="line">sl_pop_back(&sl);</span><br><span class="line">sl_print(sl);</span><br><span class="line"></span><br><span class="line">sl_pop_front(&sl);</span><br><span class="line">sl_print(sl);</span><br><span class="line"></span><br><span class="line">sl_pop_back(&sl);</span><br><span class="line">sl_print(sl);</span><br><span class="line"></span><br><span class="line">sl_pop_front(&sl);</span><br><span class="line">sl_print(sl);</span><br><span class="line"></span><br><span class="line">sl_pop_back(&sl);</span><br><span class="line">sl_print(sl);</span><br><span class="line"></span><br><span class="line">sl_destroy(&sl);</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>运行结果:</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/20241024/image.png" alt="数据结构-线性表(顺序表)"></p><p>结果和我们预期的效果一致。<br>整个程序的原码如下:</p><p><code>seqlist.h</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">pragma</span> once</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdlib.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><assert.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">//顺序表管理数据的类型</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">int</span> sl_data_type;</span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">seqlist</span></span></span><br><span class="line"><span class="class">{</span></span><br><span class="line">sl_data_type* arr;</span><br><span class="line"><span class="type">int</span> size;<span class="comment">//数据个数</span></span><br><span class="line"><span class="type">int</span> capacity;<span class="comment">//空间大小</span></span><br><span class="line">}SL;</span><br><span class="line"></span><br><span class="line"><span class="comment">//顺序表初始化</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">sl_init</span><span class="params">(SL* ps)</span>;</span><br><span class="line"><span class="comment">//头插</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">sl_push_front</span><span class="params">(SL* ps, sl_data_type x)</span>;</span><br><span class="line"><span class="comment">//尾插</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">sl_push_back</span><span class="params">(SL* ps, sl_data_type x)</span>;</span><br><span class="line"><span class="comment">//在指定位置之前插入数据</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">sl_insert</span><span class="params">(SL* ps, <span class="type">int</span> pos, sl_data_type x)</span>;</span><br><span class="line"><span class="comment">//头删</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">sl_pop_front</span><span class="params">(SL* ps)</span>;</span><br><span class="line"><span class="comment">//尾删</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">sl_pop_back</span><span class="params">(SL* ps)</span>;</span><br><span class="line"><span class="comment">//删除指定位置的数据</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">sl_erase</span><span class="params">(SL* ps, <span class="type">int</span> pos)</span>;</span><br><span class="line"><span class="comment">//顺序表的查找</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">sl_find</span><span class="params">(SL* ps, sl_data_type x)</span>;</span><br><span class="line"><span class="comment">//顺序表打印</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">sl_print</span><span class="params">(<span class="type">const</span> SL sl)</span>;</span><br><span class="line"><span class="comment">//顺序表销毁</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">sl_destroy</span><span class="params">(SL* ps)</span>;</span><br></pre></td></tr></table></figure><p><code>seqlist.c</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> _CRT_SECURE_NO_WARNINGS</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">"seqlist.h"</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">sl_init</span><span class="params">(SL* ps)</span></span><br><span class="line">{</span><br><span class="line">ps->arr = <span class="literal">NULL</span>;</span><br><span class="line">ps->size = <span class="number">0</span>;</span><br><span class="line">ps->capacity = <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">//检查是否有空间允许插入数据</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">check_capacity</span><span class="params">(SL* ps)</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span> (ps->size == ps->capacity)</span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> newcapacity = ps->capacity == <span class="number">0</span> ? <span class="number">4</span> : <span class="number">2</span> * ps->capacity;<span class="comment">//为了处理capacity为0的问题</span></span><br><span class="line">sl_data_type* tmp = (sl_data_type*)<span class="built_in">realloc</span>(ps->arr, newcapacity * <span class="keyword">sizeof</span>(sl_data_type));</span><br><span class="line"><span class="keyword">if</span> (tmp == <span class="literal">NULL</span>)</span><br><span class="line">{</span><br><span class="line">perror(<span class="string">"realloc fail!"</span>);</span><br><span class="line"><span class="built_in">exit</span>(<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line">ps->arr = tmp;</span><br><span class="line">tmp = <span class="literal">NULL</span>;</span><br><span class="line">ps->capacity = newcapacity;<span class="comment">//及时更新空间大小</span></span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">sl_push_front</span><span class="params">(SL* ps, sl_data_type x)</span></span><br><span class="line">{</span><br><span class="line">assert(ps != <span class="literal">NULL</span>);</span><br><span class="line">check_capacity(ps);</span><br><span class="line"><span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">for</span> (i = ps->size; i > <span class="number">0</span>; i--)</span><br><span class="line">{</span><br><span class="line">ps->arr[i] = ps->arr[i - <span class="number">1</span>];</span><br><span class="line">}</span><br><span class="line">ps->arr[<span class="number">0</span>] = x;</span><br><span class="line">ps->size++;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">sl_push_back</span><span class="params">(SL* ps, sl_data_type x)</span></span><br><span class="line">{</span><br><span class="line">assert(ps != <span class="literal">NULL</span>);</span><br><span class="line">check_capacity(ps);</span><br><span class="line">ps->arr[ps->size++] = x;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">sl_insert</span><span class="params">(SL* ps, <span class="type">int</span> pos, sl_data_type x)</span></span><br><span class="line">{</span><br><span class="line">assert(ps != <span class="literal">NULL</span>);</span><br><span class="line">assert(pos >= <span class="number">0</span> && pos <= ps->size);<span class="comment">//确保指定的位置是有效的</span></span><br><span class="line">check_capacity(ps);</span><br><span class="line"><span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">for</span> (i = ps->size; i > pos; i--)</span><br><span class="line">{</span><br><span class="line">ps->arr[i] = ps->arr[i - <span class="number">1</span>];</span><br><span class="line">}</span><br><span class="line">ps->arr[pos] = x;</span><br><span class="line">ps->size++;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">sl_pop_front</span><span class="params">(SL* ps)</span></span><br><span class="line">{</span><br><span class="line">assert(ps != <span class="literal">NULL</span>);</span><br><span class="line">assert(ps->size != <span class="number">0</span>);<span class="comment">//顺序表为空不能删除</span></span><br><span class="line"><span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">for</span> (i = <span class="number">0</span>; i < ps->size - <span class="number">1</span>; i++)</span><br><span class="line">{</span><br><span class="line">ps->arr[i] = ps->arr[i + <span class="number">1</span>];</span><br><span class="line">}</span><br><span class="line">ps->size--;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">sl_pop_back</span><span class="params">(SL* ps)</span></span><br><span class="line">{</span><br><span class="line">assert(ps != <span class="literal">NULL</span>);</span><br><span class="line">assert(ps->size != <span class="number">0</span>);<span class="comment">//顺序表为空不能删除</span></span><br><span class="line">ps->size--;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">sl_erase</span><span class="params">(SL* ps, <span class="type">int</span> pos)</span></span><br><span class="line">{</span><br><span class="line">assert(ps != <span class="literal">NULL</span>);</span><br><span class="line">assert(ps->size != <span class="number">0</span>);</span><br><span class="line">assert(pos >= <span class="number">0</span> && pos < ps->size);<span class="comment">//确保指定的位置是有效的</span></span><br><span class="line"><span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">for</span> (i = pos; i < ps->size - <span class="number">1</span>; i++)</span><br><span class="line">{</span><br><span class="line">ps->arr[i] = ps->arr[i + <span class="number">1</span>];</span><br><span class="line">}</span><br><span class="line">ps->size--;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">sl_find</span><span class="params">(SL* ps, sl_data_type x)</span></span><br><span class="line">{</span><br><span class="line">assert(ps != <span class="literal">NULL</span>);</span><br><span class="line"><span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">for</span> (i = <span class="number">0</span>; i < ps->size; i++)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span> (ps->arr[i] == x)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">return</span> i;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">sl_print</span><span class="params">(<span class="type">const</span> SL sl)</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">for</span> (i = <span class="number">0</span>; i < sl.size; i++)</span><br><span class="line">{</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d "</span>, sl.arr[i]);</span><br><span class="line">}</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">sl_destroy</span><span class="params">(SL* ps)</span></span><br><span class="line">{</span><br><span class="line">assert(ps != <span class="literal">NULL</span>);</span><br><span class="line"><span class="keyword">if</span> (ps->arr != <span class="literal">NULL</span>)<span class="comment">//动态内存函数开辟了空间</span></span><br><span class="line">{</span><br><span class="line"><span class="built_in">free</span>(ps->arr);</span><br><span class="line">}</span><br><span class="line">ps->arr = <span class="literal">NULL</span>;</span><br><span class="line">ps->size = <span class="number">0</span>;</span><br><span class="line">ps->capacity = <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>test.c</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> _CRT_SECURE_NO_WARNINGS</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">"seqlist.h"</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">test</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line">SL sl;</span><br><span class="line">sl_init(&sl);</span><br><span class="line">sl_push_front(&sl, <span class="number">1</span>);</span><br><span class="line">sl_push_front(&sl, <span class="number">2</span>);</span><br><span class="line">sl_push_front(&sl, <span class="number">3</span>);</span><br><span class="line">sl_print(sl);</span><br><span class="line"></span><br><span class="line">sl_push_back(&sl, <span class="number">4</span>);</span><br><span class="line">sl_push_back(&sl, <span class="number">5</span>);</span><br><span class="line">sl_push_back(&sl, <span class="number">6</span>);</span><br><span class="line">sl_print(sl);</span><br><span class="line"></span><br><span class="line">sl_pop_front(&sl);</span><br><span class="line">sl_print(sl);</span><br><span class="line"></span><br><span class="line">sl_pop_back(&sl);</span><br><span class="line">sl_print(sl);</span><br><span class="line"></span><br><span class="line">sl_pop_front(&sl);</span><br><span class="line">sl_print(sl);</span><br><span class="line"></span><br><span class="line">sl_pop_back(&sl);</span><br><span class="line">sl_print(sl);</span><br><span class="line"></span><br><span class="line">sl_pop_front(&sl);</span><br><span class="line">sl_print(sl);</span><br><span class="line"></span><br><span class="line">sl_pop_back(&sl);</span><br><span class="line">sl_print(sl);</span><br><span class="line"></span><br><span class="line">sl_insert(&sl, <span class="number">0</span>, <span class="number">4</span>);</span><br><span class="line">sl_print(sl);</span><br><span class="line"></span><br><span class="line">sl_erase(&sl, <span class="number">1</span>);</span><br><span class="line">sl_print(sl);</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> ret = sl_find(&sl, <span class="number">3</span>);</span><br><span class="line"><span class="keyword">if</span> (ret >= <span class="number">0</span>)</span><br><span class="line">{</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d\n"</span>, sl.arr[ret]);</span><br><span class="line">}</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">{</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"没找到"</span>);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">sl_destroy(&sl);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line">test();</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><ul><li><p>我们常用的是动态顺序表,通过realloc函数来对空间大小适当的扩容</p></li><li><p>顺序表中的元素在内存中是连续存储的,可以通过下标直接访问元素,提高了查找效率</p></li><li><p>适合: 频繁访问、很少插入和删除的数据集合。由于顺序表支持通过下标直接访问元素,适合频繁读取和遍历元素的场景</p></li><li><p>不适合: 频繁插入和删除的场景。由于插入和删除操作需要移动数据,频繁插入和删除会影响性能,不适合该类场景</p></li></ul>]]></content>
<categories>
<category> 数据结构 </category>
</categories>
<tags>
<tag> 数据结构知识点总结 </tag>
</tags>
</entry>
<entry>
<title>【JavaSE基础】变量</title>
<link href="/posts/10000/"/>
<url>/posts/10000/</url>
<content type="html"><![CDATA[<h1 id="为什么需要变量"><a href="#为什么需要变量" class="headerlink" title="为什么需要变量"></a>为什么需要变量</h1><h2 id="变量是程序的基本组成单位"><a href="#变量是程序的基本组成单位" class="headerlink" title="变量是程序的基本组成单位"></a>变量是程序的基本组成单位</h2><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/10000/image.png" alt="【JavaSE基础】变量"></p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Test</span>{</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span>{</span><br><span class="line"> <span class="type">int</span> <span class="variable">a</span> <span class="operator">=</span> <span class="number">1</span>; <span class="comment">//定义一个变量,类型为int,变量名为a,并赋值为1</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">b</span> <span class="operator">=</span> <span class="number">3</span>; <span class="comment">//定义另一个变量,类型为int,变量名为b,并赋值为3</span></span><br><span class="line"> b = <span class="number">89</span>; <span class="comment">//把89赋值给b变量</span></span><br><span class="line"> System.out.println(a); <span class="comment">//输出a变量的值</span></span><br><span class="line"> System.out.println(b); <span class="comment">//输出b变量的值</span></span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="简单的原理示意图"><a href="#简单的原理示意图" class="headerlink" title="简单的原理示意图"></a>简单的原理示意图</h2><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/10000/image-1.png" alt="【JavaSE基础】变量"></p><h1 id="变-变化-量-值-的介绍"><a href="#变-变化-量-值-的介绍" class="headerlink" title="变(变化)量(值)的介绍"></a>变(变化)量(值)的介绍</h1><h2 id="概念"><a href="#概念" class="headerlink" title="概念"></a>概念</h2><blockquote><p>变量相当于内存中一个数据存储空间的表示,你可以把变量看做是一个房间的门牌号,通过门牌号我们可以找到房间,而通过变量名可以访问到变量(值)。</p></blockquote><h2 id="变量使用的基本步骤"><a href="#变量使用的基本步骤" class="headerlink" title="变量使用的基本步骤"></a>变量使用的基本步骤</h2><ul><li>声明变量</li></ul><p>int a;</p><ul><li>赋值</li></ul><p>a = 60; //应该这么说: 把 60 赋给 a 使用 System.out.println(a);</p><h1 id="变量快速入门"><a href="#变量快速入门" class="headerlink" title="变量快速入门"></a>变量快速入门</h1><blockquote><p>看演示并对代码进行说明, 演示记录 人的信息的代码</p></blockquote><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Var02</span> { </span><br><span class="line"></span><br><span class="line"><span class="comment">//编写一个main方法</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"><span class="comment">//记录人的信息</span></span><br><span class="line"><span class="type">int</span> <span class="variable">age</span> <span class="operator">=</span> <span class="number">30</span>;</span><br><span class="line"><span class="type">double</span> <span class="variable">score</span> <span class="operator">=</span> <span class="number">88.9</span>;</span><br><span class="line"><span class="type">char</span> <span class="variable">gender</span> <span class="operator">=</span> <span class="string">'男'</span>;</span><br><span class="line"><span class="type">String</span> <span class="variable">name</span> <span class="operator">=</span> <span class="string">"king"</span>;</span><br><span class="line"><span class="comment">//输出信息, 快捷键</span></span><br><span class="line">System.out.println(<span class="string">"人的信息如下:"</span>);</span><br><span class="line">System.out.println(name);</span><br><span class="line">System.out.println(age);</span><br><span class="line">System.out.println(score);</span><br><span class="line">System.out.println(gender);</span><br><span class="line">}</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h1 id="变量使用注意事项"><a href="#变量使用注意事项" class="headerlink" title="变量使用注意事项"></a>变量使用注意事项</h1><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/10000/image-2.png" alt="【JavaSE基础】变量"></p><blockquote><p>举例:</p></blockquote><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">VarDetail</span> { </span><br><span class="line"></span><br><span class="line"><span class="comment">//编写一个main方法</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"><span class="comment">//变量必须先声明,后使用, 即有顺序</span></span><br><span class="line"><span class="type">int</span> <span class="variable">a</span> <span class="operator">=</span> <span class="number">50</span>;<span class="comment">//int</span></span><br><span class="line">System.out.println(a);<span class="comment">//50</span></span><br><span class="line"><span class="comment">//该区域的数据/值可以在同一类型范围内不断变化</span></span><br><span class="line"><span class="comment">//a = "jack"; //×</span></span><br><span class="line">a = <span class="number">88</span>; <span class="comment">//对</span></span><br><span class="line">System.out.println(a);<span class="comment">//88</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//变量在同一个作用域内不能重名</span></span><br><span class="line"><span class="comment">//int a = 77;//错误</span></span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">//另外一个类</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Dog</span> {</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"><span class="type">int</span> <span class="variable">a</span> <span class="operator">=</span> <span class="number">666</span>;<span class="comment">//对</span></span><br><span class="line">}</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h1 id="程序中的-号的使用"><a href="#程序中的-号的使用" class="headerlink" title="程序中的 + 号的使用"></a>程序中的 + 号的使用</h1><ol><li><p>当 <code>+</code> 号两边<code>都是数值类型</code>时,则做<code>加法运算</code></p></li><li><p>当 <code>+</code> 号两边有一个是<code>字符串类型</code>,则做<code>字符串拼接</code></p></li></ol><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">System.out.println(<span class="number">100</span> + <span class="number">98</span>); ——> <span class="number">198</span></span><br><span class="line">System.out.println(<span class="string">"100"</span> + <span class="number">98</span>); ——> <span class="number">10098</span></span><br><span class="line"></span><br><span class="line">System.out.println(<span class="number">100</span> + <span class="number">3</span> + <span class="string">"hello"</span>); ——> 103hello</span><br><span class="line">System.out.println(<span class="string">"hello"</span> + <span class="number">100</span> + <span class="number">3</span>); ——> hello1003</span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> JavaSE </category>
</categories>
<tags>
<tag> JavaSE知识点总结 </tag>
</tags>
</entry>
<entry>
<title>数据结构-绪论</title>
<link href="/posts/20240907/"/>
<url>/posts/20240907/</url>
<content type="html"><![CDATA[<h1 id="数据结构的基本概念和术语"><a href="#数据结构的基本概念和术语" class="headerlink" title="数据结构的基本概念和术语"></a>数据结构的基本概念和术语</h1><h2 id="数据,数据元素,数据项,数据对象"><a href="#数据,数据元素,数据项,数据对象" class="headerlink" title="数据,数据元素,数据项,数据对象"></a>数据,数据元素,数据项,数据对象</h2><ul><li><strong>数据(Data)</strong>:<code>是客观事物的符号化表示,在计算机科学中指的是所有能输入到计算机程序中并被计算机程序处理的符号的总称</code>。</li></ul><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/20240907/image.png" alt="数据结构-绪论"></p><ul><li><strong>数据元素(Data Element)</strong>:是数据的基本单位,在程序中通常<code>作为一个整体</code>来进行考虑和处理。</li></ul><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/20240907/image-1.png" alt="数据结构-绪论"></p><ul><li><strong>数据项(Data Item)</strong>:是指数据元素的组成部分,如整数、实数、字符、字符串等。数据项对客观事物某一方面特性的数据描述。</li></ul><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/20240907/image-2.png" alt="数据结构-绪论"></p><ul><li><strong>数据对象(Data Object)</strong>:是指数据元素的集合,如数组、链表、树、图等。</li></ul><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/20240907/image-3.png" alt="数据结构-绪论"></p><blockquote><p><code>数据元素与数据对象的关系</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/20240907/image-4.png" alt="数据结构-绪论"></p><h1 id="数据结构-Data-Structure"><a href="#数据结构-Data-Structure" class="headerlink" title="数据结构(Data Structure)"></a>数据结构(Data Structure)</h1><p><code>概念</code>:数据结构(Data Structure)是<code>相互之间存在一种或多种特定关系的数据元素的集合</code>,换句话说,<strong>数据结构是带”结构”的数据元素的集合</strong>,**”结构”就是指数据元素之间的相互关系**。</p><blockquote><p>数据结构包括以下三个方面的内容:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/20240907/image-5.png" alt="数据结构-绪论"></p><h2 id="数据结构的两个层次"><a href="#数据结构的两个层次" class="headerlink" title="数据结构的两个层次"></a>数据结构的两个层次</h2><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/20240907/image-6.png" alt="数据结构-绪论"></p><h2 id="逻辑结构"><a href="#逻辑结构" class="headerlink" title="逻辑结构"></a>逻辑结构</h2><p><code>概念</code>:逻辑结构(Logical Structure)是从逻辑关系上描述数据,它与物理结构无关,是独立于计算机的。</p><ul><li><code>以线性结构和非线性结构划分</code></li></ul><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/20240907/image-7.png" alt="数据结构-绪论"></p><ul><li><code>以集合、线性结构、树形结构、图形结构划分</code></li></ul><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/20240907/image-8.png" alt="数据结构-绪论"></p><h2 id="存储结构"><a href="#存储结构" class="headerlink" title="存储结构"></a>存储结构</h2><p><code>概念</code>:存储结构(Storage Structure)是指数据对象在计算机中的<code>存储结构</code>,也称为<code>物理结构</code>。</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/20240907/image-9.png" alt="数据结构-绪论"></p><ul><li><code>以顺序存储结构和链式存储结构划分</code></li></ul><blockquote><p>顺序存储结构:数据元素按<code>先后顺序</code>存储在计算机的<code>连续存储区</code>中。</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/20240907/image-11.png" alt="数据结构-绪论"></p><blockquote><p>链式存储结构:<code>我们在存储没有元素的本身的时候也存储了下一个元素的首地址</code>。</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/20240907/image-10.png" alt="数据结构-绪论"></p><ul><li><code>以索引存储结构和散列存储结构划分</code></li></ul><blockquote><p>索引存储结构:在存储结点信息的同时,还建立附加的<code>索引表</code>。</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/20240907/image-12.png" alt="数据结构-绪论"></p><blockquote><p>散列存储结构:根据结点的关键字<code>直接计算出该结点的存储地址</code>。</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/20240907/image-13.png" alt="数据结构-绪论"></p>]]></content>
<categories>
<category> 数据结构 </category>
</categories>
<tags>
<tag> 数据结构知识点总结 </tag>
</tags>
</entry>
<entry>
<title>【JavaSE基础】Java概述-章节作业</title>
<link href="/posts/18951/"/>
<url>/posts/18951/</url>
<content type="html"><![CDATA[<h1 id="Java概述-章节作业题目"><a href="#Java概述-章节作业题目" class="headerlink" title="Java概述-章节作业题目"></a>Java概述-章节作业题目</h1><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/18951/image.png" alt="alt text"></p><h2 id="编写hello-world程序-Homework01-java"><a href="#编写hello-world程序-Homework01-java" class="headerlink" title="编写hello world程序[Homework01.java]"></a>编写hello world程序[Homework01.java]</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Homework01</span> { </span><br><span class="line"></span><br><span class="line"> <span class="comment">//编写一个main方法</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="comment">//编写hello, world程序</span></span><br><span class="line"> System.out.println(<span class="string">"hello,world"</span>); </span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="将个人的基本信息(姓名、性别、籍贯、住址)打印到控制台上输出。各条信息分别占一行-Homework02-java"><a href="#将个人的基本信息(姓名、性别、籍贯、住址)打印到控制台上输出。各条信息分别占一行-Homework02-java" class="headerlink" title="将个人的基本信息(姓名、性别、籍贯、住址)打印到控制台上输出。各条信息分别占一行[Homework02.java]"></a>将个人的基本信息(姓名、性别、籍贯、住址)打印到控制台上输出。各条信息分别占一行[Homework02.java]</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//将个人的基本信息(姓名、性别、籍贯、住址)打印到控制台上输出。各条信息分别占一行</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Homework02</span> { </span><br><span class="line"></span><br><span class="line"><span class="comment">//编写一个main方法</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"><span class="comment">//考察队转义字符</span></span><br><span class="line">System.out.println(<span class="string">"姓名\t性别\t籍贯\t住址\n顺平\t男\t四川\t北京"</span>); </span><br><span class="line">}</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="JDK-JRE-JVM的关系-Homework03-java"><a href="#JDK-JRE-JVM的关系-Homework03-java" class="headerlink" title="JDK,JRE,JVM的关系[Homework03.java]"></a>JDK,JRE,JVM的关系[Homework03.java]</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">答:</span><br><span class="line"><span class="number">1.</span> JDK = JRE + java开发工具</span><br><span class="line"><span class="number">2.</span> JRE = JVM + 核心类库</span><br></pre></td></tr></table></figure><h2 id="环境变量path配置及其作用-Homework04-java"><a href="#环境变量path配置及其作用-Homework04-java" class="headerlink" title="环境变量path配置及其作用[Homework04.java]"></a>环境变量path配置及其作用[Homework04.java]</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 环境变量的作用是为了在dos的任意目录,可以去使用java 和 javac命令</span><br><span class="line"><span class="number">2.</span> 先配置 JAVA_HOME = 指向jdk安装的主目录</span><br><span class="line"><span class="number">3.</span> 编辑path环境变量,增加 %JAVA_HOME%\bin </span><br></pre></td></tr></table></figure><h2 id="Java编写步骤-Homework05-java"><a href="#Java编写步骤-Homework05-java" class="headerlink" title="Java编写步骤[Homework05.java]"></a>Java编写步骤[Homework05.java]</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 编写java的源代码</span><br><span class="line"><span class="number">2.</span> javac 编译 ,得到对应的 .class 字节码文件</span><br><span class="line"><span class="number">3.</span> java 运行, 本质就是把 .class 加载到jvm 运行</span><br></pre></td></tr></table></figure><h2 id="Java编写7个规范-Homework06-java"><a href="#Java编写7个规范-Homework06-java" class="headerlink" title="Java编写7个规范[Homework06.java]"></a>Java编写7个规范[Homework06.java]</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 类,方法的注释,使用javadoc的方式,即文档注释</span><br><span class="line"><span class="number">2.</span> 非javadoc注释,往往是对代码的说明(给程序的维护者),说明如何修改,注意事项</span><br><span class="line"><span class="number">3.</span> 使用tab ,整体将代码右移, 使用 shift+tab 整体左移</span><br><span class="line"><span class="number">4.</span> 运算符和 = 的两边,给空格,代码看上去清楚 <span class="type">int</span> <span class="variable">n</span> <span class="operator">=</span> <span class="number">1</span> + <span class="number">4</span>;</span><br><span class="line"><span class="number">5.</span> 源码文件使用 utf-<span class="number">8</span>编码</span><br><span class="line"><span class="number">6.</span> 行宽字符不要超过 <span class="number">80</span></span><br><span class="line"><span class="number">7.</span> 代码编程风格有两种 次行风格,行尾风格(推荐)</span><br></pre></td></tr></table></figure><h2 id="初学者java易犯错误-Homework07-java"><a href="#初学者java易犯错误-Homework07-java" class="headerlink" title="初学者java易犯错误[Homework07.java]"></a>初学者java易犯错误[Homework07.java]</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 编译或者运行时,找不到文件 javac Hell0.java, 把文件名或者目录找对</span><br><span class="line"><span class="number">2.</span> 主类名和文件名不一致 , 修改时保持一致即可</span><br><span class="line"><span class="number">3.</span> 缺少;</span><br><span class="line"><span class="number">4.</span> 拼写错误,比如 <span class="number">1</span> -> l <span class="number">0</span>-><span class="number">0</span>, <span class="keyword">void</span> -> viod , 要求写代码时,一定要小心</span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> JavaSE </category>
</categories>
<tags>
<tag> JavaSE知识点总结 </tag>
</tags>
</entry>
<entry>
<title>【JavaSE基础】Java 基础知识</title>
<link href="/posts/64063/"/>
<url>/posts/64063/</url>
<content type="html"><![CDATA[<h1 id="Java-转义字符"><a href="#Java-转义字符" class="headerlink" title="Java 转义字符"></a>Java 转义字符</h1><h2 id="Java-常用的转义字符"><a href="#Java-常用的转义字符" class="headerlink" title="Java 常用的转义字符"></a>Java 常用的转义字符</h2><blockquote><p>在控制台,输入 tab 键,可以实现命令补全</p></blockquote><table><thead><tr><th>转义字符</th><th>含义</th><th>作用</th></tr></thead><tbody><tr><td>\t</td><td>制表符</td><td>一个<code>制表位</code>,实现<code>对齐的功能\n</code> :<code>换行符</code></td></tr><tr><td>\n</td><td>换行符</td><td>一个<code>换行符</code></td></tr><tr><td>\r</td><td>回车符</td><td>一个<code>回车键</code> System.out.println(“韩顺平教育\r 北京”);</td></tr><tr><td>"</td><td>双引号</td><td>一个<code>"</code></td></tr><tr><td>'</td><td>单引号</td><td>一个<code>'</code></td></tr><tr><td>\</td><td>反斜杠</td><td>一个<code>\</code></td></tr></tbody></table><h2 id="Java-转义字符的使用"><a href="#Java-转义字符的使用" class="headerlink" title="Java 转义字符的使用"></a>Java 转义字符的使用</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//演示转义字符的使用</span></span><br><span class="line"><span class="keyword">public</span> class 转义字符的使用 {</span><br><span class="line"> <span class="comment">//编写一个main方法</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"></span><br><span class="line"> <span class="comment">//\t :一个制表位,实现对齐的功能</span></span><br><span class="line"> System.out.println(<span class="string">"北京\t天津\t上海"</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// \n :换行符</span></span><br><span class="line"> System.out.println(<span class="string">"jack\nsmith\nmary"</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// \\ :一个\ \\ ——> 第一个斜杠代表转义,第二个斜杠代表反斜杠本身</span></span><br><span class="line"> System.out.println(<span class="string">"C:\\Windows\\System32\\cmd.exe"</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// \" :一个"</span></span><br><span class="line"> System.out.println(<span class="string">"老韩说:\"要好好学习java,有前途\""</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// \' :一个'</span></span><br><span class="line"> System.out.println(<span class="string">"老韩说:\'要好好学习java,有前途\'"</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// \r :一个回车 System.out.println("韩顺平教育\r北京");</span></span><br><span class="line"> <span class="comment">// 解读</span></span><br><span class="line"> <span class="comment">// 1. 输出 韩顺平教育</span></span><br><span class="line"> <span class="comment">// 2. \r表示回车</span></span><br><span class="line"> System.out.println(<span class="string">"韩顺平教育\r北京"</span>); <span class="comment">// \r会将自己前面的字符替换为后面的字符,即将“韩顺平教育”替换为“北京”</span></span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>输出结果:</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/64063/image.png" alt="【JavaSE基础】Java 基础知识"></p><h2 id="课堂练习"><a href="#课堂练习" class="headerlink" title="课堂练习"></a>课堂练习</h2><h3 id="题目"><a href="#题目" class="headerlink" title="题目"></a>题目</h3><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/64063/image-1.png" alt="【JavaSE基础】Java 基础知识"></p><h3 id="代码呈现"><a href="#代码呈现" class="headerlink" title="代码呈现"></a>代码呈现</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> class 转义字符课堂练习 {</span><br><span class="line"></span><br><span class="line"> <span class="comment">//编写一个main方法</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="comment">//完成代码</span></span><br><span class="line"> System.out.println(<span class="string">"书名\t作者\t价格\t销量\n三国\t罗贯中\t120\t1000"</span>);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>输出结果:</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/64063/image-2.png" alt="【JavaSE基础】Java 基础知识"></p><h1 id="初学Java易犯错误"><a href="#初学Java易犯错误" class="headerlink" title="初学Java易犯错误"></a>初学Java易犯错误</h1><ul><li>找不到文件</li></ul><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/64063/image-3.png" alt="【JavaSE基础】Java 基础知识"></p><blockquote><p>解决方法:源文件名不存在或者写错,或者当前路径错误</p></blockquote><ul><li>主类名和文件名不一致</li></ul><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/64063/image-4.png" alt="【JavaSE基础】Java 基础知识"></p><blockquote><p>解决方法:声明为public的主类应与文件名一致,否则编译失败</p></blockquote><ul><li>缺少分号</li></ul><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/64063/image-5.png" alt="【JavaSE基础】Java 基础知识"></p><blockquote><p>解决方法:编译失败,注意错误出现的行数,再到源代码中指定位置改错</p></blockquote><ul><li>常见错误总结</li></ul><blockquote><p>学习编程最容易犯的错是语法错误<code>(初学者)</code>。Java要求你必须按照语法规则编写代码。如果你的程序违反了语法规则,例如:<code>忘记了分号、大括号、引号</code>,或者拼错了单词java编译器都会报语法错误。尝试着去看懂编译器会报告的错误信息。</p></blockquote><blockquote><p>举例:</p></blockquote><ul><li><p>1->1</p></li><li><p>0->o</p></li><li><p>英文符号写中文符号</p></li><li><p>void ->viod 拼写错误</p></li><li><p>不好修改的错误其实是 业务错误,环境错误</p></li></ul><h1 id="注释"><a href="#注释" class="headerlink" title="注释"></a>注释</h1><h2 id="介绍:"><a href="#介绍:" class="headerlink" title="介绍:"></a>介绍:</h2><p>用于注解说明解释程序的文字就是注释,注释提高了代码的阅读性(可读性);注释是<code>一个程序员必须要具有的良好编程习惯</code>。将自己的思想通过注释先整理出来,再用代码去体现。</p><h2 id="Java中的注释类型"><a href="#Java中的注释类型" class="headerlink" title="Java中的注释类型"></a>Java中的注释类型</h2><ol><li><p>单行注释 //</p></li><li><p>多行注释 /* */</p></li><li><p>文档注释 /** */</p></li></ol><h3 id="单行注释:"><a href="#单行注释:" class="headerlink" title="单行注释:"></a>单行注释:</h3><p>基本格式</p><p>格式: //注释文字</p><h3 id="多行注释"><a href="#多行注释" class="headerlink" title="多行注释:"></a>多行注释:</h3><p>基本格式</p><p>格式: /* 注释文字 */</p><h3 id="使用细节"><a href="#使用细节" class="headerlink" title="使用细节"></a>使用细节</h3><ol><li><p>被注释的文字,不会被JVM(java 虚拟机)解释执行</p></li><li><p>多行注释里面不允许有多行注释嵌套</p></li></ol><h3 id="演示注释使用"><a href="#演示注释使用" class="headerlink" title="演示注释使用"></a>演示注释使用</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//演示注释使用</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Comment01</span> {</span><br><span class="line"></span><br><span class="line"><span class="comment">//编写一个main方法</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"></span><br><span class="line"><span class="comment">//单行注释</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//多行注释</span></span><br><span class="line"><span class="comment">/* 示意 => 可读性很好</span></span><br><span class="line"><span class="comment">下面代码完成 两个数相加</span></span><br><span class="line"><span class="comment">定义变量 </span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">注释</span></span><br><span class="line"><span class="comment">注释</span></span><br><span class="line"><span class="comment">int n1 = 10;... </span></span><br><span class="line"><span class="comment">System.out.println("ok~~");</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="type">int</span> <span class="variable">n1</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line"><span class="type">int</span> <span class="variable">n2</span> <span class="operator">=</span> <span class="number">30</span>;</span><br><span class="line"><span class="comment">//求和</span></span><br><span class="line"><span class="type">int</span> <span class="variable">sum</span> <span class="operator">=</span> n1 + n2;</span><br><span class="line"><span class="comment">//输出结果</span></span><br><span class="line">System.out.println(<span class="string">"结果="</span> + sum);</span><br><span class="line">}</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h3 id="文档注释"><a href="#文档注释" class="headerlink" title="文档注释"></a>文档注释</h3><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/64063/image-6.png" alt="【JavaSE基础】Java 基础知识"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/64063/image-7.png" alt="【JavaSE基础】Java 基础知识"></p><blockquote><p>快捷键:</p></blockquote><p>1.//选中,然后输入 tab 整体右移</p><p>2.//选中,然后输入 shift+tab 整体左移</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/64063/image-8.png" alt="【JavaSE基础】Java 基础知识"></p><h1 id="Java代码规范"><a href="#Java代码规范" class="headerlink" title="Java代码规范"></a>Java代码规范</h1><ul><li><p>类、方法的注释,要以javadoc的方式来写。</p></li><li><p><strong>非Java Doc的注释(包括单行注释和多行注释)<strong>,往往是</strong>给代码的维护者看</strong>的,着重告述读者为什么这样写,如何修改,注意什么问题等</p></li><li><p>使用<strong>tab操作</strong>,实现缩进,默认<strong>整体向右边移动</strong>,时候用<strong>shift+tab整体向左移</strong></p></li><li><p><strong>运算符</strong>和 <strong>=</strong> 两边习惯性<strong>各加一个空格</strong>。比如:<strong>2 + 4 * 5 + 345 - 89</strong></p></li><li><p><strong>源文件</strong>使用<strong>utf-8</strong>编码</p></li><li><p><strong>行宽度<code>不要超过80字符</code></strong></p></li><li><p>代码编写<strong>次行风格</strong>和**行尾风格(推荐)**。</p></li></ul><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/64063/image-9.png" alt="【JavaSE基础】Java 基础知识"></p><h1 id="DOS命令-了解"><a href="#DOS命令-了解" class="headerlink" title="DOS命令(了解)"></a>DOS命令<code>(了解)</code></h1><h2 id="DOS介绍"><a href="#DOS介绍" class="headerlink" title="DOS介绍"></a>DOS介绍</h2><blockquote><p>Dos: Disk Operating System 磁盘操作系统,简单说一下 windows 的目录结构。<code>[原理图如下]</code></p></blockquote><ul><li><p>md: make directory 创建目录</p></li><li><p>rd: remove directory 删除目录</p></li></ul><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/64063/image-10.png" alt="【JavaSE基础】Java 基础知识"></p><blockquote><p>相关的知识补充:相对路径,绝对路径</p></blockquote><ul><li><p>相对路径: 从<strong>当前目录开始定位</strong>,形成的一个路径。</p></li><li><p>绝对路径: 从<strong>顶级目录<code>d</code>开始定位</strong>,形成的一个路径。</p></li></ul><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/64063/image-11.png" alt="【JavaSE基础】Java 基础知识"></p><h2 id="常用的dos命令"><a href="#常用的dos命令" class="headerlink" title="常用的dos命令"></a>常用的dos命令</h2><ul><li><code>查看当前目录是有什么内容</code> dir</li></ul><blockquote><p>dir dir d:\abc2\test200</p></blockquote><ul><li><code>切换到其他盘下</code> : <code>盘符号 cd</code> : change directory</li></ul><blockquote><p>案例演示: 切换到c盘 cd /D c:</p></blockquote><ul><li>切换到当前盘的其他目录下(使用相对路径和绝对路径演示),<code>..\</code>表示<strong>上一级目录</strong></li></ul><blockquote><p>案例演示: cd d:\abc2\test200 cd ....\abc2\test200</p></blockquote><ul><li>切换到上一级:</li></ul><blockquote><p>案例演示: <code>cd ..</code></p></blockquote><ul><li>切换到根目录: cd \</li></ul><blockquote><p>案例演示: <code>cd \</code></p></blockquote><ul><li><p>查看指定的目录下所有的子级目录: <code>tree</code> </p></li><li><p>清屏 <code>cls</code></p></li><li><p>退出 <code>DOS (exit)</code></p></li></ul><blockquote><p>说明:因为小伙伴后面使用DOS 非常少,所以对下面的几个指令,<strong>了解即可</strong></p></blockquote><ul><li><p>md[创建目录]</p></li><li><p>rd[删除目录]</p></li><li><p>copy[拷贝文件]</p></li><li><p>del[删除文件]</p></li><li><p>echo[输入内容到文件]</p></li><li><p>type,move[剪切], =>Linux</p></li></ul>]]></content>
<categories>
<category> JavaSE </category>
</categories>
<tags>
<tag> JavaSE知识点总结 </tag>
</tags>
</entry>
<entry>
<title>【JavaSE基础】Java 快速入门</title>
<link href="/posts/9023/"/>
<url>/posts/9023/</url>
<content type="html"><![CDATA[<h1 id="快速入门"><a href="#快速入门" class="headerlink" title="快速入门"></a>快速入门</h1><h2 id="需求说明"><a href="#需求说明" class="headerlink" title="需求说明"></a>需求说明</h2><p>要求开发一个 Hello.java 程序,可以输出 “hello,world!”</p><h2 id="开发步骤"><a href="#开发步骤" class="headerlink" title="开发步骤"></a>开发步骤</h2><ol><li>将 Java 代码编写到扩展名为 <code>Hello.java</code> 的文件中。[代码说明]</li></ol><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/9023/image.png" alt="【JavaSE基础】Java 快速入门"></p><ol start="2"><li>通过 javac 命令对该 java 文件进行编译,生成 .class 文件。</li></ol><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/9023/image-1.png" alt="【JavaSE基础】Java 快速入门"></p><ol start="3"><li>通过 java 命令对生成的 class 文件进行运行。</li></ol><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/9023/image-2.png" alt="【JavaSE基础】Java 快速入门"></p><h2 id="运行原理示意图"><a href="#运行原理示意图" class="headerlink" title="运行原理示意图"></a>运行原理示意图</h2><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/9023/image-3.png" alt="【JavaSE基础】Java 快速入门"></p><h1 id="Java-开发快速入门"><a href="#Java-开发快速入门" class="headerlink" title="Java 开发快速入门"></a>Java 开发快速入门</h1><h2 id="Java执行流程分析"><a href="#Java执行流程分析" class="headerlink" title="Java执行流程分析"></a>Java执行流程分析</h2><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/9023/image-4.png" alt="【JavaSE基础】Java 快速入门"></p><h2 id="什么是编译"><a href="#什么是编译" class="headerlink" title="什么是编译"></a>什么是编译</h2><blockquote><p><code>javac Hello.java</code></p></blockquote><ol><li><p>有了java源文件,通过<code>编译器</code>将其编译成<code>JVM可以识别的字节码文件</code>(.class文件)。</p></li><li><p>在该源文件目录下,通过<code>javac</code>编译工具对<code>Hello.java</code>文件进行编译。</p></li><li><p><code>如果程序没有错误,没有任何提示</code>,但在当前目录下生成了<code>Hello.class</code>文件,该文件称为<code>字节码文件</code>,也是可以执行java的程序。</p></li></ol><h2 id="什么是运行"><a href="#什么是运行" class="headerlink" title="什么是运行"></a>什么是运行</h2><blockquote><p><code>java Hello</code></p></blockquote><ol><li><p>有了可执行的java程序(Hello.class字节码文件)</p></li><li><p>通过运行工具java.exe对字节码文件进行执行,本质就是 .class</p></li></ol><h2 id="Java-开发注意事项和细节说明"><a href="#Java-开发注意事项和细节说明" class="headerlink" title="Java 开发注意事项和细节说明"></a>Java 开发注意事项和细节说明</h2><p>对修改后的Hello.java源文件需要重新编译,生成新的class文件后,再进行执行,才能生效。</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/9023/image-5.png" alt="【JavaSE基础】Java 快速入门"></p><ul><li><p>想要运行修改后的Java文件,需要对源文件进行重新编译,生成新的字节码文件后,再执行,才能运行出修改后的。否则运行出的只能是修改之前的。</p></li><li><p>Java应用程序的执行入口是main()方法。</p></li><li><p>Java语言严格区分大小写。</p></li><li><p>每条语句以;结束。</p></li><li><p>{}成对出现,缺一不可。</p></li><li><p>一个源文件中只能有一个public修饰的类,其它类的个数不限。</p></li><li><p>每一个类编译后,都对应一个.class文件。</p></li><li><p>如果源文件包含一个public类,则文件名必须按该类命名。</p></li><li><p>可以将main()方法写在非public类中,然后指定运行该类,这样入口方法就是非public的main方法。</p></li></ul><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//一个源文件中最多只能有一个 public 类。其它类的个数不限。[演示] </span></span><br><span class="line"><span class="comment">//Dog 是一个类</span></span><br><span class="line"><span class="comment">//编译后,每一个类,都对于一个.class</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Dog</span> {</span><br><span class="line"><span class="comment">//一个源文件中最多只能有一个 public 类。其它类的个数不限,也可以将 main 方法写在非 public 类中,//然后指定运行非 public 类,这样入口方法就是非 public 的 main 方法</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> System.out.println(<span class="string">"hello, 小狗狗~"</span>);</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Tiger</span> {</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> { </span><br><span class="line"> System.out.println(<span class="string">"hello, 小老虎~"</span>);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h1 id="如何快速掌握技术或知识点"><a href="#如何快速掌握技术或知识点" class="headerlink" title="如何快速掌握技术或知识点"></a>如何快速掌握技术或知识点</h1><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/9023/image-6.png" alt="【JavaSE基础】Java 快速入门"></p>]]></content>
<categories>
<category> JavaSE </category>
</categories>
<tags>
<tag> JavaSE知识点总结 </tag>
</tags>
</entry>
<entry>
<title>【JavaSE基础】Java 语言概述</title>
<link href="/posts/18950/"/>
<url>/posts/18950/</url>
<content type="html"><![CDATA[<h1 id="目录:"><a href="#目录:" class="headerlink" title="目录:"></a>目录:</h1><ul><li><p>Java历史</p></li><li><p>Java重要特点</p></li><li><p>Java的开发工具</p></li><li><p>Java的工具选择</p></li><li><p>Java运行机制及运行过程</p></li><li><p>什么是 JDK,JRE</p></li></ul><h1 id="Java历史"><a href="#Java历史" class="headerlink" title="Java历史"></a>Java历史</h1><h2 id="什么是程序"><a href="#什么是程序" class="headerlink" title="什么是程序"></a>什么是程序</h2><p>程序:计算机执行某些操作或解决某个问题而**<code>编写的一系列有序指令的集合</code>**</p><blockquote><p><code>举例说明</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/18950/image.png" alt="【JavaSE基础】Java 语言概述"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/18950/image-1.png" alt="【JavaSE基础】Java 语言概述"></p><blockquote><p>编写一个简单的Java程序:</p></blockquote><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">test</span> {</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="type">int</span> <span class="variable">res</span> <span class="operator">=</span> <span class="number">1</span> + <span class="number">1</span>;</span><br><span class="line"> <span class="comment">//显示</span></span><br><span class="line"> System.out.println(<span class="string">"结果="</span> + res);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>运行该程序,输出结果为:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">结果=<span class="number">2</span></span><br></pre></td></tr></table></figure><h2 id="Java诞生的小故事"><a href="#Java诞生的小故事" class="headerlink" title="Java诞生的小故事"></a>Java诞生的小故事</h2><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/18950/image-2.png" alt="【JavaSE基础】Java 语言概述"></p><blockquote><p>Java背景知识</p></blockquote><ul><li><p>Java是美国 sun 公司(Stanford University Network)在1995年推出的一门计算机高级编程语言。</p></li><li><p>Java 早期称为Oak(橡树),后期改名为Java。</p></li><li><p>Java 之父:**詹姆斯·高斯林(James Gosling)**。</p></li><li><p>2009年 sun公司被Oracle公司收购。</p></li></ul><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/18950/image-3.png" alt="【JavaSE基础】Java 语言概述"></p><h2 id="Java技术体系平台"><a href="#Java技术体系平台" class="headerlink" title="Java技术体系平台"></a>Java技术体系平台</h2><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/18950/image-4.png" alt="【JavaSE基础】Java 语言概述"></p><h1 id="Java重要特点"><a href="#Java重要特点" class="headerlink" title="Java重要特点"></a>Java重要特点</h1><ul><li><p>Java 语言是面向对象的(oop)</p></li><li><p>Java 语言是健壮的。Java 的强类型机制、异常处理、垃圾的自动收集等是 Java 程序健壮性的重要保证</p></li><li><p>Java 语言是<code>跨平台性的</code>。<code>[即: 一个编译好的.class 文件可以在多个系统下运行,这种特性称为跨平台]</code></p></li></ul><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/18950/image-5.png" alt="【JavaSE基础】Java 语言概述"></p><ul><li>Java 语言是解释型的<code>[了解]</code></li></ul><p>解释性语言:<code>javascript</code>,<code>PHP</code>, <code>java</code> 编译性语言: <code>c / c++</code></p><p>区别是:<code>解释性语言</code>,编译后的代码,<code>不能直接被机器执行</code>,需要解释器来执行, <code>编译性语言</code>, 编译后的代码, <code>可以直接被机器执行</code>, <strong>c /c++</strong></p><h1 id="Java-的开发工具"><a href="#Java-的开发工具" class="headerlink" title="Java 的开发工具"></a>Java 的开发工具</h1><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/18950/image-6.png" alt="【JavaSE基础】Java 语言概述"></p><blockquote><p>前期使用Sublime Text + Java插件进行Java开发,后期转向IntelliJ IDEA + Java插件进行Java开发。</p></blockquote><p>Sublime Text 官方网址:<a href="https://www.sublimetext.com/">https://www.sublimetext.com/</a></p><p>IntelliJ IDEA 官方网址:<a href="https://www.jetbrains.com/idea/">https://www.jetbrains.com/idea/</a></p><ul><li><p>Sublime Text:<code>一个跨平台的文本编辑器</code>,支持Java开发,有丰富的插件,语法高亮,代码自动完成等功能。</p></li><li><p>editplus.com:<code>一个免费的文本编辑器</code>,支持Java开发,有丰富的插件,语法高亮,代码自动完成等功能。</p></li><li><p>NetBeans++:<code>一个商业Java开发环境</code>,支持Java开发,有丰富的插件,语法高亮,代码自动完成等功能。</p></li><li><p>Eclipse:<code>一个开源的Java开发环境</code>,支持Java开发,有丰富的插件,语法高亮,代码自动完成等功能。</p></li><li><p>IntelliJ IDEA:<code>一个商业Java开发环境</code>,支持Java开发,有丰富的插件,语法高亮,代码自动完成等功能。</p></li></ul><h1 id="Java-工具选择"><a href="#Java-工具选择" class="headerlink" title="Java 工具选择"></a>Java 工具选择</h1><blockquote><p>如何选择开发工具</p></blockquote><ul><li>我们先选择用文本编辑器本 sublime,到大家对 java 有一定了解后,我们再使用 IDEA 和 Eclipse 开发工具。</li></ul><blockquote><p>这是为什么呢</p></blockquote><ul><li><p>更深刻的理解 java 技术,培养代码感。【面试时,往往程序员手写代码】</p></li><li><p>有利于公司面试。</p></li></ul><h1 id="Java运行机制及运行过程"><a href="#Java运行机制及运行过程" class="headerlink" title="Java运行机制及运行过程"></a>Java运行机制及运行过程</h1><h2 id="Java-语言的特点:跨平台性"><a href="#Java-语言的特点:跨平台性" class="headerlink" title="Java 语言的特点:跨平台性"></a>Java 语言的特点:跨平台性</h2><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/18950/image-7.png" alt="【JavaSE基础】Java 语言概述"></p><blockquote><p><code>Java 核心机制-Java 虚拟机 [JVM java virtual machine]</code></p></blockquote><p>基本介绍</p><ol><li><p><code>JVM</code> 是一个<code>虚拟的计算机</code>,具有<code>指令集并使用不同的存储区域</code>。负责执行指令,管理数据、内存、寄存器,包含在JDK 中.</p></li><li><p>对于<code>不同的平台</code>,有<code>不同的虚拟机</code>。</p></li><li><p>Java 虚拟机机制屏蔽了底层运行平台的差别,实现了“<code>一次编译,到处运行</code>” [说明]</p></li></ol><blockquote><p><code>Java 程序运行过程示意图</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/18950/image-8.png" alt="【JavaSE基础】Java 语言概述"></p><h1 id="什么是-JDK,JRE"><a href="#什么是-JDK,JRE" class="headerlink" title="什么是 JDK,JRE"></a>什么是 JDK,JRE</h1><h2 id="JDK-基本介绍"><a href="#JDK-基本介绍" class="headerlink" title="JDK 基本介绍"></a>JDK 基本介绍</h2><ol><li>JDK 的全称(Java Development Kit <code>Java 开发工具包</code>)</li></ol><blockquote><p>JDK = JRE + java 的开发工具 [java, javac,javadoc,javap 等]</p></blockquote><ol start="2"><li>JDK 是提供给 Java 开发人员使用的,其中包含了 java 的开发工具,也包括了 JRE。所以安装了 JDK,就不用在单独安装 JRE 了。</li></ol><h2 id="JRE-基本介绍"><a href="#JRE-基本介绍" class="headerlink" title="JRE 基本介绍"></a>JRE 基本介绍</h2><ol><li>JRE(Java Runtime Environment <code>Java 运行环境</code>)</li></ol><blockquote><p>JRE = JVM + Java 的核心类库[类]</p></blockquote><ol start="2"><li>包括 Java 虚拟机(JVM Java Virtual Machine)和 Java 程序所需的核心类库等,如果想要运行一个开发好的 Java 程序,计算机中只需要安装 JRE 即可。</li></ol><h2 id="JDK、JRE-和-JVM-的包含关系"><a href="#JDK、JRE-和-JVM-的包含关系" class="headerlink" title="JDK、JRE 和 JVM 的包含关系"></a>JDK、JRE 和 JVM 的包含关系</h2><ol><li><p><code>JDK = JRE + 开发工具集</code>(例如 Javac,java 编译工具等)</p></li><li><p><code>JRE = JVM + Java SE 标准类库</code>(java 核心类库)</p></li><li><p>如果只想运行开发好的 <code>.class</code> 文件 <code>只需要 JRE</code></p></li></ol>]]></content>
<categories>
<category> JavaSE </category>
</categories>
<tags>
<tag> JavaSE知识点总结 </tag>
</tags>
</entry>
<entry>
<title>Windows下Java环境配置教程</title>
<link href="/posts/52027/"/>
<url>/posts/52027/</url>
<content type="html"><![CDATA[<h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>这篇博客介绍Java环境的配置,主要是下载JDK,安装JDK,以及<code>path</code>、<code>JAVA_hOME</code>、<code>CLASSPAT</code>的配置,还会介绍配置这些的原因。</p><h1 id="JDK的安装"><a href="#JDK的安装" class="headerlink" title="JDK的安装"></a>JDK的安装</h1><h2 id="下载JDK"><a href="#下载JDK" class="headerlink" title="下载JDK"></a>下载JDK</h2><p>首先,你需要到<a href="https://www.oracle.com/cn/java/technologies/downloads/">Oracle官网</a>下载Java Development Kit (JDK)安装包。</p><h3 id="下载JDK的步骤如下"><a href="#下载JDK的步骤如下" class="headerlink" title="下载JDK的步骤如下"></a>下载JDK的步骤如下</h3><p><code>第1步</code>: 进入网站 ——> <img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/52027/image.png" alt="Windows下Java环境配置教程"></p><p><code>第2步</code>: 选择你需要的JDK版本,比如我这里选择的是<code>JDK 8u411(简称JDK 8)</code>,<code>JDK 17</code>。下面分别演示两个版本的下载过程。</p><blockquote><p><code>JDK 17</code></p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/52027/image-1.png" alt="Windows下Java环境配置教程"></p><blockquote><p><code>JDK 8u411</code></p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/52027/image-2.png" alt="Windows下Java环境配置教程"></p><blockquote><p><code>注意</code>:官网下载<code>JDK 8u411</code>,可能会提示你需要注册登录Oracle账号,这里注册Oracle账号比较麻烦,接下来我给大家提供一个网址,里面有免注册的共用Oracle账号。</p></blockquote><p><a href="https://bugmenot.com/view/oracle.com">Oracle免注册账号</a></p><h2 id="安装JDK"><a href="#安装JDK" class="headerlink" title="安装JDK"></a>安装JDK</h2><blockquote><p>安装JDK,点击下载好的安装包,运行<code>setup.exe</code>文件,按照提示一步步安装即可。</p></blockquote><p><code>建议:安装路径不要有中文或者空格等特殊符号,否则可能会导致配置环境变量失败。</code></p><p>安装步骤(<code>以JDK 17为例,安装JDK 8u411的步骤类似</code>):</p><p>(1)双击jdk-17-windows-x64.exe文件,并单击下一步</p><p>(2)修改安装路径,自定义</p><p>(3)将安装路径修改为D:\0-ProgrammingSoftware\Java\jdk-17并单击下一步 ——> <code>这里根据自己的实际情况修改安装路径</code></p><p>(4)稍后几秒,安装完成</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/52027/image-3.png" alt="Windows下Java环境配置教程"></p><h1 id="配置环境变量-———-JAVA-HOME、path、CLASSPAT"><a href="#配置环境变量-———-JAVA-HOME、path、CLASSPAT" class="headerlink" title="配置环境变量 ———> JAVA_HOME、path、CLASSPAT"></a>配置环境变量 ———> JAVA_HOME、path、CLASSPAT</h1><p>安装完成后,你需要配置环境变量,让系统能够识别到JDK。</p><h2 id="打开环境变量编辑器"><a href="#打开环境变量编辑器" class="headerlink" title="打开环境变量编辑器"></a>打开环境变量编辑器</h2><p>你可以在开始菜单中搜索<code>环境变量</code>或者在<code>控制面板</code>中搜索<code>系统</code>,然后点击<code>高级系统设置</code>-><code>环境变量</code>。</p><h2 id="配置JAVA-HOME环境变量"><a href="#配置JAVA-HOME环境变量" class="headerlink" title="配置JAVA_HOME环境变量"></a>配置<code>JAVA_HOME</code>环境变量</h2><h3 id="配置原因"><a href="#配置原因" class="headerlink" title="配置原因"></a>配置原因</h3><p><code>JAVA_HOME</code>环境变量是JDK的安装目录,它告诉系统到哪里去找Java的运行时环境。</p><h3 id="配置方法"><a href="#配置方法" class="headerlink" title="配置方法"></a>配置方法</h3><p>首先找到我的电脑图标(就是一个电脑似的图标),然后点击鼠标右键,再点击属性,找到高级系统设置,点击环境变量。弹出下图所示的窗口。</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/52027/image-8.png" alt="Windows下Java环境配置教程"></p><p>在系统变量里新建一个<code>JAVA_HOME</code>,并赋值为JDK的安装目录,如下图所示: </p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/52027/image-9.png" alt="Windows下Java环境配置教程"></p><blockquote><p>注意:这里的路径要根据自己的实际情况修改,比如我这里安装的JDK 17的安装路径是<code>D:\0-ProgrammingSoftware\Java\jdk-17</code>,所以<code>JAVA_HOME</code>的值就是<code>D:\0-ProgrammingSoftware\Java\jdk-17</code>。</p></blockquote><h2 id="配置Path环境变量"><a href="#配置Path环境变量" class="headerlink" title="配置Path环境变量"></a>配置<code>Path</code>环境变量</h2><h3 id="配置原因-1"><a href="#配置原因-1" class="headerlink" title="配置原因"></a>配置原因</h3><p>因为JDK装好后的两个重要执行文件<code>javac.exe(代码编译文件)</code>和<code>java.exe(代码执行文件)</code>存在于所装的JDK的bin目录下,<code>而我们写的Java或者跑的别人的程序一般是放在一个新的目录</code>,主要是方便管理,如果都放在这里,会很混乱,更何况编译还有class文件生成。</p><p>但是,当我们要运行Java程序时,我们需要<code>编译(需要使用javac.exe)</code>和<code>执行(需要使用java.exe)</code>,而<code>系统默认</code>的是在<code>当前程序</code>所在的<code>文件夹目录下去寻找这两个执行文件</code>,而不会去其他地方找,此时我们就需要配置Path环境变量,使得每次程序运行就<code>自动到安装JDK的bin目录</code>下去搜索<code>javac.exe</code>和<code>java.exe</code>文件。</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/52027/image-4.png" alt="Windows下Java环境配置教程"></p><p>点击新建,找到 JDK 的安装目录中javac.exe和java.exe所在的目录,把目录复制到这里,点击确认即可,Path路径就设置完毕了。</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/52027/image-6.png" alt="Windows下Java环境配置教程"></p><h3 id="配置方法-1"><a href="#配置方法-1" class="headerlink" title="配置方法"></a>配置方法</h3><p>在下面的系统变量框中(Path变量是系统本身就有的),选中Path那一行点击编辑,出现如下界面:</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/52027/image-5.png" alt="Windows下Java环境配置教程"></p><p>由于JAVA_HOME配置的是JDK的安装目录,所以Path有另一种配置方式,<code>配置jdk和jre的bin目录</code>即可,如下所示:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">%JAVA_HOME%\bin</span><br><span class="line">%JAVA_HOME%\jre\bin</span><br><span class="line"></span><br><span class="line"><span class="number">1.</span>其中% %表示引用JAVA_HOME的值</span><br><span class="line"><span class="number">2.</span>这样的好处在于,如果Java安装目录发生变话,</span><br><span class="line">需要修改JAVA_HOME的值,不用再修改Path变量的值</span><br><span class="line"><span class="number">3.</span>部分Windows10的系统在重启之后%JAVA_HOME%\bin配置的失效,</span><br><span class="line">这里解决方法是按上面的那种方法配置全路径即可</span><br></pre></td></tr></table></figure><blockquote><p>注意:Windows 8 , 10的Path变量配置要友好一些,直接配置一条记录即可,如果是Windows7的话,需要在Path变量的值中使用 ;(英文分号)分割,然后追加Java的安装目录下的bin目录路径即可。</p></blockquote><h2 id="配置CLASSPATH环境变量"><a href="#配置CLASSPATH环境变量" class="headerlink" title="配置CLASSPATH环境变量"></a>配置<code>CLASSPATH</code>环境变量</h2><h3 id="配置原因-2"><a href="#配置原因-2" class="headerlink" title="配置原因"></a>配置原因</h3><p>CLASSPATH顾名思义为包路径,告诉Java在执行的时候,去哪里找到需要的包和类供程序使用。所以配置时应把包的路径赋值给CLASSPATH。</p><h3 id="配置方法-2"><a href="#配置方法-2" class="headerlink" title="配置方法"></a>配置方法</h3><p>我们在系统变量里新建一个CLASSPATH,赋值如下:</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar</span><br></pre></td></tr></table></figure><p>注意前面的 . 这个点号不能省略掉</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/52027/image-7.png" alt="Windows下Java环境配置教程"></p><p>此时赋值的路径有三个,以分号为间隔隔开 ,第一个英文的句号表示当前目录路径,第二个目录为lib库中的tools工具包路径,第三个为lib库中的dt包路径。</p><h1 id="验证JDK安装"><a href="#验证JDK安装" class="headerlink" title="验证JDK安装"></a>验证JDK安装</h1><p>安装完成JDK以及相应的配置之后,需要验证是否安装成功,是否配置正常。</p><ul><li>第一步:打开cmd命令行窗口,键盘win+r;输入 cmd 打开命令行</li></ul><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/52027/image-10.png" alt="Windows下Java环境配置教程"></p><ul><li>第二步:验证JDK安装是否成功,你可以在命令行中输入<code>java -version</code>命令,如果出现以下输出,说明JDK安装成功:</li></ul><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/52027/image-11.png" alt="Windows下Java环境配置教程"></p><ul><li>第三步:输入java,回车可以出现如下的提示:</li></ul><figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">C:\<span class="title">Users</span>\<span class="title">LenovoHZB</span>><span class="title">java</span></span></span><br><span class="line"><span class="function">用法:<span class="title">java</span> [<span class="title">options</span>] <主类> [<span class="title">args</span>...]</span></span><br><span class="line"><span class="function"> (执行类)</span></span><br><span class="line"><span class="function"> 或 <span class="title">java</span> [<span class="title">options</span>] -<span class="title">jar</span> <<span class="title">jar</span> 文件> [<span class="title">args</span>...]</span></span><br><span class="line"><span class="function"> (执行 <span class="title">jar</span> 文件)</span></span><br><span class="line"><span class="function"> 或 <span class="title">java</span> [<span class="title">options</span>] -<span class="title">m</span> <模块>[/<主类>] [<span class="title">args</span>...]</span></span><br><span class="line"><span class="function"> <span class="title">java</span> [<span class="title">options</span>] --<span class="title">module</span> <模块>[/<主类>] [<span class="title">args</span>...]</span></span><br><span class="line"><span class="function"> (执行模块中的主类)</span></span><br><span class="line"><span class="function"> 或 <span class="title">java</span> [<span class="title">options</span>] <源文件> [<span class="title">args</span>]</span></span><br><span class="line"><span class="function"> (执行单个源文件程序)</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"> 将主类、源文件、-<span class="title">jar</span> <<span class="title">jar</span> 文件>、-<span class="title">m</span> 或</span></span><br><span class="line"><span class="function"> --<span class="title">module</span> <模块>/<主类> 后的参数作为参数</span></span><br><span class="line"><span class="function"> 传递到主类。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"> 其中,选项包括:</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"> -<span class="title">cp</span> <目录和 <span class="title">zip</span>/<span class="title">jar</span> 文件的类搜索路径></span></span><br><span class="line"><span class="function"> -<span class="title">classpath</span> <目录和 <span class="title">zip</span>/<span class="title">jar</span> 文件的类搜索路径></span></span><br><span class="line"><span class="function"> --<span class="title">class</span>-<span class="title">path</span> <目录和 <span class="title">zip</span>/<span class="title">jar</span> 文件的类搜索路径></span></span><br><span class="line"><span class="function"> 使用 ; 分隔的, 用于搜索类文件的目录, <span class="title">JAR</span> 档案</span></span><br><span class="line"><span class="function"> 和 <span class="title">ZIP</span> 档案列表。</span></span><br><span class="line"><span class="function"> -<span class="title">p</span> <模块路径></span></span><br><span class="line"><span class="function"> --<span class="title">module</span>-<span class="title">path</span> <模块路径>...</span></span><br><span class="line"><span class="function"> 用 ; 分隔的目录列表, 每个目录</span></span><br><span class="line"><span class="function"> 都是一个包含模块的目录。</span></span><br><span class="line"><span class="function"> --<span class="title">upgrade</span>-<span class="title">module</span>-<span class="title">path</span> <模块路径>...</span></span><br><span class="line"><span class="function"> 用 ; 分隔的目录列表, 每个目录</span></span><br><span class="line"><span class="function"> 都是一个包含模块的目录, 这些模块</span></span><br><span class="line"><span class="function"> 用于替换运行时映像中的可升级模块</span></span><br><span class="line"><span class="function"> --<span class="title">add</span>-<span class="title">modules</span> <模块名称>[,<模块名称>...]</span></span><br><span class="line"><span class="function"> 除了初始模块之外要解析的根模块。</span></span><br><span class="line"><span class="function"> <模块名称> 还可以为 <span class="title">ALL</span>-<span class="title">DEFAULT</span>, <span class="title">ALL</span>-<span class="title">SYSTEM</span>,</span></span><br><span class="line"><span class="function"> <span class="title">ALL</span>-<span class="title">MODULE</span>-<span class="title">PATH</span>.</span></span><br><span class="line"><span class="function"> --<span class="title">list</span>-<span class="title">modules</span></span></span><br><span class="line"><span class="function"> 列出可观察模块并退出</span></span><br><span class="line"><span class="function"> -<span class="title">d</span> <<span class="title">module</span> <span class="title">name</span>></span></span><br><span class="line"><span class="function"> --<span class="title">describe</span>-<span class="title">module</span> <模块名称></span></span><br><span class="line"><span class="function"> 描述模块并退出</span></span><br><span class="line"><span class="function"> --<span class="title">dry</span>-<span class="title">run</span> 创建 <span class="title">VM</span> 并加载主类, 但不执行 <span class="title">main</span> 方法。</span></span><br><span class="line"><span class="function"> 此 --<span class="title">dry</span>-<span class="title">run</span> 选项对于验证诸如</span></span><br><span class="line"><span class="function"> 模块系统配置这样的命令行选项可能非常有用。</span></span><br><span class="line"><span class="function"> --<span class="title">validate</span>-<span class="title">modules</span></span></span><br><span class="line"><span class="function"> 验证所有模块并退出</span></span><br><span class="line"><span class="function"> --<span class="title">validate</span>-<span class="title">modules</span> 选项对于查找</span></span><br><span class="line"><span class="function"> 模块路径中模块的冲突及其他错误可能非常有用。</span></span><br><span class="line"><span class="function"> -<span class="title">D</span><名称>=<值></span></span><br><span class="line"><span class="function"> 设置系统属性</span></span><br><span class="line"><span class="function"> -<span class="title">verbose</span>:[<span class="title">class</span>|<span class="title">module</span>|<span class="title">gc</span>|<span class="title">jni</span>]</span></span><br><span class="line"><span class="function"> 为给定子系统启用详细输出</span></span><br><span class="line"><span class="function"> -<span class="title">version</span> 将产品版本输出到错误流并退出</span></span><br><span class="line"><span class="function"> --<span class="title">version</span> 将产品版本输出到输出流并退出</span></span><br><span class="line"><span class="function"> -<span class="title">showversion</span> 将产品版本输出到错误流并继续</span></span><br><span class="line"><span class="function"> --<span class="title">show</span>-<span class="title">version</span></span></span><br><span class="line"><span class="function"> 将产品版本输出到输出流并继续</span></span><br><span class="line"><span class="function"> --<span class="title">show</span>-<span class="title">module</span>-<span class="title">resolution</span></span></span><br><span class="line"><span class="function"> 在启动过程中显示模块解析输出</span></span><br><span class="line"><span class="function"> -? -<span class="title">h</span> -<span class="title">help</span></span></span><br><span class="line"><span class="function"> 将此帮助消息输出到错误流</span></span><br><span class="line"><span class="function"> --<span class="title">help</span> 将此帮助消息输出到输出流</span></span><br><span class="line"><span class="function"> -<span class="title">X</span> 将额外选项的帮助输出到错误流</span></span><br><span class="line"><span class="function"> --<span class="title">help</span>-<span class="title">extra</span> 将额外选项的帮助输出到输出流</span></span><br><span class="line"><span class="function"> -<span class="title">ea</span>[:<程序包名称>...|:<类名>]</span></span><br><span class="line"><span class="function"> -<span class="title">enableassertions</span>[:<程序包名称>...|:<类名>]</span></span><br><span class="line"><span class="function"> 按指定的粒度启用断言</span></span><br><span class="line"><span class="function"> -<span class="title">da</span>[:<程序包名称>...|:<类名>]</span></span><br><span class="line"><span class="function"> -<span class="title">disableassertions</span>[:<程序包名称>...|:<类名>]</span></span><br><span class="line"><span class="function"> 按指定的粒度禁用断言</span></span><br><span class="line"><span class="function"> -<span class="title">esa</span> | -<span class="title">enablesystemassertions</span></span></span><br><span class="line"><span class="function"> 启用系统断言</span></span><br><span class="line"><span class="function"> -<span class="title">dsa</span> | -<span class="title">disablesystemassertions</span></span></span><br><span class="line"><span class="function"> 禁用系统断言</span></span><br><span class="line"><span class="function"> -<span class="title">agentlib</span>:<库名>[=<选项>]</span></span><br><span class="line"><span class="function"> 加载本机代理库 <库名>, 例如 -<span class="title">agentlib:jdwp</span></span></span><br><span class="line"><span class="function"> 另请参阅 -<span class="title">agentlib:jdwp</span>=<span class="title">help</span></span></span><br><span class="line"><span class="function"> -<span class="title">agentpath</span>:<路径名>[=<选项>]</span></span><br><span class="line"><span class="function"> 按完整路径名加载本机代理库</span></span><br><span class="line"><span class="function"> -<span class="title">javaagent</span>:<<span class="title">jar</span> 路径>[=<选项>]</span></span><br><span class="line"><span class="function"> 加载 <span class="title">Java</span> 编程语言代理, 请参阅 <span class="title">java.lang.instrument</span></span></span><br><span class="line"><span class="function"> -<span class="title">splash</span>:<图像路径></span></span><br><span class="line"><span class="function"> 使用指定的图像显示启动屏幕</span></span><br><span class="line"><span class="function"> 自动支持和使用 <span class="title">HiDPI</span> 缩放图像</span></span><br><span class="line"><span class="function"> (如果可用)。应始终将未缩放的图像文件名 (例如, <span class="title">image.ext</span>)</span></span><br><span class="line"><span class="function"> 作为参数传递给 -<span class="title">splash</span> 选项。</span></span><br><span class="line"><span class="function"> 将自动选取提供的最合适的缩放</span></span><br><span class="line"><span class="function"> 图像。</span></span><br><span class="line"><span class="function"> 有关详细信息, 请参阅 <span class="title">SplashScreen</span> <span class="title">API</span> 文档</span></span><br><span class="line"><span class="function"> @<span class="title">argument</span> 文件</span></span><br><span class="line"><span class="function"> 一个或多个包含选项的参数文件</span></span><br><span class="line"><span class="function"> -<span class="title">disable</span>-@<span class="title">files</span></span></span><br><span class="line"><span class="function"> 阻止进一步扩展参数文件</span></span><br><span class="line"><span class="function"> --<span class="title">enable</span>-<span class="title">preview</span></span></span><br><span class="line"><span class="function"> 允许类依赖于此发行版的预览功能</span></span><br><span class="line"><span class="function">要为长选项指定参数, 可以使用 --<名称>=<值> 或</span></span><br><span class="line"><span class="function">--<名称> <值>。</span></span><br></pre></td></tr></table></figure><ul><li>第四步:输入javac,(javac是用来编译.java文件的):</li></ul><figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">C:\<span class="title">Users</span>\<span class="title">LenovoHZB</span>><span class="title">javac</span></span></span><br><span class="line"><span class="function">用法: <span class="title">javac</span> <<span class="title">options</span>> <<span class="title">source</span> <span class="title">files</span>></span></span><br><span class="line"><span class="function">其中, 可能的选项包括:</span></span><br><span class="line"><span class="function"> @<<span class="title">filename</span>> 从文件读取选项和文件名</span></span><br><span class="line"><span class="function"> -<span class="title">Akey</span>[=<span class="title">value</span>] 传递给注释处理程序的选项</span></span><br><span class="line"><span class="function"> --<span class="title">add</span>-<span class="title">modules</span> <模块>(,<模块>)*</span></span><br><span class="line"><span class="function"> 除了初始模块之外要解析的根模块; 如果 <<span class="title">module</span>></span></span><br><span class="line"><span class="function"> 为 <span class="title">ALL</span>-<span class="title">MODULE</span>-<span class="title">PATH</span>, 则为模块路径中的所有模块。</span></span><br><span class="line"><span class="function"> --<span class="title">boot</span>-<span class="title">class</span>-<span class="title">path</span> <<span class="title">path</span>>, -<span class="title">bootclasspath</span> <<span class="title">path</span>></span></span><br><span class="line"><span class="function"> 覆盖引导类文件的位置</span></span><br><span class="line"><span class="function"> --<span class="title">class</span>-<span class="title">path</span> <<span class="title">path</span>>, -<span class="title">classpath</span> <<span class="title">path</span>>, -<span class="title">cp</span> <<span class="title">path</span>></span></span><br><span class="line"><span class="function"> 指定查找用户类文件和注释处理程序的位置</span></span><br><span class="line"><span class="function"> -<span class="title">d</span> <<span class="title">directory</span>> 指定放置生成的类文件的位置</span></span><br><span class="line"><span class="function"> -<span class="title">deprecation</span> 输出使用已过时的 <span class="title">API</span> 的源位置</span></span><br><span class="line"><span class="function"> --<span class="title">enable</span>-<span class="title">preview</span> 启用预览语言功能。要与 -<span class="title">source</span> 或 --<span class="title">release</span> 一起使用。</span></span><br><span class="line"><span class="function"> -<span class="title">encoding</span> <<span class="title">encoding</span>> 指定源文件使用的字符编码</span></span><br><span class="line"><span class="function"> -<span class="title">endorseddirs</span> <<span class="title">dirs</span>> 覆盖签名的标准路径的位置</span></span><br><span class="line"><span class="function"> -<span class="title">extdirs</span> <<span class="title">dirs</span>> 覆盖所安装扩展的位置</span></span><br><span class="line"><span class="function"> -<span class="title">g</span> 生成所有调试信息</span></span><br><span class="line"><span class="function"> -<span class="title">g</span>:{<span class="title">lines</span>,<span class="title">vars</span>,<span class="title">source</span>} 只生成某些调试信息</span></span><br><span class="line"><span class="function"> -<span class="title">g:none</span> 不生成任何调试信息</span></span><br><span class="line"><span class="function"> -<span class="title">h</span> <<span class="title">directory</span>> 指定放置生成的本机标头文件的位置</span></span><br><span class="line"><span class="function"> --<span class="title">help</span>, -<span class="title">help</span>, -? 输出此帮助消息</span></span><br><span class="line"><span class="function"> --<span class="title">help</span>-<span class="title">extra</span>, -<span class="title">X</span> 输出额外选项的帮助</span></span><br><span class="line"><span class="function"> -<span class="title">implicit</span>:{<span class="title">none</span>,<span class="title">class</span>} 指定是否为隐式引用文件生成类文件</span></span><br><span class="line"><span class="function"> -<span class="title">J</span><<span class="title">flag</span>> 直接将 <标记> 传递给运行时系统</span></span><br><span class="line"><span class="function"> --<span class="title">limit</span>-<span class="title">modules</span> <模块>(,<模块>)*</span></span><br><span class="line"><span class="function"> 限制可观察模块的领域</span></span><br><span class="line"><span class="function"> --<span class="title">module</span> <模块>(,<模块>)*, -<span class="title">m</span> <模块>(,<模块>)*</span></span><br><span class="line"><span class="function"> 只编译指定的模块,请检查时间戳</span></span><br><span class="line"><span class="function"> --<span class="title">module</span>-<span class="title">path</span> <<span class="title">path</span>>, -<span class="title">p</span> <<span class="title">path</span>></span></span><br><span class="line"><span class="function"> 指定查找应用程序模块的位置</span></span><br><span class="line"><span class="function"> --<span class="title">module</span>-<span class="title">source</span>-<span class="title">path</span> <<span class="title">module</span>-<span class="title">source</span>-<span class="title">path</span>></span></span><br><span class="line"><span class="function"> 指定查找多个模块的输入源文件的位置</span></span><br><span class="line"><span class="function"> --<span class="title">module</span>-<span class="title">version</span> <版本> 指定正在编译的模块版本</span></span><br><span class="line"><span class="function"> -<span class="title">nowarn</span> 不生成任何警告</span></span><br><span class="line"><span class="function"> -<span class="title">parameters</span> 生成元数据以用于方法参数的反射</span></span><br><span class="line"><span class="function"> -<span class="title">proc</span>:{<span class="title">none</span>,<span class="title">only</span>,<span class="title">full</span>} 控制是否执行注释处理和/或编译。</span></span><br><span class="line"><span class="function"> -<span class="title">processor</span> <<span class="title">class1</span>>[,<<span class="title">class2</span>>,<<span class="title">class3</span>>...]</span></span><br><span class="line"><span class="function"> 要运行的注释处理程序的名称; 绕过默认的搜索进程</span></span><br><span class="line"><span class="function"> --<span class="title">processor</span>-<span class="title">module</span>-<span class="title">path</span> <<span class="title">path</span>></span></span><br><span class="line"><span class="function"> 指定查找注释处理程序的模块路径</span></span><br><span class="line"><span class="function"> --<span class="title">processor</span>-<span class="title">path</span> <<span class="title">path</span>>, -<span class="title">processorpath</span> <<span class="title">path</span>></span></span><br><span class="line"><span class="function"> 指定查找注释处理程序的位置</span></span><br><span class="line"><span class="function"> -<span class="title">profile</span> <<span class="title">profile</span>> 请确保使用的 <span class="title">API</span> 在指定的配置文件中可用</span></span><br><span class="line"><span class="function"> --<span class="title">release</span> <<span class="title">release</span>></span></span><br><span class="line"><span class="function"> 为指定的 <span class="title">Java</span> <span class="title">SE</span> 发行版编译。支持的发行版:7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17</span></span><br><span class="line"><span class="function"> -<span class="title">s</span> <<span class="title">directory</span>> 指定放置生成的源文件的位置</span></span><br><span class="line"><span class="function"> --<span class="title">source</span> <<span class="title">release</span>>, -<span class="title">source</span> <<span class="title">release</span>></span></span><br><span class="line"><span class="function"> 提供与指定的 <span class="title">Java</span> <span class="title">SE</span> 发行版的源兼容性。支持的发行版:7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17</span></span><br><span class="line"><span class="function"> --<span class="title">source</span>-<span class="title">path</span> <<span class="title">path</span>>, -<span class="title">sourcepath</span> <<span class="title">path</span>></span></span><br><span class="line"><span class="function"> 指定查找输入源文件的位置</span></span><br><span class="line"><span class="function"> --<span class="title">system</span> <<span class="title">jdk</span>>|<span class="title">none</span> 覆盖系统模块位置</span></span><br><span class="line"><span class="function"> --<span class="title">target</span> <<span class="title">release</span>>, -<span class="title">target</span> <<span class="title">release</span>></span></span><br><span class="line"><span class="function"> 生成适合指定的 <span class="title">Java</span> <span class="title">SE</span> 发行版的类文件。支持的发行版:7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17</span></span><br><span class="line"><span class="function"> --<span class="title">upgrade</span>-<span class="title">module</span>-<span class="title">path</span> <<span class="title">path</span>></span></span><br><span class="line"><span class="function"> 覆盖可升级模块位置</span></span><br><span class="line"><span class="function"> -<span class="title">verbose</span> 输出有关编译器正在执行的操作的消息</span></span><br><span class="line"><span class="function"> --<span class="title">version</span>, -<span class="title">version</span> 版本信息</span></span><br><span class="line"><span class="function"> -<span class="title">Werror</span> 出现警告时终止编译</span></span><br></pre></td></tr></table></figure><h1 id="结语"><a href="#结语" class="headerlink" title="结语"></a>结语</h1><p>以上就是Windows下Java环境的配置教程,希望对你有所帮助。感谢每一位走到这里的小伙伴,我们可以一起学习交流,一起进步😉!!!加油🏃!!!</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/52027/image-12.png" alt="Windows下Java环境配置教程"></p>]]></content>
<categories>
<category> JavaSE </category>
</categories>
<tags>
<tag> JavaSE知识点总结 </tag>
</tags>
</entry>
<entry>
<title>打开PR世界的大门</title>
<link href="/posts/59157/"/>
<url>/posts/59157/</url>
<content type="html"><![CDATA[<h1 id="关于PR"><a href="#关于PR" class="headerlink" title="关于PR"></a>关于PR</h1><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/59157/image.png" alt="alt text"></p><h2 id="Pr项目工程文件的后缀"><a href="#Pr项目工程文件的后缀" class="headerlink" title="Pr项目工程文件的后缀"></a>Pr项目工程文件的后缀</h2><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/59157/image-1.png" alt="alt text"></p><blockquote><p><code>注意</code>:</p></blockquote><p>工程文件兼容性:</p><p>(1) PR工程文件后缀为<code>.prproj</code>,请勿修改。</p><p>(2) 工程文件中,<code>向下兼容</code> <code>不向上兼容</code>。即,<code>新版本的PR软件可以打开旧版本的工程文件</code>,<code>但旧版本的PR软件不能打开新版本的工程文件</code>。</p><h1 id><a href="#" class="headerlink" title></a></h1><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/59157/image-2.png" alt="alt text"></p>]]></content>
<categories>
<category> PR剪辑学习 </category>
</categories>
<tags>
<tag> PR剪辑 </tag>
</tags>
</entry>
<entry>
<title>多元函数微分学</title>
<link href="/posts/22439/"/>
<url>/posts/22439/</url>
<content type="html"><![CDATA[<h1 id="二元函数极限与连续性"><a href="#二元函数极限与连续性" class="headerlink" title="二元函数极限与连续性"></a>二元函数极限与连续性</h1><blockquote><p><code>等价无穷小点的替换</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image.png" alt="alt text"></p><blockquote><p><code>例题-1</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-1.png" alt="alt text"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-2.png" alt="alt text"></p><blockquote><p><code>例题-2</code>:<strong><code>(了解)</code></strong></p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-3.png" alt="alt text"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-4.png" alt="alt text"></p><h1 id="如何求-二重极限"><a href="#如何求-二重极限" class="headerlink" title="如何求**二重极限**"></a>如何求**<code>二重极限</code>**</h1><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-5.png" alt="alt text"></p><h2 id="求-偏导数-和二阶偏导数"><a href="#求-偏导数-和二阶偏导数" class="headerlink" title="求**偏导数**和二阶偏导数"></a>求**<code>偏导数</code>**和二阶偏导数</h2><blockquote><p><strong><code>求导公式</code></strong>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-6.png" alt="alt text"></p><blockquote><p><strong><code>一阶偏导数例题</code></strong>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-7.png" alt="alt text"></p><blockquote><p><strong><code>二阶偏导数例题</code></strong>:</p></blockquote><blockquote><p><code>技巧</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-8.png" alt="alt text"></p><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-9.png" alt="alt text"></p><h1 id="求全微分"><a href="#求全微分" class="headerlink" title="求全微分"></a>求全微分</h1><blockquote><p><strong><code>全微分计算公式</code></strong>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-10.png" alt="alt text"></p><blockquote><p><strong><code>全微分求解</code></strong>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-11.png" alt="alt text"></p><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-12.png" alt="alt text"></p><h1 id="求复合函数的偏导"><a href="#求复合函数的偏导" class="headerlink" title="求复合函数的偏导"></a>求复合函数的偏导</h1><blockquote><p><strong><code>复合函数求偏导</code></strong>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-23.png" alt="alt text"></p><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-24.png" alt="alt text"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-25.png" alt="alt text"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-26.png" alt="alt text"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-27.png" alt="alt text"></p><h1 id="隐函数求偏导数"><a href="#隐函数求偏导数" class="headerlink" title="隐函数求偏导数"></a>隐函数求偏导数</h1><blockquote><p><strong><code>隐函数求偏导数</code></strong>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-28.png" alt="alt text"></p><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-29.png" alt="alt text"></p><h1 id="求多元函数极值"><a href="#求多元函数极值" class="headerlink" title="求多元函数极值"></a>求多元函数极值</h1><blockquote><p><strong><code>极值定理</code></strong>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-13.png" alt="alt text"></p><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-14.png" alt="alt text"></p><h1 id="方向导数与梯度"><a href="#方向导数与梯度" class="headerlink" title="方向导数与梯度"></a>方向导数与梯度</h1><blockquote><p><strong><code>求方向导数</code></strong>:</p></blockquote><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-18.png" alt="alt text"></p><blockquote><p><strong><code>求梯度</code></strong>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-15.png" alt="alt text"></p><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-16.png" alt="alt text"></p><blockquote><p><strong><code>求在点处的最大方向导数</code></strong>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-17.png" alt="alt text"></p><h1 id="连续,可偏导,可微分,方向导数的关系"><a href="#连续,可偏导,可微分,方向导数的关系" class="headerlink" title="连续,可偏导,可微分,方向导数的关系"></a>连续,可偏导,可微分,方向导数的关系</h1><blockquote><p><strong><code>关系金字塔</code></strong>(<strong><code>强能推弱</code>-<code>平级之间也不能相互推导</code></strong>):</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-19.png" alt="alt text"></p><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-20.png" alt="alt text"></p><h1 id="隐函数存在定理-了解"><a href="#隐函数存在定理-了解" class="headerlink" title="隐函数存在定理(了解)"></a>隐函数存在定理<code>(了解)</code></h1><blockquote><p><strong><code>隐函数存在定理</code></strong>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-21.png" alt="alt text"></p><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/22439/image-22.png" alt="alt text"></p>]]></content>
<categories>
<category> 高等数学(下)复习 </category>
</categories>
<tags>
<tag> 多元函数微分学题型总结 </tag>
</tags>
</entry>
<entry>
<title>无穷级数</title>
<link href="/posts/34027/"/>
<url>/posts/34027/</url>
<content type="html"><![CDATA[<blockquote><p>当拿到我们拿到一个无穷级数的题目时,下面就来总结一下无穷级数的判别方法。</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-16.png" alt="alt text"></p><h1 id="用定义判定级数收敛性"><a href="#用定义判定级数收敛性" class="headerlink" title="用定义判定级数收敛性"></a>用定义判定级数收敛性</h1><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image.png" alt="alt text"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-1.png" alt="alt text"></p><h1 id="数项级数敛散性判定"><a href="#数项级数敛散性判定" class="headerlink" title="数项级数敛散性判定"></a>数项级数敛散性判定</h1><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-2.png" alt="alt text"></p><h1 id="收敛级数的基本性质"><a href="#收敛级数的基本性质" class="headerlink" title="收敛级数的基本性质"></a>收敛级数的基本性质</h1><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-3.png" alt="alt text"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-4.png" alt="alt text"></p><h2 id="使用常用结论判别敛散性"><a href="#使用常用结论判别敛散性" class="headerlink" title="使用常用结论判别敛散性"></a>使用<code>常用结论</code>判别敛散性</h2><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-5.png" alt="alt text"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-6.png" alt="alt text"></p><h2 id="正项级数敛散性判别法"><a href="#正项级数敛散性判别法" class="headerlink" title="正项级数敛散性判别法"></a><code>正项级数</code>敛散性判别法</h2><blockquote><p><code>技巧</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-7.png" alt="alt text"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-8.png" alt="alt text"></p><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-9.png" alt="alt text"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-10.png" alt="alt text"></p><h2 id="使用比值判别法和根值判别法判别收敛性"><a href="#使用比值判别法和根值判别法判别收敛性" class="headerlink" title="使用比值判别法和根值判别法判别收敛性"></a>使用<code>比值判别法</code>和<code>根值判别法</code>判别收敛性</h2><blockquote><p><code>技巧</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-11.png" alt="alt text"></p><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-12.png" alt="alt text"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-13.png" alt="alt text"></p><h2 id="交错级数敛散性判别法"><a href="#交错级数敛散性判别法" class="headerlink" title="交错级数敛散性判别法"></a><code>交错级数</code>敛散性判别法</h2><blockquote><p><code>技巧</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-20.png" alt="alt text"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-14.png" alt="alt text"></p><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-15.png" alt="alt text"></p><h2 id="绝对收敛和条件收敛判别法"><a href="#绝对收敛和条件收敛判别法" class="headerlink" title="绝对收敛和条件收敛判别法"></a><code>绝对收敛</code>和<code>条件收敛</code>判别法</h2><blockquote><p><code>技巧</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-17.png" alt="alt text"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-19.png" alt="alt text"></p><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-18.png" alt="alt text"></p><h1 id="幂级数"><a href="#幂级数" class="headerlink" title="幂级数"></a>幂级数</h1><p><code>总结</code>:</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-21.png" alt="alt text"></p><h2 id="幂级数求收敛域-收敛区间"><a href="#幂级数求收敛域-收敛区间" class="headerlink" title="幂级数求收敛域(收敛区间)"></a>幂级数求收敛域<code>(收敛区间)</code></h2><blockquote><p><strong><code>题型1</code></strong>:</p></blockquote><blockquote><p><code>技巧</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-23.png" alt="alt text"></p><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-22.png" alt="alt text"></p><blockquote><p><strong><code>题型2</code></strong>:</p></blockquote><blockquote><p><code>技巧</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-24.png" alt="alt text"></p><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-25.png" alt="alt text"></p><blockquote><p><strong><code>题型3</code></strong>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-26.png" alt="alt text"></p><h1 id="题库例题"><a href="#题库例题" class="headerlink" title="题库例题"></a>题库例题</h1><h2 id="题目一-——-P级数的收敛性判别法,P-1-——-收敛-,P-1-——-发散"><a href="#题目一-——-P级数的收敛性判别法,P-1-——-收敛-,P-1-——-发散" class="headerlink" title="题目一 ——> P级数的收敛性判别法,P > 1 ——> 收敛 ,P =< 1 ——> 发散"></a><code>题目一</code> ——> P级数的收敛性判别法,<code>P > 1 ——> 收敛</code> ,<code>P =< 1 ——> 发散</code></h2><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-27.png" alt="alt text"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-32.png" alt="alt text"></p><p><code>P级数的收敛性判别法</code> ——> <img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-5.png" alt="alt text"></p><h2 id="题目二-——-收-发-发-,补充-收-收-收,发-发-不确定"><a href="#题目二-——-收-发-发-,补充-收-收-收,发-发-不确定" class="headerlink" title="题目二 ——> 收 + 发 = 发 ,补充: 收 + 收 = 收,发 + 发 = 不确定"></a><code>题目二</code> ——> <strong><code>收 + 发 = 发</code></strong> ,补充: <code>收 + 收 = 收</code>,<code>发 + 发 = 不确定</code></h2><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-28.png" alt="alt text"></p><h2 id="题目三-——-限定在某一个范围内,级数收敛"><a href="#题目三-——-限定在某一个范围内,级数收敛" class="headerlink" title="题目三 ——> 限定在某一个范围内,级数收敛"></a><code>题目三</code> ——> 限定在某一个范围内,级数收敛</h2><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-29.png" alt="alt text"></p><h2 id="题目四-——-带入级数式子直接求解"><a href="#题目四-——-带入级数式子直接求解" class="headerlink" title="题目四 ——> 带入级数式子直接求解"></a><code>题目四</code> ——> 带入级数式子直接求解</h2><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-30.png" alt="alt text"></p><h2 id="题目五-——-级数收敛的必要条件"><a href="#题目五-——-级数收敛的必要条件" class="headerlink" title="题目五 ——> 级数收敛的必要条件"></a><code>题目五</code> ——> 级数收敛的必要条件</h2><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-31.png" alt="alt text"></p><h2 id="题目六-——-幂级数的收敛区间"><a href="#题目六-——-幂级数的收敛区间" class="headerlink" title="题目六 ——> 幂级数的收敛区间"></a><code>题目六</code> ——> 幂级数的收敛区间</h2><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-33.png" alt="alt text"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-34.png" alt="alt text"></p><blockquote><p><code>技巧</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-23.png" alt="alt text"></p><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-22.png" alt="alt text"></p><h2 id="题目七-——-等比级数的收敛性判别法"><a href="#题目七-——-等比级数的收敛性判别法" class="headerlink" title="题目七 ——> 等比级数的收敛性判别法"></a><code>题目七</code> ——> 等比级数的收敛性判别法</h2><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-6.png" alt="alt text"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/34027/image-35.png" alt="alt text"></p>]]></content>
<categories>
<category> 高等数学(下)复习 </category>
</categories>
<tags>
<tag> 无穷级数题型总结 </tag>
</tags>
</entry>
<entry>
<title>向量代数与空间解析几何</title>
<link href="/posts/39261/"/>
<url>/posts/39261/</url>
<content type="html"><![CDATA[<h1 id="题型总结"><a href="#题型总结" class="headerlink" title="题型总结:"></a><code>题型总结</code>:</h1><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/39261/image.png" alt="alt text"></p><h1 id="空间向量"><a href="#空间向量" class="headerlink" title="空间向量"></a>空间向量</h1><h2 id="向量的模"><a href="#向量的模" class="headerlink" title="向量的模"></a>向量的模</h2><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/39261/image-1.png" alt="alt text"></p><h2 id="向量积"><a href="#向量积" class="headerlink" title="向量积"></a>向量积</h2><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/39261/image-2.png" alt="alt text"></p><h2 id="利用向量积求图形面积"><a href="#利用向量积求图形面积" class="headerlink" title="利用向量积求图形面积"></a>利用向量积求图形面积</h2><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/39261/image-3.png" alt="alt text"></p><h1 id="空间平面方程"><a href="#空间平面方程" class="headerlink" title="空间平面方程"></a>空间平面方程</h1><h2 id="平面的点法式方程"><a href="#平面的点法式方程" class="headerlink" title="平面的点法式方程"></a>平面的点法式方程</h2><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/39261/image-4.png" alt="alt text"></p><h2 id="两平面的夹角"><a href="#两平面的夹角" class="headerlink" title="两平面的夹角"></a>两平面的夹角</h2><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/39261/image-5.png" alt="alt text"></p><h2 id="点到平面的距离"><a href="#点到平面的距离" class="headerlink" title="点到平面的距离"></a>点到平面的距离</h2><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/39261/image-6.png" alt="alt text"></p><h1 id="空间直线方程"><a href="#空间直线方程" class="headerlink" title="空间直线方程"></a>空间直线方程</h1><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/39261/image-7.png" alt="alt text"></p><h2 id="直线的一般方程"><a href="#直线的一般方程" class="headerlink" title="直线的一般方程"></a>直线的一般方程</h2><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/39261/image-8.png" alt="alt text"></p><h2 id="空间直线对称式方程,空间直线的参数方程"><a href="#空间直线对称式方程,空间直线的参数方程" class="headerlink" title="空间直线对称式方程,空间直线的参数方程"></a>空间直线对称式方程,空间直线的参数方程</h2><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/39261/image-9.png" alt="alt text"></p><h1 id="旋转曲面方程"><a href="#旋转曲面方程" class="headerlink" title="旋转曲面方程"></a>旋转曲面方程</h1><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/39261/image-10.png" alt="alt text"></p><h1 id="空间曲线在平面的投影"><a href="#空间曲线在平面的投影" class="headerlink" title="空间曲线在平面的投影"></a>空间曲线在平面的投影</h1><blockquote><p><code>解题方法</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/39261/image-11.png" alt="alt text"></p><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/39261/image-12.png" alt="alt text"></p><h1 id="空间曲面的切平面和法线"><a href="#空间曲面的切平面和法线" class="headerlink" title="空间曲面的切平面和法线"></a>空间曲面的切平面和法线</h1><blockquote><p><code>题型1</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/39261/image-13.png" alt="alt text"></p><blockquote><p><code>题型2</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/39261/image-14.png" alt="alt text"></p><h1 id="空间曲线的切线和法平面"><a href="#空间曲线的切线和法平面" class="headerlink" title="空间曲线的切线和法平面"></a>空间曲线的切线和法平面</h1><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/39261/image-15.png" alt="alt text"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/39261/image-16.png" alt="alt text"></p>]]></content>
<categories>
<category> 高等数学(下)复习 </category>
</categories>
<tags>
<tag> 向量代数与空间解析几何题型总结 </tag>
</tags>
</entry>
<entry>
<title>微分方程</title>
<link href="/posts/57959/"/>
<url>/posts/57959/</url>
<content type="html"><![CDATA[<h1 id="一阶微分方程"><a href="#一阶微分方程" class="headerlink" title="一阶微分方程"></a>一阶微分方程</h1><p><code>题目拿到手,首先要对题目类型进行分类,根据题目类型,我们可以将题目分为以下几类</code>:</p><ul><li><strong>变量可分离型</strong></li><li><strong>齐次微分方程</strong></li><li><strong>一阶线性微分方程</strong></li><li><strong>积分方程<code>(了解)</code></strong></li><li><strong>x(y)型方程<code>(了解)</code></strong></li></ul><h1 id="题型辨别:"><a href="#题型辨别:" class="headerlink" title="题型辨别:"></a><code>题型辨别</code>:</h1><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-15.png" alt="alt text"></p><h1 id="题型总结:"><a href="#题型总结:" class="headerlink" title="题型总结:"></a><code>题型总结</code>:</h1><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image.png" alt="alt text"></p><h2 id="变量可分离型"><a href="#变量可分离型" class="headerlink" title="变量可分离型"></a>变量可分离型</h2><blockquote><p>**<code>解题步骤</code>**:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-1.png" alt="alt text"></p><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-2.png" alt="alt text"></p><blockquote><p><code>练习题</code>:</p></blockquote><p>1.<br><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-3.png" alt="alt text"></p><p>2.<br><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-4.png" alt="alt text"></p><h2 id="齐次微分方程"><a href="#齐次微分方程" class="headerlink" title="齐次微分方程"></a>齐次微分方程</h2><blockquote><p>**<code>解题步骤</code>**:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-5.png" alt="alt text"></p><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-6.png" alt="alt text"></p><blockquote><p><code>练习题</code>:</p></blockquote><p>1.<br><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-7.png" alt="alt text"></p><h2 id="一阶线性微分方程"><a href="#一阶线性微分方程" class="headerlink" title="一阶线性微分方程"></a>一阶线性微分方程</h2><blockquote><p><strong><code>公式推导</code></strong>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-10.png" alt="alt text"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-11.png" alt="alt text"></p><blockquote><p><code>解题步骤</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-8.png" alt="alt text"></p><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-9.png" alt="alt text"></p><blockquote><p><code>练习题</code>:</p></blockquote><p>1.<br><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-12.png" alt="alt text"></p><p>1.<br><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-13.png" alt="alt text"></p><h2 id="积分方程-了解"><a href="#积分方程-了解" class="headerlink" title="积分方程(了解)"></a>积分方程<code>(了解)</code></h2><blockquote><p><code>解题步骤</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-14.png" alt="alt text"></p><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-14.png" alt="alt text"></p><h2 id="x-y-型方程"><a href="#x-y-型方程" class="headerlink" title="x(y)型方程"></a>x(y)型方程</h2><blockquote><p>**<code>解题步骤</code>**:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-16.png" alt="alt text"></p><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-16.png" alt="alt text"></p><h1 id="二阶微分方程"><a href="#二阶微分方程" class="headerlink" title="二阶微分方程"></a>二阶微分方程</h1><p><code>题目拿到手,首先要对题目类型进行分类,根据题目类型,我们可以将题目分为以下几类</code>:</p><ul><li><strong>可降阶的二阶微分方程<code>(了解)</code></strong></li><li><strong>二阶常系数齐次线性微分方程</strong></li><li><strong>n阶常系数齐次线性微分方程<code>(了解)</code></strong></li><li><strong>二阶常系数非齐次线性微分方程<code>(了解)</code></strong></li></ul><h1 id="题型总结:-1"><a href="#题型总结:-1" class="headerlink" title="题型总结:"></a><code>题型总结</code>:</h1><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-17.png" alt="alt text"></p><h2 id="可降阶的二阶微分方程-了解"><a href="#可降阶的二阶微分方程-了解" class="headerlink" title="可降阶的二阶微分方程(了解)"></a>可降阶的二阶微分方程<code>(了解)</code></h2><blockquote><p><strong><code>题型1</code></strong>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-22.png" alt="alt text"></p><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-23.png" alt="alt text"></p><blockquote><p><code>练习题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-24.png" alt="alt text"></p><blockquote><p><strong><code>题型2</code></strong>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-20.png" alt="alt text"></p><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-18.png" alt="alt text"></p><blockquote><p><code>练习题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-25.png" alt="alt text"></p><blockquote><p><strong><code>题型3</code></strong>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-21.png" alt="alt text"></p><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-19.png" alt="alt text"></p><blockquote><p><code>练习题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-26.png" alt="alt text"></p><h2 id="二阶常系数齐次线性微分方程"><a href="#二阶常系数齐次线性微分方程" class="headerlink" title="二阶常系数齐次线性微分方程"></a>二阶常系数齐次线性微分方程</h2><blockquote><p>**<code>解题步骤</code>**:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-27.png" alt="alt text"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-28.png" alt="alt text"></p><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-29.png" alt="alt text"></p><h2 id="n阶常系数齐次线性微分方程-了解"><a href="#n阶常系数齐次线性微分方程-了解" class="headerlink" title="n阶常系数齐次线性微分方程(了解)"></a>n阶常系数齐次线性微分方程<code>(了解)</code></h2><blockquote><p>**<code>解题步骤</code>**:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-31.png" alt="alt text"></p><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-30.png" alt="alt text"></p><h2 id="二阶常系数非齐次线性微分方程-了解"><a href="#二阶常系数非齐次线性微分方程-了解" class="headerlink" title="二阶常系数非齐次线性微分方程(了解)"></a>二阶常系数非齐次线性微分方程<code>(了解)</code></h2><blockquote><p><strong><code>题型1</code></strong>:</p></blockquote><blockquote><p><code>解题步骤</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-32.png" alt="alt text"></p><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-33.png" alt="alt text"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-34.png" alt="alt text"></p><blockquote><p><strong><code>题型2</code></strong>:</p></blockquote><blockquote><p><code>解题步骤</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-35.png" alt="alt text"></p><blockquote><p><code>例题</code>:</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/57959/image-36.png" alt="alt text"></p>]]></content>
<categories>
<category> 高等数学(下)复习 </category>
</categories>
<tags>
<tag> 微分方程题型总结 </tag>
</tags>
</entry>
<entry>
<title>第3讲:分支和循环</title>
<link href="/posts/62221/"/>
<url>/posts/62221/</url>
<content type="html"><![CDATA[<h1 id="目录"><a href="#目录" class="headerlink" title="目录"></a>目录</h1><ol><li><p>if语句</p></li><li><p>关系操作符</p></li><li><p>条件操作符</p></li><li><p>逻辑操作符:&& , || , !</p></li><li><p>switch语句</p></li><li><p>while循环</p></li><li><p>for循环</p></li><li><p>do-while循环</p></li><li><p>break和continue语句</p></li><li><p>循环的嵌套</p></li><li><p>goto语句</p></li></ol><blockquote><p><strong>正文开始</strong></p></blockquote><p>C语言是结构化的程序设计语言,这里的结构指的是 <strong>顺序结构、选择结构、循环结构</strong> ,C语言是能够实<br>现这三种结构的,其实我们如果仔细分析,我们日常所⻅的事情都可以拆分为这三种结构或者这三种<br>结构的组合。<br>我们可以使用if、switch实现分支结构,使用for、while、do while实现循环结构。</p><h1 id="1-if语句"><a href="#1-if语句" class="headerlink" title="1. if语句"></a>1. if语句</h1><h2 id="1-1-if"><a href="#1-1-if" class="headerlink" title="1.1 if"></a>1.1 if</h2><p>if语句的语法形式如下:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> ( 表达式 )</span><br><span class="line">语句</span><br></pre></td></tr></table></figure><p><code>表达式成立(为真)</code>,则<code>语句执行</code>,表达式<code>不成立(为假)</code>,则<code>语句不执行</code></p><p><strong><code>在C语言中, 0 为假,非 0 表示真</code></strong> ,也就是表达式的结果如果是 0 ,则语句不执行,表达式的结果如果不是 0 ,则语句执行。</p><blockquote><p>**<code>例子</code>**: 输入一个整数,判断是否为奇数</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> num = <span class="number">0</span> ;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &num);</span><br><span class="line"> <span class="keyword">if</span>(num % <span class="number">2</span> == <span class="number">1</span> )</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d 是奇数\n"</span>, num);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/62221/image.png" alt="alt text"></p><h2 id="1-2-else"><a href="#1-2-else" class="headerlink" title="1.2 else"></a>1.2 else</h2><blockquote><p>如果一个数不是奇数,那就是偶数了,如果任意一个整数,我们要清楚的判断是奇数还是偶数怎么表示呢?</p></blockquote><p>这里就需要 **<code>if...else...</code>**语句了,语法形式如下:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> ( 表达式 )</span><br><span class="line"> 语句 <span class="number">1</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">语句 <span class="number">2</span></span><br></pre></td></tr></table></figure><blockquote><p><strong><code>例子</code>:</strong> 输入一个整数,判断是否为奇数,如果是奇数打印是奇数,否则打印偶数。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> num = <span class="number">0</span> ;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &num);</span><br><span class="line"> <span class="keyword">if</span>(num % <span class="number">2</span> == <span class="number">1</span> )</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d 是奇数\n"</span>, num);</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d 是偶数\n"</span>, num);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><blockquote><p><strong><code>练习</code>:</strong> 输入一个年龄,>=18岁就输出:成年,否则就输出:未成年</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> age = <span class="number">0</span> ;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &age);</span><br><span class="line"> <span class="keyword">if</span>(age>= <span class="number">18</span> )</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"成年\n"</span>);</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"未成年\n"</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h1 id="1-3-分支中包含多条语句"><a href="#1-3-分支中包含多条语句" class="headerlink" title="1.3 分支中包含多条语句"></a>1.3 分支中包含多条语句</h1><p>默认在<code>if</code>和<code>else</code>语句中默认都只控制一条语句,比如:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> age = <span class="number">0</span> ;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &age);</span><br><span class="line"> <span class="keyword">if</span>(age >= <span class="number">18</span> )</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"成年了\n"</span>);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"可以谈恋爱了\n"</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>上面的代码,你会发现输入的值不管是>=18还是<18,”可以谈恋爱了”都会打印在屏幕上。</p><p>这是因为if语句只能控制一条语句,就是printf(“成年了\n”);,if语句为真,则打印成年了,<br>if语句为假,则不打印,对于printf(“可以谈恋爱了\n”);是独立存在的,不管if语句的条件的真<br>假,都会被执行。那如果我们要if语句同时控制 2 条语句,怎么办呢?那就要使用{}将代码括起来,<br>else后也可以跟上大括号。如下:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int age = 0 ;</span><br><span class="line">scanf("%d", &age);</span><br><span class="line">if(age >= 18 ) //if 后使用{} 控制多条语句-这个块也叫:程序块,或者复合语句</span><br><span class="line">{</span><br><span class="line">printf("成年了\n");</span><br><span class="line">printf("可以谈恋爱了\n");</span><br><span class="line">}</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int age = 0 ;</span><br><span class="line">scanf("%d", &age);</span><br><span class="line">if(age >= 18 ) //if 后使用{} 控制多条语句-这个块也叫:程序块,或者复合语句</span><br><span class="line">{</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">printf("成年了\n");</span><br><span class="line">printf("可以谈恋爱了\n");</span><br><span class="line">}</span><br><span class="line">else //else 后使用{}控制多条语句-这个块也叫:程序块,或者复合语句</span><br><span class="line">{</span><br><span class="line">printf("未成年\n");</span><br><span class="line">printf("不可以早恋哦\n");</span><br><span class="line">}</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td></tr></table></figure><h3 id="1-4-嵌套if"><a href="#1-4-嵌套if" class="headerlink" title="1.4 嵌套if"></a>1.4 嵌套if</h3><p>在if else语句中,else可以与另一个if语句连用,构成多重判断。<br>比如:要求输入一个整数,判断输入的整数是 0 ,还是正数或者负数。请看如下代码:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int num = 0 ;</span><br><span class="line">scanf("%d", &num);</span><br><span class="line">if(num == 0 )</span><br><span class="line">printf("输入的数字是0\n");</span><br><span class="line">else if(num > 0 ) //这里的if 相当于嵌套在else语句中,形成了嵌套结构</span><br><span class="line">printf("输入的数字是正数\n");</span><br><span class="line">else</span><br><span class="line">printf("输入的数字是负数\n");</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td></tr></table></figure><p>上图中橙色背景的代码就是嵌套在前面的else子句中的,构成了嵌套的if语句。<br>再比如:<br>输入一个整数,如果是正数,再判断是奇数还是偶数,并输出;如果不是正数则输出:非正数。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int num = 0 ;</span><br><span class="line">scanf("%d", &num);</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">if(num> 0 )</span><br><span class="line">{</span><br><span class="line">if(num% 2 == 0 )</span><br><span class="line">printf("偶数\n");</span><br><span class="line">else</span><br><span class="line">printf("奇数\n");</span><br><span class="line">}</span><br><span class="line">else</span><br><span class="line">{</span><br><span class="line">printf("非正数\n");</span><br><span class="line">}</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td></tr></table></figure><p>上面的代码中橙色背景的代码也是嵌套在if语句中的,构成了嵌套的if语句。</p><p>以上就是嵌套if语句的语法,有了嵌套if语句,就可以完成更多复杂逻辑的判断。</p><p>练习:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">输入一个人的年龄</span><br><span class="line">如果年龄< 18 岁,打印"少年"</span><br><span class="line">如果年龄在 18 岁至 44 岁打印"⻘年"</span><br><span class="line">如果年龄在 45 岁至 59 岁打印"中老年"</span><br><span class="line">如果年龄在 60 岁至 89 岁打印"老年"</span><br><span class="line">如果 90 岁及以上打印"老寿星"</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6</span><br></pre></td></tr></table></figure><h4 id="参考代码:"><a href="#参考代码:" class="headerlink" title="参考代码:"></a>参考代码:</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">//方法 1</span><br><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int age = 0 ;</span><br><span class="line">scanf("%d", &age);</span><br><span class="line">if(age< 18 )</span><br><span class="line">printf("少年\n");</span><br><span class="line">else if(age<= 44 )</span><br><span class="line">printf("⻘年\n");</span><br><span class="line">else if(age<= 59 )</span><br><span class="line">printf("中老年\n");</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">else if(age<= 89 )</span><br><span class="line">printf("老年\n");</span><br><span class="line">else</span><br><span class="line">printf("老寿星\n");</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">//带上大括号更容易看明白</span><br><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int age = 0 ;</span><br><span class="line">scanf("%d", &age);</span><br><span class="line">if(age< 18 )</span><br><span class="line">{</span><br><span class="line">printf("少年\n");</span><br><span class="line">}</span><br><span class="line">else</span><br><span class="line">{</span><br><span class="line">if(age<= 44 )</span><br><span class="line">{</span><br><span class="line">printf("⻘年\n");</span><br><span class="line">}</span><br><span class="line">else</span><br><span class="line">{</span><br><span class="line">if(age<= 59 )</span><br><span class="line">{</span><br><span class="line">printf("中老年\n");</span><br><span class="line">}</span><br><span class="line">else</span><br><span class="line">{</span><br><span class="line">if(age<= 89 )</span><br><span class="line">printf("老年\n");</span><br><span class="line">else</span><br><span class="line">printf("老寿星\n");</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td></tr></table></figure><h3 id="1-5-悬空else问题"><a href="#1-5-悬空else问题" class="headerlink" title="1.5 悬空else问题"></a>1.5 悬空else问题</h3><p>如果有多个if和else,可以记住这样一条规则,else总是跟最接近的if匹配。<br>我们首先从下面代码开始</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int a = 0 ;</span><br><span class="line">int b = 2 ;</span><br><span class="line">if(a == 1 )</span><br><span class="line">if(b == 2 )</span><br><span class="line">printf("hehe\n");</span><br><span class="line">else</span><br><span class="line">printf("haha\n");</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td></tr></table></figure><p>程序运行的结果是啥?<br>很多初学者,上来以判断a是0,不等于1,那就执行else子句,打印haha<br>但是当你去运行代码,输出的结果是:啥都不输出。</p><h4 id="为什么呢?"><a href="#为什么呢?" class="headerlink" title="为什么呢?"></a>为什么呢?</h4><p>这就是悬空else的问题,如果有多个if和else,可以记住这样一条规则,else总是跟最接近<br>的if匹配。<br>上面的代码排版,让else和第一个if语句对⻬,让我们以为else是和第一个if匹配的,当if<br>语句不成立的时候,自然想到的就是执行else子句,打印haha,但实际上else是和第二个if<br>进行匹配的,这样后边的if…else语句是嵌套在第一个if语句中的,如果第一个if语句就不<br>成立,嵌套if和else就没机会执行了,最终啥都不打印。<br>如果代码改成下面这样就更加容易理解了</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int a = 0 ;</span><br><span class="line">int b = 2 ;</span><br><span class="line">if(a == 1 )</span><br><span class="line">{</span><br><span class="line">if(b == 2 )</span><br><span class="line">printf("hehe\n");</span><br><span class="line">else</span><br><span class="line">printf("haha\n");</span><br><span class="line">}</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td></tr></table></figure><p>或者如果我们希望else确实和第一个if匹配,可以这样修改代码:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int a = 0 ;</span><br><span class="line">int b = 2 ;</span><br><span class="line">if(a == 1 )</span><br><span class="line">{</span><br><span class="line">if(b == 2 )</span><br><span class="line">printf("hehe\n");</span><br><span class="line">}</span><br><span class="line">else</span><br><span class="line">{</span><br><span class="line">printf("haha\n");</span><br><span class="line">}</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td></tr></table></figure><p>只要带上适当的大括号,代码的逻辑就会更加的清晰,所以大家以后在写代码的时候要注意括号的使<br>用,让代码的可读性更高。</p><h2 id="2-关系操作符"><a href="#2-关系操作符" class="headerlink" title="2. 关系操作符"></a>2. 关系操作符</h2><p>C语言用于比较的表达式,称为“ <strong>关系表达式</strong> ”(relationalexpression),里面使用的运算符就称<br>为“关系运算符”(relationaloperator),主要有下面 6 个。</p><ul><li><blockquote><p>大于运算符</p></blockquote></li><li><p><小于运算符</p></li><li><blockquote><p>=大于等于运算符</p></blockquote></li><li><p><=小于等于运算符</p></li><li><p>==相等运算符</p></li><li><p>!=不相等运算符<br>下面是一些例子。</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">a == b;</span><br><span class="line">a != b;</span><br><span class="line">a < b;</span><br><span class="line">a > b;</span><br><span class="line">a <= b;</span><br><span class="line">a >= b;</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6</span><br></pre></td></tr></table></figure><h4 id="关系表达式通常返回-0-或-1-,表示真假。"><a href="#关系表达式通常返回-0-或-1-,表示真假。" class="headerlink" title="关系表达式通常返回 0 或 1 ,表示真假。"></a>关系表达式通常返回 0 或 1 ,表示真假。</h4><h4 id="C语言中,-0-表示假,所有非零值表示真。比如,20-12返回-1-,12-20返回-0-。"><a href="#C语言中,-0-表示假,所有非零值表示真。比如,20-12返回-1-,12-20返回-0-。" class="headerlink" title="C语言中, 0 表示假,所有非零值表示真。比如,20 > 12返回 1 ,12 > 20返回 0 。"></a>C语言中, 0 表示假,所有非零值表示真。比如,20 > 12返回 1 ,12 > 20返回 0 。</h4><p>关系表达式常用于if或while结构。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">if (x == 3 ) {</span><br><span class="line">printf("x is 3.\n");</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td></tr></table></figure><p>注意:相等运算符==与赋值运算符=是两个不一样的运算符,不要混淆。有时候,可能会不小心写<br>出下面的代码,它可以运行,但很容易出现意料之外的结果。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 if (x = 3 ) ...</span><br></pre></td></tr></table></figure><p>上面示例中,原意是x == 3,但是不小心写成x = 3。这个式子表示对变量x赋值 3 ,它的返<br>回值为 3 ,所以if判断总是为真。<br>为了防止出现这种错误,有的程序员喜欢将变量写在等号的右边。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 if ( 3 == x) ...</span><br></pre></td></tr></table></figure><h4 id="这样的话,如果把-误写成-,编译器就会报错。"><a href="#这样的话,如果把-误写成-,编译器就会报错。" class="headerlink" title="这样的话,如果把==误写成=,编译器就会报错。"></a>这样的话,如果把==误写成=,编译器就会报错。</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">/* 报错 */</span><br><span class="line">if ( 3 = x) ...</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td></tr></table></figure><p>另一个需要避免的错误是: <strong>多个关系运算符不宜连用</strong> 。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 i < j < k</span><br></pre></td></tr></table></figure><h4 id="上面示例中,连续使用两个小于运算符。这是合法表达式,不会报错,但是通常达不到想要的结果,"><a href="#上面示例中,连续使用两个小于运算符。这是合法表达式,不会报错,但是通常达不到想要的结果," class="headerlink" title="上面示例中,连续使用两个小于运算符。这是合法表达式,不会报错,但是通常达不到想要的结果,"></a>上面示例中,连续使用两个小于运算符。这是合法表达式,不会报错,但是通常达不到想要的结果,</h4><p>即不是保证变量j的值在i和k之间。因为关系运算符是从左到右计算,所以实际执行的是下面的<br>表达式。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 (i < j) < k</span><br></pre></td></tr></table></figure><p>上面式子中,i < j返回 0 或 1 ,所以最终是 0 或 1 与变量k进行比较。如果想要判断变量j<br>的值是否在i和k之间,应该使用下面的写法。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 i < j && j < k</span><br></pre></td></tr></table></figure><h4 id="比如:我们输入一个年龄,如果年龄在-18-岁-36岁之间,我们输出⻘年。"><a href="#比如:我们输入一个年龄,如果年龄在-18-岁-36岁之间,我们输出⻘年。" class="headerlink" title="比如:我们输入一个年龄,如果年龄在 18 岁~36岁之间,我们输出⻘年。"></a>比如:我们输入一个年龄,如果年龄在 18 岁~36岁之间,我们输出⻘年。</h4><h4 id="如果我们这样写"><a href="#如果我们这样写" class="headerlink" title="如果我们这样写"></a>如果我们这样写</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int age = 0 ;</span><br><span class="line">scanf("%d", &age);</span><br><span class="line">if( 18 <=age<= 36 )</span><br><span class="line">{</span><br><span class="line">printf("⻘年\n");</span><br><span class="line">}</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">11</span><br></pre></td></tr></table></figure><h4 id="当我们输入-10-的时候,依然输出⻘年,如下图:"><a href="#当我们输入-10-的时候,依然输出⻘年,如下图:" class="headerlink" title="当我们输入 10 的时候,依然输出⻘年,如下图:"></a>当我们输入 10 的时候,依然输出⻘年,如下图:</h4><p>这是因为,我们先拿 18 和age中存放的 10 比较,表达式18<=10为假,18<=age的结果是 0 ,再拿 0 和<br>36 比较,0<=36为真,所以打印了⻘年,所以即使当age是 10 的时候,也能打印⻘年,逻辑上是有<br>问题,这个代码应该怎么写呢?</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int age = 0 ;</span><br><span class="line">scanf("%d", &age);</span><br><span class="line">if(age>= 18 && age<= 36 )</span><br><span class="line">{</span><br><span class="line">printf("⻘年\n");</span><br><span class="line">}</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">11</span><br></pre></td></tr></table></figure><p>以上就是关于操作符,我们需要掌握的,剩下的只要按照字面意思理解使用就行,没有特别注意的。</p><h2 id="3-条件操作符"><a href="#3-条件操作符" class="headerlink" title="3. 条件操作符"></a>3. 条件操作符</h2><h4 id="条件操作符也叫三目操作符,需要接受三个操作数的,形式如下:"><a href="#条件操作符也叫三目操作符,需要接受三个操作数的,形式如下:" class="headerlink" title="条件操作符也叫三目操作符,需要接受三个操作数的,形式如下:"></a>条件操作符也叫三目操作符,需要接受三个操作数的,形式如下:</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 exp1? exp2 : exp</span><br></pre></td></tr></table></figure><p>条件操作符的计算逻辑是:如果exp1为真,exp2计算,计算的结果是整个表达式的结果;如果<br>exp1为假,exp3计算,计算的结果是整个表达式的结果。</p><p>练习 1 :使用条件操作符表示下面代码的逻辑</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">{</span><br><span class="line">int a = 0 ;</span><br><span class="line">int b = 0 ;</span><br><span class="line">scanf("%d", &a);</span><br><span class="line">if (a > 5 )</span><br><span class="line">b = 3 ;</span><br><span class="line">else</span><br><span class="line">b = -3;</span><br><span class="line">printf("%d\n", b);</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br><span class="line">改造后:</span><br><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int a = 0 ;</span><br><span class="line">int b = 0 ;</span><br><span class="line">scanf("%d", &a);</span><br><span class="line">b = a> 5? 3 :-3;</span><br><span class="line">printf("%d\n", b);</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td></tr></table></figure><p>练习 2 :使用条件表达式实现找两个数中较大值。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int a = 0 ;</span><br><span class="line">int b = 0 ;</span><br><span class="line">scanf("%d %d", &a, &b);</span><br><span class="line">int m = a>b? a : b;</span><br><span class="line">printf("%d\n", m);</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td></tr></table></figure><h2 id="4-逻辑操作符:-!"><a href="#4-逻辑操作符:-!" class="headerlink" title="4. 逻辑操作符:&&, | |, !"></a>4. 逻辑操作符:&&, | |, !</h2><h4 id="逻辑运算符提供逻辑判断功能,用于构建更复杂的表达式,主要有下面三个运算符。"><a href="#逻辑运算符提供逻辑判断功能,用于构建更复杂的表达式,主要有下面三个运算符。" class="headerlink" title="逻辑运算符提供逻辑判断功能,用于构建更复杂的表达式,主要有下面三个运算符。"></a>逻辑运算符提供逻辑判断功能,用于构建更复杂的表达式,主要有下面三个运算符。</h4><ul><li>!:逻辑取反运算符(改变单个表达式的真假)。</li><li>&&:逻辑与运算符,就是并且的意思(两侧的表达式都为真,则为真,否则为假)。</li><li>||:逻辑或运算符,就是或者的意思(两侧至少有一个表达式为真,则为真,否则为假)。<br> <strong>注:</strong> C语言中,非 0 表示真, 0 表示假</li></ul><h3 id="4-1-逻辑取反运算符!"><a href="#4-1-逻辑取反运算符!" class="headerlink" title="4.1 逻辑取反运算符!"></a>4.1 逻辑取反运算符!</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">逻辑反操作</span><br></pre></td></tr></table></figure><p>比如,我们有一个变量叫flag,如果flag为假,要做一个什么事情,就可以这样写代码:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int flag = 0 ;</span><br><span class="line">if(!flag)</span><br><span class="line">{</span><br><span class="line">printf("do something\n");</span><br><span class="line">}</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">10</span><br></pre></td></tr></table></figure><p>如果flag为真,!flag就是假,如果flag为假,!flag就是真<br>所以上面的代码的意思就是flag为假,执行if语句中的代码。</p><h3 id="4-2-逻辑与运算符"><a href="#4-2-逻辑与运算符" class="headerlink" title="4.2 逻辑与运算符"></a>4.2 逻辑与运算符</h3><p>逻辑与<br>&&就是与运算符,也是并且的意思,&&是一个双目操作符,使用的方式是a&&b,&&两边的表达<br>式都是真的时候,整个表达式才为真,只要有一个是假,则整个表达式为假。<br>比如:如果我们说月份是 3 月到 5 月,是春天,那使用代码怎么体现呢?</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">int month = 0 ;</span><br><span class="line">scanf("%d", &month);</span><br><span class="line">if(month >= 3 && month <= 5 )</span><br><span class="line">{</span><br><span class="line">printf("春季\n");</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6</span><br></pre></td></tr></table></figure><p>这里表达的意思就是month既要大于等于 3 ,又要小于等于 5 ,必须同时满足。</p><h3 id="4-3-逻辑或运算符"><a href="#4-3-逻辑或运算符" class="headerlink" title="4.3 逻辑或运算符"></a>4.3 逻辑或运算符</h3><p>逻辑或<br>||就是或运算符,也就是或者的意思,||也是一个双目操作符,使用的方式是a || b,||<br>两边的表达式只要有一个是真,整个表达式就是真,两边的表达式都为假的时候,才为假。<br>比如:我们说一年中月份是 12 月或者 1 月或者 2 月是冬天,那么我们怎么使用代码体现呢?</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">int month = 0 ;</span><br><span class="line">scanf("%d", &month);</span><br><span class="line">if(month == 12 || month== 1 || month == 2 )</span><br><span class="line">{</span><br><span class="line">printf("冬季\n");</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6</span><br></pre></td></tr></table></figure><h3 id="4-4-练习:闰年的判断"><a href="#4-4-练习:闰年的判断" class="headerlink" title="4.4 练习:闰年的判断"></a>4.4 练习:闰年的判断</h3><p>输入一个年份year,判断year是否是闰年<br>闰年判断的规则:</p><ol><li>能被 4 整除并且不能被 100 整除是闰年</li><li>能被 400 整除是闰年</li></ol><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">//代码 1</span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int year = 0 ;</span><br><span class="line">scanf("%d", &year);</span><br><span class="line">if(year% 4 == 0 && year% 100 != 0 )</span><br><span class="line">printf("是闰年\n");</span><br><span class="line">else if(year% 400 == 0 )</span><br><span class="line">printf("是闰年\n");</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">//代码 2</span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int year = 0 ;</span><br><span class="line">scanf("%d", &year);</span><br><span class="line">if((year% 4 == 0 && year% 100 != 0 ) || (year% 400 == 0 ))</span><br><span class="line">printf("是闰年\n");</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td></tr></table></figure><h3 id="4-5-短路"><a href="#4-5-短路" class="headerlink" title="4.5 短路"></a>4.5 短路</h3><h4 id="C语言-逻辑运算符-还有一个特点,它总是先对左侧的表达式求值,再对右边的表达式求值,这个顺序是"><a href="#C语言-逻辑运算符-还有一个特点,它总是先对左侧的表达式求值,再对右边的表达式求值,这个顺序是" class="headerlink" title="C语言 逻辑运算符 还有一个特点,它总是先对左侧的表达式求值,再对右边的表达式求值,这个顺序是"></a>C语言 逻辑运算符 还有一个特点,它总是先对左侧的表达式求值,再对右边的表达式求值,这个顺序是</h4><h4 id="保证的。"><a href="#保证的。" class="headerlink" title="保证的。"></a>保证的。</h4><h4 id="如果左边的表达式满足逻辑运算符的条件,就不再对右边的表达式求值。这种情况称为-“短路”-。"><a href="#如果左边的表达式满足逻辑运算符的条件,就不再对右边的表达式求值。这种情况称为-“短路”-。" class="headerlink" title="如果左边的表达式满足逻辑运算符的条件,就不再对右边的表达式求值。这种情况称为 “短路” 。"></a>如果左边的表达式满足逻辑运算符的条件,就不再对右边的表达式求值。这种情况称为 “短路” 。</h4><h4 id="如前面的代码"><a href="#如前面的代码" class="headerlink" title="如前面的代码:"></a>如前面的代码:</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 if(month >= 3 && month <= 5 )</span><br></pre></td></tr></table></figure><p>表达式中&&的左操作数是month >= 3,右操作数是month <= 5,当左操作数month >= 3的<br>结果是 0 的时候,即使不判断month <= 5,整个表达式的结果也是 0 (不是春季)。<br>所以,对于&&操作符来说,左边操作数的结果是 0 的时候,右边操作数就不再执行。</p><p>对于||操作符是怎么样呢?我们结合前面的代码:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 if(month == 12 || month== 1 || month == 2 )</span><br></pre></td></tr></table></figure><p>如果month==12,则不用再判断month是否等于 1 或者 2 ,整个表达式的结果也是 1 (是冬季)。<br>所以,||操作符的左操作数的结果不为 0 时,就无需执行右操作数。</p><p>像这种仅仅根据左操作数的结果就能知道整个表达式的结果,不再对右操作数进行计算的运算称为 <strong>短<br>路求值</strong> 。</p><p><strong>练习:阅读代码,计算代码输出的结果</strong></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int i = 0 ,a= 0 ,b= 2 ,c = 3 ,d= 4 ;</span><br><span class="line">i = a++ && ++b && d++;</span><br><span class="line">//i = a++||++b||d++;</span><br><span class="line">printf("a = %d\n b = %d\n c = %d\nd = %d\n", a, b, c, d);</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">10</span><br></pre></td></tr></table></figure><h2 id="5-switch语句"><a href="#5-switch语句" class="headerlink" title="5. switch语句"></a>5. switch语句</h2><p>除了if语句外,C语言还提供了switch语句来实现分支结构。<br>switch语句是一种特殊形式的if…else结构,用于判断条件有多个结果的情况。它把多重<br>的else if改成更易用、可读性更好的形式。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">switch (expression) {</span><br><span class="line">case value1: statement</span><br><span class="line">case value2: statement</span><br><span class="line">default: statement</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td></tr></table></figure><p>上面代码中,根据表达式expression不同的值,执行相应的case分支。如果找不到对应的值,<br>就执行default分支。<br><strong>注:</strong></p><ul><li>switch后的expression必须是整型表达式</li><li>case后的值,必须是整形常量表达式</li></ul><h3 id="5-1-if语句和switch语句的对比"><a href="#5-1-if语句和switch语句的对比" class="headerlink" title="5.1 if语句和switch语句的对比"></a>5.1 if语句和switch语句的对比</h3><h4 id="练习:-输入任意一个整数值,计算除-3-之后的余数"><a href="#练习:-输入任意一个整数值,计算除-3-之后的余数" class="headerlink" title="练习: 输入任意一个整数值,计算除 3 之后的余数"></a>练习: 输入任意一个整数值,计算除 3 之后的余数</h4><p>如果使用if语句完成,如下:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int n = 0 ;</span><br><span class="line">scanf("%d", &n);</span><br><span class="line">if(n% 3 == 0 )</span><br><span class="line">printf("整除,余数为0\n");</span><br><span class="line">else if(n% 3 == 1 )</span><br><span class="line">printf("余数是1\n");</span><br><span class="line">else</span><br><span class="line">printf("余数是2\n");</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td></tr></table></figure><p>如果使用switch语句改写,就可以是这样的:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int n = 0 ;</span><br><span class="line">scanf("%d", &n);</span><br><span class="line">switch(n% 3 )</span><br><span class="line">{</span><br><span class="line">case 0 :</span><br><span class="line">printf("整除,余数为0\n");</span><br><span class="line">break;</span><br><span class="line">case 1 :</span><br><span class="line">printf("余数是1\n");</span><br><span class="line">break;</span><br><span class="line">case 2 :</span><br><span class="line">printf("余数是2\n");</span><br><span class="line">break;</span><br><span class="line">}</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td></tr></table></figure><p>上述的代码中,我们要注意的点有:</p><ol><li>case和后边的数字之间必须有空格</li><li>每一个case语句中的代码执行完成后,需要加上break,才能跳出这个switch语句。</li></ol><h3 id="5-2-switch语句中的break"><a href="#5-2-switch语句中的break" class="headerlink" title="5.2 switch语句中的break"></a>5.2 switch语句中的break</h3><p>前面的代码中,如果我们去掉case语句中的break,会出现什么情况呢?</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int n = 0 ;</span><br><span class="line">scanf("%d", &n);</span><br><span class="line">switch(n% 3 )</span><br><span class="line">{</span><br><span class="line">case 0 :</span><br><span class="line">printf("整除,余数为0\n");</span><br><span class="line">case 1 :</span><br><span class="line">printf("余数是1\n");</span><br><span class="line">case 2 :</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">printf("余数是2\n");</span><br><span class="line">}</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td></tr></table></figure><p>测试一组,看一下结果:</p><h4 id="我们发现,-7-除以-3-本来余数是-1-,但是我们发现程序运行的结果多了一行“余数是-2-”的打印。"><a href="#我们发现,-7-除以-3-本来余数是-1-,但是我们发现程序运行的结果多了一行“余数是-2-”的打印。" class="headerlink" title="我们发现, 7 除以 3 本来余数是 1 ,但是我们发现程序运行的结果多了一行“余数是 2 ”的打印。"></a>我们发现, 7 除以 3 本来余数是 1 ,但是我们发现程序运行的结果多了一行“余数是 2 ”的打印。</h4><h4 id="这是为什么呢?"><a href="#这是为什么呢?" class="headerlink" title="这是为什么呢?"></a>这是为什么呢?</h4><p>原因是switch语句也是分支效果的,只有在switch语句中使用break才能在跳出switch语<br>句,如果某一个case语句的后边没有break语句,代码会继续往下执行,有可能执行其他case<br>语句中的代码,直到遇到break语句或者switch语句结束。就比如上面的代码就执行了case<br>2 中的语句。<br>所以在switch语句中break语句是非常重要的,能实现真正的分支效果。<br>当然,break也不是每个case语句都得有,这就得根据实际情况来看了。</p><p><strong>练习:</strong><br>输入一个1~7的数字,打印对应的星期几<br>例如:<br>输入:1输出:星期一<br>输入:2输出:星期二<br>输入:3输出:星期三<br>输入:4输出:星期四<br>输入:5输出:星期五<br>输入:6输出:星期六<br>输入:7输出:星期天</p><p>参考代码如下:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int day = 0 ;</span><br><span class="line">scanf("%d", &day);</span><br><span class="line">switch(day)</span><br><span class="line">{</span><br><span class="line">case 1 :</span><br><span class="line">printf("星期一\n");</span><br><span class="line">break;</span><br><span class="line">case 2 :</span><br><span class="line">printf("星期二\n");</span><br><span class="line">break;</span><br><span class="line">case 3 :</span><br><span class="line">printf("星期三\n");</span><br><span class="line">break;</span><br><span class="line">case 4 :</span><br><span class="line">printf("星期四\n");</span><br><span class="line">break;</span><br><span class="line">case 5 :</span><br><span class="line">printf("星期五\n");</span><br><span class="line">break;</span><br><span class="line">case 6 :</span><br><span class="line">printf("星期六\n");</span><br><span class="line">break;</span><br><span class="line">case 7 :</span><br><span class="line">printf("星期天\n");</span><br><span class="line">break;</span><br><span class="line">}</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td></tr></table></figure><p>如果需求发生变化,变为:</p><ol><li>输入1~5,输出的是“工作日”;</li><li>输入6~7,输出“休息日”<br>那参考代码如下:</li></ol><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int day = 0 ;</span><br><span class="line">scanf("%d", &day);</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">switch(day)</span><br><span class="line">{</span><br><span class="line">case 1 :</span><br><span class="line">case 2 :</span><br><span class="line">case 3 :</span><br><span class="line">case 4 :</span><br><span class="line">case 5 :</span><br><span class="line">printf("工作日\n");</span><br><span class="line">break;</span><br><span class="line">case 6 :</span><br><span class="line">case 7 :</span><br><span class="line">printf("休息日\n");</span><br><span class="line">break;</span><br><span class="line">}</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td></tr></table></figure><p>上面的练习中,我们发现应该根据实际的情况,来在代码中觉得是否使用break,或者在哪里使用<br>break,才能正确完成实际的需求。</p><h3 id="5-3-switch语句中的default"><a href="#5-3-switch语句中的default" class="headerlink" title="5.3 switch语句中的default"></a>5.3 switch语句中的default</h3><p>在使用switch语句的时候,我们经常可能遇到一种情况,比如switch后的表达式中的值无法匹<br>配代码中的case语句的时候,这时候要不就不做处理,要不就得在switch语句中加入<br>default子句。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">switch (expression) {</span><br><span class="line">case value1: statement</span><br><span class="line">case value2: statement</span><br><span class="line">default: statement</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td></tr></table></figure><p>switch后边的expression的结果不是value1,也不是value2的时候,就会执行<br>default子句。<br>就比如前面做的打印星期的练习,如果day的输入不是1~7的值,如果我们要提示:输入错误,则可<br>以这样完成代码:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">int day = 0 ;</span><br><span class="line">scanf("%d", &day);</span><br><span class="line">switch(day)</span><br><span class="line">{</span><br><span class="line">case 1 :</span><br><span class="line">case 2 :</span><br><span class="line">case 3 :</span><br><span class="line">case 4 :</span><br><span class="line">case 5 :</span><br><span class="line">printf("工作日\n");</span><br><span class="line">break;</span><br><span class="line">case 6 :</span><br><span class="line">case 7 :</span><br><span class="line">printf("休息日\n");</span><br><span class="line">break;</span><br><span class="line">default:</span><br><span class="line">printf("输入错误\n");</span><br><span class="line">break;</span><br><span class="line">}</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td></tr></table></figure><h3 id="5-4-switch语句中的case和default的顺序问题"><a href="#5-4-switch语句中的case和default的顺序问题" class="headerlink" title="5.4 switch语句中的case和default的顺序问题"></a>5.4 switch语句中的case和default的顺序问题</h3><p>在switch语句中case子句和default子句有要求顺序吗?default只能放在最后吗?<br>其实,在switch语句中case语句和default语句是没有顺序要求的,只要你的顺序是满足实<br>际需求的就可以。<br>不过我们通常是把default子句放在最后处理的。</p><h2 id="6-while循环"><a href="#6-while循环" class="headerlink" title="6. while循环"></a>6. while循环</h2><p>C语言提供了 3 种循环语句,while就是其中一种,接下来就介绍一下while语句。<br>while语句的语法结构和if语句非常相似。</p><h3 id="6-1-if和while的对比"><a href="#6-1-if和while的对比" class="headerlink" title="6.1 if和while的对比"></a>6.1 if和while的对比</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">if(表达式)</span><br><span class="line">语句;</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">while(表达式)</span><br><span class="line">语句;//如果循环体想包含更多的语句,可以加上大括号</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">5</span><br><span class="line">6</span><br></pre></td></tr></table></figure><p>你可以对比来看一下,具体写个代码吧</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">//代码 1</span><br><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">if( 1 )</span><br><span class="line">printf("hehe\n"); //if后边条件满足,打印一次hehe</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">//代码 2</span><br><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">while( 1 )</span><br><span class="line">printf("hehe\n"); //while后边的条件满足,死循环的打印hehe</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><p>这就是他们的区别,while语句是可以实现循环效果的。</p><h3 id="6-2-while语句的执行流程"><a href="#6-2-while语句的执行流程" class="headerlink" title="6.2 while语句的执行流程"></a>6.2 while语句的执行流程</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">语句</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">值为 0</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">结束</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">break continue</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">判断表达式</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">while语句的执行流程图</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">值不为 0</span><br></pre></td></tr></table></figure><h4 id="首先上来就是执行判断表达式,表达式的值为-0-,循环直接结束;表达式的值不为-0-,则执行循环语"><a href="#首先上来就是执行判断表达式,表达式的值为-0-,循环直接结束;表达式的值不为-0-,则执行循环语" class="headerlink" title="首先上来就是执行判断表达式,表达式的值为 0 ,循环直接结束;表达式的值不为 0 ,则执行循环语"></a>首先上来就是执行判断表达式,表达式的值为 0 ,循环直接结束;表达式的值不为 0 ,则执行循环语</h4><h4 id="句,语句执行完后再继续判断,是否进行下一次判断。"><a href="#句,语句执行完后再继续判断,是否进行下一次判断。" class="headerlink" title="句,语句执行完后再继续判断,是否进行下一次判断。"></a>句,语句执行完后再继续判断,是否进行下一次判断。</h4><h3 id="6-3-while循环的实践"><a href="#6-3-while循环的实践" class="headerlink" title="6.3 while循环的实践"></a>6.3 while循环的实践</h3><h4 id="练习:在屏幕上打印1-10的值"><a href="#练习:在屏幕上打印1-10的值" class="headerlink" title="练习:在屏幕上打印1~10的值"></a>练习:在屏幕上打印1~10的值</h4><h4 id="参考代码:-1"><a href="#参考代码:-1" class="headerlink" title="参考代码:"></a>参考代码:</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int i = 1 ;</span><br><span class="line">while(i<= 10 )</span><br><span class="line">{</span><br><span class="line">printf("%d ", i);</span><br><span class="line">i = i+ 1 ;</span><br><span class="line">}</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">11</span><br></pre></td></tr></table></figure><h3 id="6-4-练习"><a href="#6-4-练习" class="headerlink" title="6.4 练习"></a>6.4 练习</h3><h4 id="输入一个正的整数,逆序打印这个整数的每一位"><a href="#输入一个正的整数,逆序打印这个整数的每一位" class="headerlink" title="输入一个正的整数,逆序打印这个整数的每一位"></a>输入一个正的整数,逆序打印这个整数的每一位</h4><h4 id="例如:"><a href="#例如:" class="headerlink" title="例如:"></a>例如:</h4><h4 id="输入:-1234-,输出:4321"><a href="#输入:-1234-,输出:4321" class="headerlink" title="输入: 1234 ,输出:4321"></a>输入: 1234 ,输出:4321</h4><h4 id="输入:-521-,输出:125"><a href="#输入:-521-,输出:125" class="headerlink" title="输入: 521 ,输出:125"></a>输入: 521 ,输出:125</h4><h4 id="题目解析"><a href="#题目解析" class="headerlink" title="题目解析"></a>题目解析</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">1. 要想得到n的最低位,可以使用n%10的运算,得到的余数就是最低位,如: 1 234%10得到 4 </span><br><span class="line">2. 要想去掉n的最低位,找出倒数第二位,则使用n=n/10操作就可以去掉最低位的,如:</span><br><span class="line">n=1234/10得到 123 , 123 相较于 1234 就去掉了最低位,123%10就得到倒数第二位 3 。</span><br><span class="line">3. 循环 1 和 2 两个步骤,在n变成 0 之前,就能到所有的位。</span><br></pre></td></tr></table></figure><p>参考代码:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int n = 0 ;</span><br><span class="line">scanf("%d", &n);</span><br><span class="line">while(n)</span><br><span class="line">{</span><br><span class="line">printf("%d ", n% 10 );</span><br><span class="line">n /= 10 ;</span><br><span class="line">}</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td></tr></table></figure><h2 id="7-for循环"><a href="#7-for循环" class="headerlink" title="7. for循环"></a>7. for循环</h2><h3 id="7-1-语法形式"><a href="#7-1-语法形式" class="headerlink" title="7.1 语法形式"></a>7.1 语法形式</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">for循环是三种循环中使用最多的,for循环的语法形式如下:</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">for(表达式 1 ; 表达式 2 ; 表达式 3 )</span><br><span class="line">语句;//如果循环体想包含更多的语句,可以加上大括号</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td></tr></table></figure><p><strong>表达式1</strong> 用于循环变量的初始化<br><strong>表达式 2</strong> 用于循环结束条件的判断<br><strong>表达式 3</strong> 用于循环变量的调整</p><h3 id="7-2-for循环的执行流程"><a href="#7-2-for循环的执行流程" class="headerlink" title="7.2 for循环的执行流程"></a>7.2 for循环的执行流程</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">表达式 1 :初始化</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">表达式 2 :判断</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">循环语句</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">表达式 3 :调整</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">结束</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">!=0</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">==0</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">break continue</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">for循环的执行流程</span><br></pre></td></tr></table></figure><p>首先执行表达式 1 初始化循环变量,接下来就是执行表达式 2 的判断部分,表达式 2 的结果如果<br>==0,则循环结束;表达式 2 的结果如果!=0则执行循环语句,循环语句执行完后,再去执行表达式<br>3 ,调整循环变量,然后再去表达式 2 的地方执行判断,表达式 2 的结果是否为 0 ,决定循环是否继<br>续。<br>整个循环的过程中,表达式 1 初始化部分只被执行 1 次,剩下的就是表达式 2 、循环语句、表达式 3 在循<br>环。</p><h3 id="7-3-for循环的实践"><a href="#7-3-for循环的实践" class="headerlink" title="7.3 for循环的实践"></a>7.3 for循环的实践</h3><h4 id="练习:在屏幕上打印1-10的值-1"><a href="#练习:在屏幕上打印1-10的值-1" class="headerlink" title="练习:在屏幕上打印1~10的值"></a>练习:在屏幕上打印1~10的值</h4><h4 id="参考代码:-2"><a href="#参考代码:-2" class="headerlink" title="参考代码:"></a>参考代码:</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int i = 0 ;</span><br><span class="line">for(i= 1 ; i<= 10 ; i++)</span><br><span class="line">{</span><br><span class="line">printf("%d ", i);</span><br><span class="line">}</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td></tr></table></figure><p>运行结果:</p><h3 id="7-4-while循环和for循环的对比"><a href="#7-4-while循环和for循环的对比" class="headerlink" title="7.4 while循环和for循环的对比"></a>7.4 while循环和for循环的对比</h3><p>for和while在实现循环的过程中都有初始化、判断、调整这三个部分,但是for循环的三个部<br>分非常集中,便于代码的维护,而如果代码较多的时候while循环的三个部分就比较分散,所以从形<br>式上for循环要更优一些。</p><h3 id="7-5-练习"><a href="#7-5-练习" class="headerlink" title="7.5 练习"></a>7.5 练习</h3><h4 id="练习-1-:"><a href="#练习-1-:" class="headerlink" title="练习 1 :"></a>练习 1 :</h4><h4 id="计算1-100之间-3-的倍数的数字之和"><a href="#计算1-100之间-3-的倍数的数字之和" class="headerlink" title="计算1~100之间 3 的倍数的数字之和"></a>计算1~100之间 3 的倍数的数字之和</h4><h4 id="参考代码:-3"><a href="#参考代码:-3" class="headerlink" title="参考代码:"></a>参考代码:</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int i = 0 ;</span><br><span class="line">int sum = 0 ;</span><br><span class="line">for(i= 1 ; i<= 100 ; i++)</span><br><span class="line">{</span><br><span class="line">if(i % 3 == 0 )</span><br><span class="line">sum += i;</span><br><span class="line">}</span><br><span class="line">printf("%d\n", sum);</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">//小小的优化</span><br><span class="line">//如果能直接产生 3 的倍数的数字就省去了多余的循环和判断</span><br><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int i = 0 ;</span><br><span class="line">int sum = 0 ;</span><br><span class="line">for(i= 3 ; i<= 100 ; i+= 3 )</span><br><span class="line">{</span><br><span class="line">sum += i;</span><br><span class="line">}</span><br><span class="line">printf("%d\n", sum);</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td></tr></table></figure><h2 id="8-do-while循环"><a href="#8-do-while循环" class="headerlink" title="8. do-while循环"></a>8. do-while循环</h2><h3 id="8-1-语法形式"><a href="#8-1-语法形式" class="headerlink" title="8.1 语法形式"></a>8.1 语法形式</h3><p>在循环语句中do while语句的使用最少,它的语法如下:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">do</span><br><span class="line">语句;</span><br><span class="line">while(表达式);</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td></tr></table></figure><p>while和for这两种循环都是先判断,条件如果满足就进入循环,执行循环语句,如果不满足就跳<br>出循环;<br>而do while循环则是先直接进入循环体,执行循环语句,然后再执行while后的判断表达式,表<br>达式为真,就会进行下一次,表达式为假,则不再继续循环。</p><h3 id="8-2-dowhile循环的执行流程"><a href="#8-2-dowhile循环的执行流程" class="headerlink" title="8.2 dowhile循环的执行流程"></a>8.2 dowhile循环的执行流程</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">语句</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">判断表达式</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">break continue</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">!=0</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">==0</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">dowhile循环的执行流程</span><br></pre></td></tr></table></figure><p>在do while循环中先执行图上的“语句”,执行完语句,在去执行“判断表达式”,判断表达式的<br>结果是!=0,则继续循环,执行循环语句;判断表达式的结果==0,则循环结束。<br>所以在do while语句中循环体是至少执行一次的,这是do while循环比较特殊的地方。</p><h3 id="8-3-dowhile循环的实例"><a href="#8-3-dowhile循环的实例" class="headerlink" title="8.3 dowhile循环的实例"></a>8.3 dowhile循环的实例</h3><h4 id="在屏幕上打印1-10的值"><a href="#在屏幕上打印1-10的值" class="headerlink" title="在屏幕上打印1~10的值"></a>在屏幕上打印1~10的值</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int i = 1 ;</span><br><span class="line">do</span><br><span class="line">{</span><br><span class="line">printf("%d ", i);</span><br><span class="line">i = i + 1 ;</span><br><span class="line">}while(i<= 10 );</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td></tr></table></figure><p>一般do while使用在循环体至少被执行一次的场景下,所以较少一些。</p><h3 id="8-4-练习"><a href="#8-4-练习" class="headerlink" title="8.4 练习"></a>8.4 练习</h3><h4 id="输入一个正整数,计算这个整数是几位数?"><a href="#输入一个正整数,计算这个整数是几位数?" class="headerlink" title="输入一个正整数,计算这个整数是几位数?"></a>输入一个正整数,计算这个整数是几位数?</h4><h4 id="例如:-1"><a href="#例如:-1" class="headerlink" title="例如:"></a>例如:</h4><h4 id="输入:1234输出:-4"><a href="#输入:1234输出:-4" class="headerlink" title="输入:1234输出: 4 "></a>输入:1234输出: 4 </h4><h4 id="输入:12输出:-2"><a href="#输入:12输出:-2" class="headerlink" title="输入:12输出: 2 "></a>输入:12输出: 2 </h4><h4 id="参考代码:-4"><a href="#参考代码:-4" class="headerlink" title="参考代码:"></a>参考代码:</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int n = 0 ;</span><br><span class="line">scanf("%d", &n);</span><br><span class="line">int cnt = 0 ;</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">do</span><br><span class="line">{</span><br><span class="line">cnt++;</span><br><span class="line">n = n / 10 ;</span><br><span class="line">} while (n);</span><br><span class="line">printf("%d\n", cnt);</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td></tr></table></figure><p>这里并非必须使用do while语句,但是这个代码就比较适合使用do while循环,因为n即使是<br>0 ,也是 1 位数,要统计位数的。</p><h2 id="9-break和continue语句"><a href="#9-break和continue语句" class="headerlink" title="9. break和continue语句"></a>9. break和continue语句</h2><h4 id="在循环执行的过程中,如果某些状况发生的时候,需要提前终止循环,这是非常常⻅的现象。C语言中"><a href="#在循环执行的过程中,如果某些状况发生的时候,需要提前终止循环,这是非常常⻅的现象。C语言中" class="headerlink" title="在循环执行的过程中,如果某些状况发生的时候,需要提前终止循环,这是非常常⻅的现象。C语言中"></a>在循环执行的过程中,如果某些状况发生的时候,需要提前终止循环,这是非常常⻅的现象。C语言中</h4><p>提供了break和continue两个关键字,就是应用到循环中的。</p><ul><li>break的作用是用于永久的终止循环,只要break被执行,直接就会跳出循环,继续往后执<br> 行。</li><li>continue的作用是跳过本次循环中continue后边的代码,在for循环和while循环中有<br> 所差异的。</li></ul><h3 id="9-1-while循环中的break和continue"><a href="#9-1-while循环中的break和continue" class="headerlink" title="9.1 while循环中的break和continue"></a>9.1 while循环中的break和continue</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">9.1.1 break举例</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int i = 1 ;</span><br><span class="line">while(i<= 10 )</span><br><span class="line">{</span><br><span class="line">if(i == 5 )</span><br><span class="line">break;//当i等于 5 后,就执行break,循环就终止了</span><br><span class="line">printf("%d ", i);</span><br><span class="line">i = i+ 1 ;</span><br><span class="line">}</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td></tr></table></figure><p>执行的结果:</p><p>打印了1,2,3,4后,当i等于 5 的时候,循环在break的地方终止,不再打印,不再循环。<br>所以break的作用就是永久的终止循环,只要break被执行,break外的第一层循环就终止了。<br>那以后我们在循环中,想在某种条件下终止循环,则可以使用break来完成我们想要的效果。</p><p>9.1.2 continue举例<br>continue是继续的意思,在循环中的作用就是跳过本次循环中continue后边的代码,继续进行<br>下一次循环的判断。<br>上面的代码,如果把break换成continue会是什么结果呢?</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int i = 1 ;</span><br><span class="line">while(i<= 10 )</span><br><span class="line">{</span><br><span class="line">if(i == 5 )</span><br><span class="line">continue;</span><br><span class="line">//当i等于 5 后,就执行continue,直接跳过continue的代码,去循环的判断的地方</span><br><span class="line">//因为这里跳过了i = i+1,所以i一直为 5 ,程序陷入和死循环</span><br><span class="line">printf("%d ", i);</span><br><span class="line">i = i+ 1 ;</span><br><span class="line">}</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td></tr></table></figure><p>到这里我们就能分析出来,continue可以帮助我们跳过某一次循环continue后边的代码,直接<br>到循环的判断部分,进行下一次循环的判断,如果循环的调整是在continue后边的话,可能会造成<br>死循环。</p><h3 id="9-2-for循环中的break和continue"><a href="#9-2-for循环中的break和continue" class="headerlink" title="9.2 for循环中的break和continue"></a>9.2 for循环中的break和continue</h3><p>9.2.1 break举例<br>其实和while循环中的break一样,for循环中的break也是用于终止循环的,不管循环还需<br>要循环多少次,只要执行到了break,循环就彻底终止,我们上代码。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int i = 1 ;</span><br><span class="line">for(i= 1 ; i<= 10 ; i++)</span><br><span class="line">{</span><br><span class="line">if(i == 5 )</span><br><span class="line">break;</span><br><span class="line">printf("%d ", i);</span><br><span class="line">}</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td></tr></table></figure><p>运行结果:</p><p>break的作用是永久的终止循环,未来我们在某个条件发生的时候,不想再继续循环的时候,就可以<br>使用break来完成。</p><p>9.2.2 continue举例<br>上面的代码,如果把break换成continue会是什么结果呢?</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int i = 1 ;</span><br><span class="line">for(i= 1 ; i<= 10 ; i++)</span><br><span class="line">{</span><br><span class="line">if(i == 5 )</span><br><span class="line">continue;//这里continue跳过了后边的打印,来到了i++的调整部分</span><br><span class="line">printf("%d ", i);</span><br><span class="line">}</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td></tr></table></figure><p>所以在for循环中continue的作用是跳过本次循环中continue后的代码,直接去到循环的调<br>整部分。未来当某个条件发生的时候,本次循环无需再执行后续某些操作的时候,就可以使用<br>continue来实现。</p><p>在这里我们也可以对比一下while循环和for循环中continue的区别:</p><h3 id="9-3-dowhile循环中的break和continue"><a href="#9-3-dowhile循环中的break和continue" class="headerlink" title="9.3 dowhile循环中的break和continue"></a>9.3 dowhile循环中的break和continue</h3><p>do.while语句中的break和continue的作用和while循环中几乎一模一样,大家下来可以<br>自行测试并体会。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int i = 1 ;</span><br><span class="line">do</span><br><span class="line">{</span><br><span class="line">if(i == 5 )</span><br><span class="line">break;</span><br><span class="line">printf("%d ", i);</span><br><span class="line">i = i + 1 ;</span><br><span class="line">}while(i<= 10 );</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int i = 1 ;</span><br><span class="line">do</span><br><span class="line">{</span><br><span class="line">if(i == 5 )</span><br><span class="line">continue;</span><br><span class="line">printf("%d ", i);</span><br><span class="line">i = i + 1 ;</span><br><span class="line">}while(i<= 10 );</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">14</span><br><span class="line">15</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">14</span><br><span class="line">15</span><br></pre></td></tr></table></figure><h2 id="10-循环的嵌套"><a href="#10-循环的嵌套" class="headerlink" title="10. 循环的嵌套"></a>10. 循环的嵌套</h2><p>前面学习了三种循环while,do while,for,这三种循环往往会嵌套在一起才能更好的解决<br>问题,就是我们所说的:循环嵌套,这里我们就看一个例子。</p><h3 id="10-1-练习"><a href="#10-1-练习" class="headerlink" title="10.1 练习"></a>10.1 练习</h3><h4 id="找出100-200之间的素数,并打印在屏幕上。"><a href="#找出100-200之间的素数,并打印在屏幕上。" class="headerlink" title="找出100~200之间的素数,并打印在屏幕上。"></a>找出100~200之间的素数,并打印在屏幕上。</h4><h4 id="注:素数又称质数,只能被-1-和本身整除的数字。"><a href="#注:素数又称质数,只能被-1-和本身整除的数字。" class="headerlink" title="注:素数又称质数,只能被 1 和本身整除的数字。"></a>注:素数又称质数,只能被 1 和本身整除的数字。</h4><h3 id="10-2-题目解析:"><a href="#10-2-题目解析:" class="headerlink" title="10.2 题目解析:"></a>10.2 题目解析:</h3><h4 id="1-要从100200之间找出素数,首先得有100200之间的数,这里可以使用循环解决。"><a href="#1-要从100200之间找出素数,首先得有100200之间的数,这里可以使用循环解决。" class="headerlink" title="1. 要从100200之间找出素数,首先得有100200之间的数,这里可以使用循环解决。"></a>1. 要从100<del>200之间找出素数,首先得有100</del>200之间的数,这里可以使用循环解决。</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">2. 假设要判断i是否为素数,需要拿2~i-1之间的数字去试除i,需要产生2~i-1之间的数字,也可以使用</span><br><span class="line">循环解决。</span><br><span class="line">3. 如果2~i-1之间有数字能整除i,则i不是素数,如果都不能整除,则i是素数。</span><br></pre></td></tr></table></figure><h3 id="10-3-参考代码:"><a href="#10-3-参考代码:" class="headerlink" title="10.3 参考代码:"></a>10.3 参考代码:</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">int i = 0 ;</span><br><span class="line">//循环产生100~200的数字</span><br><span class="line">for(i= 100 ; i<= 200 ; i++)</span><br><span class="line">{</span><br><span class="line">//判断i是否为素数</span><br><span class="line">//循环产生2~i-1之间的数字</span><br><span class="line">int j = 0 ;</span><br><span class="line">int flag = 1 ;//假设i是素数</span><br><span class="line">for(j= 2 ; j<i; j++)</span><br><span class="line">{</span><br><span class="line">if(i % j == 0 )</span><br><span class="line">{</span><br><span class="line">flag = 0 ;</span><br><span class="line">break;</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">}</span><br><span class="line">}</span><br><span class="line">if(flag == 1 )</span><br><span class="line">printf("%d ", i);</span><br><span class="line">}</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td></tr></table></figure><h2 id="11-goto语句"><a href="#11-goto语句" class="headerlink" title="11. goto语句"></a>11. goto语句</h2><p>C语言提供了一种非常特别的语法,就是goto语句和跳转标号,goto语句可以实现在 <strong>同一个函数<br>内</strong> 跳转到设置好的标号处。<br>例如:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">#include <stdio.h></span><br><span class="line">int main()</span><br><span class="line">{</span><br><span class="line">printf("hehe\n");</span><br><span class="line">goto next;</span><br><span class="line">printf("haha\n");</span><br><span class="line">next:</span><br><span class="line">printf("跳过了haha的打印\n");</span><br><span class="line">return 0 ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">11</span><br></pre></td></tr></table></figure><p>goto语句如果使用的不当,就会导致在函数内部随意乱跳转,打乱程序的执行流程,所以我们的建<br>议是能不用尽量不去使用;但是goto语句也不是一无是处,在多层循环的代码中,如果想快速跳出<br>使用goto就非常的方便了。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">for(...)</span><br><span class="line">{</span><br><span class="line">for(...)</span><br><span class="line">{</span><br><span class="line">for(...)</span><br><span class="line">{</span><br><span class="line">if(disaster)</span><br><span class="line">goto error;</span><br><span class="line">}</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7 8 9</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">10</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">}</span><br><span class="line">error:</span><br><span class="line">//...</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td></tr></table></figure><p>本来for循环想提前退出得使用break,一个break只能跳出一层for循环,如果 3 层循环嵌套<br>就得使用 3 个break才能跳出循环,所以在这种情况下我们使用goto语句就会更加的快捷。</p><h4 id="完"><a href="#完" class="headerlink" title="完"></a>完</h4>]]></content>
<categories>
<category> C语言程序设计 </category>
</categories>
<tags>
<tag> C语言程序设计知识点总结 </tag>
</tags>
</entry>
<entry>
<title>第13讲:深⼊理解指针-字符,数组,函数-指针变量</title>
<link href="/posts/49708/"/>
<url>/posts/49708/</url>
<content type="html"><![CDATA[<h1 id="目录"><a href="#目录" class="headerlink" title="目录"></a>目录</h1><ol><li><p>字符指针变量</p></li><li><p>数组指针变量</p></li><li><p>二维数组传参的本质</p></li><li><p>函数指针变量</p></li><li><p>函数指针数组</p></li><li><p>转移表</p></li></ol><blockquote><p><strong>正文开始</strong></p></blockquote><h1 id="字符指针变量"><a href="#字符指针变量" class="headerlink" title="字符指针变量"></a>字符指针变量</h1><blockquote><p>在指针的类型中我们知道有一种指针类型为字符指针<code>char*</code>;</p></blockquote><p><code>一般使用:</code></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">char</span> ch = <span class="string">'w'</span>;</span><br><span class="line"> <span class="type">char</span>* pc = &ch;</span><br><span class="line"> *pc = <span class="string">'b'</span>; <span class="comment">//修改指针指向的变量的值,屏幕打印出'b'</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%c\n"</span>, *pc); <span class="comment">//屏幕打印出'b'</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>还有一种使用方式如下:</code></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">const</span> <span class="type">char</span>* p = <span class="string">"abcdef"</span>; <span class="comment">//声明一个字符指针变量p指向字符串"abcdef"的首元素,是一个常量字符串,不能被修改</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%c\n"</span>, *p); <span class="comment">//屏幕打印出'a'</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%s\n"</span>, p); <span class="comment">//使用%s打印字符串的时候,只是提供首字符的地址就行,所以屏幕打印出'abcdef'</span></span><br><span class="line"> <span class="comment">//char arr[] = "abcdef"; //进行初始化数组</span></span><br><span class="line"> <span class="comment">//char* p = arr; //声明一个字符指针变量p指向数组的首元素</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%s\n"</span>, arr);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>画图理解:</code></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/49708/image.png" alt="alt text"></p><p><code>代码 const char* p = "abcdef"; 特别容易让同学以为是把字符串</code><strong>abcdef</strong><code>放到字符指针</code><strong>p</strong><code>⾥了,但是本质是把字符串</code><strong>abcdef</strong><code>⾸字符的地址放到了</code><strong>p</strong><code>中。</code></p><blockquote><p>《剑指offer》中收录了一道和字符串相关的笔试题,我们一起来学习一下:</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">char</span> str1[] = <span class="string">"hello bit."</span>;</span><br><span class="line"> <span class="type">char</span> str2[] = <span class="string">"hello bit."</span>;</span><br><span class="line"></span><br><span class="line"> <span class="type">const</span> <span class="type">char</span>* str3 = <span class="string">"hello bit."</span>;</span><br><span class="line"> <span class="type">const</span> <span class="type">char</span>* str4 = <span class="string">"hello bit."</span>;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> (str1 == str2) <span class="comment">//数组名是数组首元素的地址</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"str1 and str2 are same\n"</span>);<span class="comment">//1</span></span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"str1 and str2 are not same\n"</span>);<span class="comment">//2-打印</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> (str3 == str4)</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"str3 and str4 are same\n"</span>);<span class="comment">//3-打印</span></span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"str3 and str4 are not same\n"</span>);<span class="comment">//4</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>输出结果:</code></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/49708/image-1.png" alt="alt text"></p><p><code>画图理解:</code></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/49708/image-2.png" alt="alt text"></p><p><code>这里str 3 和str 4 指向的是一个同一个常量字符串。C/C++会把常量字符串存储到单独的一个内存区域,当几个指针指向同一个字符串的时候,他们实际会指向同一块内存。但是用相同的常量字符串去初始化不同的数组的时候就会开辟出不同的内存块。所以str 1 和str 2 不同,str 3 和str 4 相同。</code></p><h1 id="数组指针变量"><a href="#数组指针变量" class="headerlink" title="数组指针变量"></a>数组指针变量</h1><h2 id="数组指针变量是什么?"><a href="#数组指针变量是什么?" class="headerlink" title="数组指针变量是什么?"></a>数组指针变量是什么?</h2><p><code>指针数组是一种数组,数组中存放的是地址(指针)。</code></p><blockquote><p><code>数组指针变量是指针变量?还是数组?</code></p></blockquote><p>答案是:**<code>指针变量</code>**。</p><p><strong><code>类比:</code></strong></p><p><code>字符指针 -char* -指向字符的指针 - 字符指针变量中存放字符变量的地址。</code></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">char</span> ch = <span class="string">'w'</span>;</span><br><span class="line"><span class="type">char</span>* p = &ch;</span><br></pre></td></tr></table></figure><p><code>整型指针 -int* -指向整型的指针 - 整型指针变量中存放整型变量的地址。</code></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a = <span class="number">10</span>;</span><br><span class="line"><span class="type">int</span>* p = &a;</span><br></pre></td></tr></table></figure><p><code>数组指针 -char* -指向数组的指针 - 数组指针变量中存放</code><strong>数组的地址</strong>`。<br> <strong>&数组名</strong></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 包含标准输入输出库</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 主函数</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 定义并初始化大小为10的字符数组</span></span><br><span class="line"> <span class="type">char</span> arr[<span class="number">10</span>] = {<span class="number">1</span> , <span class="number">2</span> , <span class="number">3</span> , <span class="number">4</span> , <span class="number">5</span>};</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 定义指向数组的指针p,p中存放的是数组arr的地址</span></span><br><span class="line"> <span class="type">char</span> (*p)[<span class="number">10</span>] = &arr; <span class="comment">//p就是是数组指针,p中存放的是数组的地址</span></span><br><span class="line"> <span class="comment">//int(*)[10] = int(*)[10]</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">//arr --int* arr+1跳过4个字节</span></span><br><span class="line"> <span class="comment">//&arr[0] --int(*)[10] &arr[0]+1跳过4个字节</span></span><br><span class="line"> <span class="comment">//&arr --int(*)[10] &arr+1跳过40个字节</span></span><br><span class="line"> <span class="comment">// 返回0</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>运行结果:</code></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span> <span class="number">2</span> <span class="number">3</span> <span class="number">4</span> <span class="number">5</span></span><br></pre></td></tr></table></figure><p><code>我们已经熟悉:</code></p><ul><li><p><strong>整形指针变量</strong> :<code>int * pint</code>;存放<code>整形变量的地址</code>,能够<code>指向整形数据的指针</code>。</p></li><li><p><strong>浮点型指针变量</strong> :<code>float * pf</code>;存放<code>浮点型变量的地址</code>,能够<code>指向浮点型数据的指针</code>。</p></li><li><p><strong>数组指针变量</strong>:<code>存放的应该是数组的地址,能够指向数组的指针变量</code>。</p></li></ul><p><code>练习:</code></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">char</span>* ch[<span class="number">5</span>] = {<span class="number">0</span>};</span><br><span class="line"> </span><br><span class="line"> <span class="type">char</span>* (*p)[<span class="number">5</span>] = &ch;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>运行结果:</code></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">00000000</span></span><br></pre></td></tr></table></figure><blockquote><p><strong>下面代码哪个是数组指针变量</strong>?</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> *p1[<span class="number">10</span>];</span><br><span class="line"><span class="type">int</span> (*p2)[<span class="number">10</span>];</span><br></pre></td></tr></table></figure><blockquote><p><strong>思考一下:p 1 ,p 2 分别是什么</strong>?</p></blockquote><p><strong>数组指针变量</strong></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> (*p)[<span class="number">10</span>];</span><br></pre></td></tr></table></figure><p><strong>解释</strong>:<code>p先和*结合,说明p是一个指针变量,然后指针指向的是一个大小为 10 个整型的数组。所以p是一个指针,指向一个数组,叫 **数组指针** </code>。</p><blockquote><p><strong>注意</strong>:<code>[]的优先级要高于*号的,所以必须加上()来保证p先和*结合</code>。</p></blockquote><h2 id="数组指针变量怎么初始化"><a href="#数组指针变量怎么初始化" class="headerlink" title="数组指针变量怎么初始化"></a>数组指针变量怎么初始化</h2><blockquote><p>数组指针变量是用来存放数组地址的,那怎么获得数组的地址呢?就是我们之前学习的&数组名。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> arr[<span class="number">10</span>] = { <span class="number">0</span> };</span><br><span class="line">&arr;<span class="comment">//得到的就是数组的地址</span></span><br></pre></td></tr></table></figure><blockquote><p>如果要存放个数组的地址,就得存放在 <strong>数组指针变量</strong> 中,如下:</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span> <span class="type">int</span>(*p)[ <span class="number">10</span> ] = &arr;</span><br></pre></td></tr></table></figure><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/49708/image-3.png" alt="alt text"></p><p><code>我们调试也能看到&arr和p的类型是完全一致的</code>。</p><p><strong>数组指针类型解析</strong>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> (*p) [<span class="number">10</span>] = &arr;</span><br><span class="line"> | | |</span><br><span class="line"> | | |</span><br><span class="line"> | | p指向数组的元素个数</span><br><span class="line"> | p是数组指针变量名</span><br><span class="line"> p指向的数组的元素类型</span><br></pre></td></tr></table></figure><h1 id="二维数组传参的本质"><a href="#二维数组传参的本质" class="headerlink" title="二维数组传参的本质"></a>二维数组传参的本质</h1><blockquote><p>有了数组指针的理解,我们就能够讲一下二维数组传参的本质了。</p></blockquote><p><code>过去我们有一个二维数组的需要传参给一个函数的时候,我们是这样写的</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 定义一个名为Print的函数,接受一个二维整型数组a,以及两个整型参数r(行数)和c(列数)</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">Pirnt</span><span class="params">(<span class="type">int</span> a[ <span class="number">3</span> ][ <span class="number">5</span> ], <span class="type">int</span> r, <span class="type">int</span> c)</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 使用嵌套的for循环遍历二维数组a的每个元素</span></span><br><span class="line"> <span class="keyword">for</span>(i= <span class="number">0</span> ; i<r; i++) <span class="comment">//i锁定行</span></span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">for</span>(j= <span class="number">0</span> ; j<c; j++) <span class="comment">//j锁定列</span></span><br><span class="line"> {</span><br><span class="line"> <span class="comment">// 打印二维数组a中每个元素的值</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d "</span>, a[i][j]); <span class="comment">//注意这里的i和j是循环变量,不是数组下标</span></span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 打印换行符,换行显示二维数组</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 主函数</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 声明并初始化一个3行5列的二维数组arr</span></span><br><span class="line"> <span class="type">int</span> arr[ <span class="number">3</span> ][ <span class="number">5</span> ] = {{ <span class="number">1</span> , <span class="number">2</span> , <span class="number">3</span> , <span class="number">4</span> , <span class="number">5</span> }, { <span class="number">2</span> , <span class="number">3</span> , <span class="number">4</span> , <span class="number">5</span> , <span class="number">6</span> },{ <span class="number">3</span> , <span class="number">4</span> , <span class="number">5</span> , <span class="number">6</span> , <span class="number">7</span> }};</span><br><span class="line"> <span class="comment">// 调用Print函数,并传入数组arr以及其行列数作为参数</span></span><br><span class="line"> Pirnt(arr, <span class="number">3</span> , <span class="number">5</span> ); <span class="comment">//将arr数组的内容打印出来</span></span><br><span class="line"> </span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>运行结果:</code></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span> <span class="number">2</span> <span class="number">3</span> <span class="number">4</span> <span class="number">5</span> </span><br><span class="line"><span class="number">2</span> <span class="number">3</span> <span class="number">4</span> <span class="number">5</span> <span class="number">6</span> </span><br><span class="line"><span class="number">3</span> <span class="number">4</span> <span class="number">5</span> <span class="number">6</span> <span class="number">7</span> </span><br></pre></td></tr></table></figure><p><code>类比</code>:</p><p><code>一维数组传参</code>:</p><ol><li><p><strong>数组名是首元素地址</strong></p></li><li><p><strong>一维数组在传参的时候,传递的是数组的地址,也就是数组的首元素的地址</strong>。</p></li><li><p><strong>函数的参数可以写成数组,也可以写成指针形式</strong>。</p></li></ol><p><code>二维数组传参</code>:</p><ol><li><strong>数组名是首元素地址</strong></li></ol><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/49708/image-5.png" alt="alt text"></p><ol start="2"><li><p><strong>二维数组可以理解为一维数组的数组,也就是每个元素是一个一维数组</strong>。</p></li><li><p><strong>二维数组的每一行可以看做是一个一维数组,所以二维数组其实是一个一维数组,二维数组的首元素就是它的第一行</strong>。</p></li><li><p><strong>所以二维数组的数组名表示的就是第一行的地址,是一维数组的地址</strong>。</p></li></ol><p>如下图:<br><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/49708/image-4.png" alt="alt text"></p><hr><p><code>根据上面的例子,第一行的一维数组的类型就是int [5],所以第一行的地址的类型就是数组指针类型int(*)[5]。那就意味着</code> <strong>二维数组传参本质上也是传递了地址,传递的是第一行这个一维数组的地址</strong> ,<code>那么形参也是可以写成指针形式的</code>。如下:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">Print</span><span class="params">(<span class="type">int</span>(*arr)[<span class="number">5</span>], <span class="type">int</span> r, <span class="type">int</span> c)</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 使用嵌套的for循环遍历二维数组a的每个元素</span></span><br><span class="line"> <span class="keyword">for</span>(i= <span class="number">0</span> ; i<r; i++) <span class="comment">//i锁定行</span></span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">for</span>(j= <span class="number">0</span> ; j<c; j++) <span class="comment">//j锁定列</span></span><br><span class="line"> {</span><br><span class="line"> <span class="comment">// 打印二维数组a中每个元素的值</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d "</span>, *(*(arr+i)+j);<span class="comment">//*(arr+i)获取第i行的首地址,然后再加上j,获取第i行第j个元素的地址,再用*解引用,获取该元素的值。</span></span><br><span class="line"> <span class="comment">//*(arr+i) == arr[i]——>arr[i]是第i行的数组名,数组名又表示数组首元素的地址,arr[i]表示是&arr[i][0]</span></span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 打印换行符,换行显示二维数组</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 主函数</span></span><br><span class="line"><span class="type">int</span> main()</span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 声明并初始化一个3行5列的二维数组arr</span></span><br><span class="line"> <span class="type">int</span> arr[ <span class="number">3</span> ][ <span class="number">5</span> ] = {{ <span class="number">1</span> , <span class="number">2</span> , <span class="number">3</span> , <span class="number">4</span> , <span class="number">5</span> }, { <span class="number">2</span> , <span class="number">3</span> , <span class="number">4</span> , <span class="number">5</span> , <span class="number">6</span> },{ <span class="number">3</span> , <span class="number">4</span> , <span class="number">5</span> , <span class="number">6</span> , <span class="number">7</span> }};</span><br><span class="line"> <span class="comment">// 调用Print函数,并传入数组arr以及其行列数作为参数</span></span><br><span class="line"> Print(arr, <span class="number">3</span> , <span class="number">5</span> ); <span class="comment">//将arr数组的内容打印出来</span></span><br><span class="line"> </span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>运行结果</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span> <span class="number">2</span> <span class="number">3</span> <span class="number">4</span> <span class="number">5</span> </span><br><span class="line"><span class="number">2</span> <span class="number">3</span> <span class="number">4</span> <span class="number">5</span> <span class="number">6</span> </span><br><span class="line"><span class="number">3</span> <span class="number">4</span> <span class="number">5</span> <span class="number">6</span> <span class="number">7</span> </span><br></pre></td></tr></table></figure><p><strong>总结:</strong> <code>二维数组传参,形参的部分可以写成数组,也可以写成指针形式</code>。</p><p><code>补充解释</code>:</p><p><strong>第9行代码</strong>:<code>*(*(arr+i)+j)</code></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/49708/image-6.png" alt="alt text"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/49708/image-7.png" alt="alt text"></p><h1 id="函数指针变量"><a href="#函数指针变量" class="headerlink" title="函数指针变量"></a>函数指针变量</h1><h2 id="函数指针变量的创建"><a href="#函数指针变量的创建" class="headerlink" title="函数指针变量的创建"></a>函数指针变量的创建</h2><blockquote><p>什么是函数指针变量呢?</p></blockquote><p><code>根据前面学习整型指针,数组指针的时候,我们的类比关系,我们不难得出结论</code>:</p><blockquote><p><strong>函数指针变量应该是用来存放函数地址的,未来通过地址能够调用函数的</strong>。</p></blockquote><p><code>那么函数是否有地址呢?</code></p><p><code>做个测试</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">Add</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">return</span> x + y;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"><span class="comment">//int a = 10;</span></span><br><span class="line"><span class="comment">//int* pa = &a;//整型指针变量</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//int arr[5] = {0};</span></span><br><span class="line"><span class="comment">//int (*parr)[5] = &arr;//parr 是数组指针变量</span></span><br><span class="line"><span class="comment">//arr &arr</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//&函数名和函数名都是函数的地址,没有区别,打印两个函数的地址是一样的。</span></span><br><span class="line"><span class="comment">//printf("%p\n", &Add);//打印函数的地址</span></span><br><span class="line"><span class="comment">//printf("%p\n", Add);//打印函数的地址</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//int (*pf)(int,int) = &Add;//pf 函数指针变量</span></span><br><span class="line"><span class="type">int</span> (* pf)(<span class="type">int</span>, <span class="type">int</span>) = Add;<span class="comment">//pf 函数指针变量</span></span><br><span class="line"><span class="type">int</span> ret2 = (*pf)(<span class="number">4</span>, <span class="number">5</span>);<span class="comment">//也可以写成 int ret2 = pf(4, 5);//调用函数指针变量</span></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d\n"</span>, ret2);<span class="comment">//ret2 = 9</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> ret = Add(<span class="number">4</span>, <span class="number">5</span>);<span class="comment">//常规调用函数</span></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d\n"</span>, ret);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//int (*pf)(int x, int y) = &Add;//pf 函数指针变量</span></span><br><span class="line"><span class="comment">//int (*)(int,int) 函数指针类型</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义一个不返回数值的函数test</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">test</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"hehe\n"</span>); <span class="comment">// 在控制台输出"hehe"</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 主函数</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"test: %p\n"</span>, test); <span class="comment">// 打印函数test的地址</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"&test: %p\n"</span>, &test); <span class="comment">// 打印函数test的地址</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>; <span class="comment">// 返回0</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>输出结果如下</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">test: <span class="number">005913</span> CA</span><br><span class="line">&test: <span class="number">005913</span> CA</span><br></pre></td></tr></table></figure><blockquote><p>确实打印出来了地址,所以函数是有地址的,函数名就是函数的地址,当然也可以通过&函数名的方<br>式获得函数的地址。</p></blockquote><p><code>如果我们要将函数的地址存放起来,就得创建函数指针变量咯,函数指针变量的写法其实和数组指针 非常类似</code>。<strong>如下</strong>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 定义一个无返回值、无参数的函数test</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">test</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"hehe\n"</span>); <span class="comment">// 打印出"hehe"</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 声明一个指向test函数的指针pf1</span></span><br><span class="line"><span class="type">void</span> (*pf1)() = &test;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 声明一个指向test函数的指针pf2,可以省略取地址运算符&</span></span><br><span class="line"><span class="type">void</span> (*pf2)()= test;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义一个有两个参数、返回整型值的函数Add</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">Add</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span></span><br><span class="line">{</span><br><span class="line"> <span class="keyword">return</span> x+y; <span class="comment">// 返回x和y的和</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 声明一个指向Add函数的指针pf3</span></span><br><span class="line"><span class="type">int</span> (*pf3)(<span class="type">int</span>, <span class="type">int</span>) = Add;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 声明一个指向Add函数的指针pf3,参数可以写上类型名或者省略</span></span><br><span class="line"><span class="type">int</span> (*pf3)(<span class="type">int</span> x, <span class="type">int</span> y) = &Add;</span><br></pre></td></tr></table></figure><p><code>函数指针类型解析</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> (*pf3) (<span class="type">int</span> x, <span class="type">int</span> y)</span><br><span class="line">| | ------------</span><br><span class="line">| | |</span><br><span class="line">| | pf3指向函数的参数类型和个数的交代</span><br><span class="line">| 函数指针变量名</span><br><span class="line">pf3指向函数的返回类型</span><br><span class="line"><span class="type">int</span> (*) (<span class="type">int</span> x, <span class="type">int</span> y) <span class="comment">//pf3函数指针变量的类型</span></span><br></pre></td></tr></table></figure><h2 id="函数指针变量的使用"><a href="#函数指针变量的使用" class="headerlink" title="函数指针变量的使用"></a>函数指针变量的使用</h2><blockquote><p>通过函数指针调用指针指向的函数。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义一个函数,名称为Add,接受两个int类型的参数x和y,返回它们的和</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">Add</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span></span><br><span class="line">{</span><br><span class="line"> <span class="keyword">return</span> x + y;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 声明一个指向函数的指针pf3,该函数接受两个int类型参数并返回int类型的结果</span></span><br><span class="line"> <span class="type">int</span> (*pf3)(<span class="type">int</span>, <span class="type">int</span>) = Add;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 通过指针pf3调用Add函数并输出结果</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, (*pf3)(<span class="number">2</span>, <span class="number">3</span>));</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 通过指针pf3直接调用Add函数并输出结果</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, pf3(<span class="number">3</span>, <span class="number">5</span>));</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>输出结果</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">5</span></span><br><span class="line"><span class="number">8</span></span><br></pre></td></tr></table></figure><h2 id="两段有趣的代码-了解一下"><a href="#两段有趣的代码-了解一下" class="headerlink" title="两段有趣的代码(了解一下)"></a>两段有趣的代码(<code>了解一下</code>)</h2><p><code>代码 1 </code></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(*(<span class="type">void</span> (*)()) <span class="number">0</span> )();</span><br></pre></td></tr></table></figure><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/49708/image-8.png" alt="alt text"></p><p><code>代码 2</code> </p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> (*signal(<span class="type">int</span> , <span class="type">void</span>(*)(<span class="type">int</span>)))(<span class="type">int</span>);</span><br></pre></td></tr></table></figure><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/49708/image-9.png" alt="alt text"></p><p><code>两段代码均出自:《C陷阱和缺陷》这本书</code></p><h3 id="typedef关键字-了解一下"><a href="#typedef关键字-了解一下" class="headerlink" title="typedef关键字(了解一下)"></a>typedef关键字(<code>了解一下</code>)</h3><p>typedef是用来类型重命名的,可以将复杂的类型,简单化。</p><p>比如,你觉得<code>unsigned int</code>写起来不方便,如果能写成<code>uint</code>就方便多了,那么我们可以使用:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> <span class="type">unsigned</span> <span class="type">int</span> uint;</span><br><span class="line"><span class="comment">//将unsigned int 重命名为uint</span></span><br></pre></td></tr></table></figure><p>如果是指针类型,能否重命名呢?其实也是可以的,比如,将<code>int*</code>重命名为<code>ptr_t</code>,这样写:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span> <span class="keyword">typedef</span> <span class="type">int</span>* <span class="type">ptr_t</span>;</span><br></pre></td></tr></table></figure><p>但是对于数组指针和函数指针稍微有点区别:</p><p>比如我们有数组指针类型<code>int(*)[5]</code>,需要重命名为<code>parr_t</code>,那可以这样写:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span> <span class="keyword">typedef</span> <span class="title function_">int</span><span class="params">(*<span class="type">parr_t</span>)</span>[5]; <span class="comment">//新的类型名必须在*的右边</span></span><br></pre></td></tr></table></figure><p>函数指针类型的重命名也是一样的,比如,将<code>void(*)(int)</code>类型重命名为<code>pf_t</code>,就可以这样写:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span> <span class="keyword">typedef</span> <span class="title function_">void</span><span class="params">(*<span class="type">pfun_t</span>)</span><span class="params">(<span class="type">int</span>)</span>;<span class="comment">//新的类型名必须在*的右边</span></span><br></pre></td></tr></table></figure><p>那么要简化代码 2 ,可以这样写:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> <span class="title function_">void</span><span class="params">(*<span class="type">pfun_t</span>)</span><span class="params">(<span class="type">int</span>)</span>;</span><br><span class="line"><span class="type">pfun_t</span> <span class="title function_">signal</span><span class="params">(<span class="type">int</span>, <span class="type">pfun_t</span>)</span>;</span><br></pre></td></tr></table></figure><h1 id="函数指针数组"><a href="#函数指针数组" class="headerlink" title="函数指针数组"></a>函数指针数组</h1><p>数组是一个存放相同类型数据的存储空间,我们已经学习了指针数组,</p><p>比如:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> * arr[ <span class="number">10</span> ];</span><br><span class="line"><span class="comment">//数组的每个元素是int*</span></span><br></pre></td></tr></table></figure><blockquote><p>那要把函数的地址存到一个数组中,那这个数组就叫 函数指针数组 ,那函数指针的数组如何定义呢?</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> (*parr1[ <span class="number">3</span> ])();</span><br><span class="line"><span class="type">int</span> *parr2[ <span class="number">3</span> ]();</span><br><span class="line"><span class="type">int</span> (*)() parr3[ <span class="number">3</span> ];</span><br></pre></td></tr></table></figure><p>答案是:<code>parr 1 </code></p><blockquote><p><code>parr1</code>先和<code>[]</code>结合,说明parr 1 是数组,数组的内容是什么呢?</p></blockquote><p>是<code>int (*)()</code>类型的函数指针。</p><p><code>举例</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//指针数组</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//加减乘除四个函数的地址存到数组中</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//定义四个函数</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">Add</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span><span class="comment">//加法函数</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">return</span> x + y;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">Sub</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span><span class="comment">//减法函数</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">return</span> x - y;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">Mul</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span><span class="comment">//乘法函数</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">return</span> x * y;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">Div</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span><span class="comment">//除法函数</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">return</span> x / y;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"><span class="comment">//int* arr1[6];//整型指针数组</span></span><br><span class="line"><span class="comment">//char* arr2[5];//字符指针数组</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//arr3[5];//每个元素是函数的地址 - 函数指针数组</span></span><br><span class="line"><span class="comment">//int (*pf1)(int, int) = Add;</span></span><br><span class="line"><span class="comment">//int (*pf2)(int, int) = Sub;</span></span><br><span class="line"><span class="comment">//int (*pf3)(int, int) = Mul;</span></span><br><span class="line"><span class="comment">//int (*pf4)(int, int) = Div;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//函数指针数组,来存放这些函数的地址呢?</span></span><br><span class="line"><span class="type">int</span> (* pf[<span class="number">4</span>])(<span class="type">int</span>, <span class="type">int</span>) = {Add, Sub, Mul, Div};</span><br><span class="line"> <span class="comment">// 0 1 2 3 </span></span><br><span class="line"> <span class="comment">//通过数组下标来存放函数的地址</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">for</span> (i = <span class="number">0</span>; i < <span class="number">4</span>; i++)<span class="comment">//遍历数组,调用函数</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> ret = pf[i](<span class="number">6</span>, <span class="number">2</span>);<span class="comment">//通过数组下标调用函数</span></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d\n"</span>, ret);</span><br><span class="line">}</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>运行结果</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">6</span></span><br><span class="line"><span class="number">4</span></span><br><span class="line"><span class="number">12</span></span><br><span class="line"><span class="number">3</span></span><br></pre></td></tr></table></figure><h1 id="转移表"><a href="#转移表" class="headerlink" title="转移表"></a>转移表</h1><p><code>函数指针数组</code>的用途: <code>转移表</code></p><p>举例:<code>计算器的一般实现</code>:</p><p><code>(1)</code></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string"><stdio.h></span></span></span><br><span class="line"><span class="comment">// 原始实现的计算器功能</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义加法函数,接收两个整数参数,返回它们的和</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">add</span><span class="params">(<span class="type">int</span> a, <span class="type">int</span> b)</span></span><br><span class="line">{</span><br><span class="line"> <span class="keyword">return</span> a + b;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义减法函数,接收两个整数参数,返回它们的差</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">sub</span><span class="params">(<span class="type">int</span> a, <span class="type">int</span> b)</span></span><br><span class="line">{</span><br><span class="line"> <span class="keyword">return</span> a - b;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义乘法函数,接收两个整数参数,返回它们的积</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">mul</span><span class="params">(<span class="type">int</span> a, <span class="type">int</span> b)</span></span><br><span class="line">{</span><br><span class="line"> <span class="keyword">return</span> a * b;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义除法函数,接收两个整数参数,返回它们的商</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">div</span><span class="params">(<span class="type">int</span> a, <span class="type">int</span> b)</span></span><br><span class="line">{</span><br><span class="line"> <span class="keyword">return</span> a / b;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 主函数</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> x, y; <span class="comment">// 用于接收用户输入的操作数</span></span><br><span class="line"> <span class="type">int</span> input = <span class="number">1</span>; <span class="comment">// 用于接收用户输入的选项</span></span><br><span class="line"> <span class="type">int</span> ret = <span class="number">0</span>; <span class="comment">// 用于接收计算结果</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">// 循环,直到用户选择退出</span></span><br><span class="line"> <span class="keyword">do</span></span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"*************************\n"</span>);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">" 1:add 2:sub \n"</span>);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">" 3:mul 4:div \n"</span>);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">" 0:exit \n"</span>);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"*************************\n"</span>);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"请选择:"</span>);</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &input); <span class="comment">// 接收用户选择的选项</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">// 根据用户选择的选项执行相应的操作</span></span><br><span class="line"> <span class="keyword">switch</span> (input)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"输入操作数:"</span>);</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d %d"</span>, &x, &y); <span class="comment">// 接收用户输入的操作数</span></span><br><span class="line"> ret = add(x, y); <span class="comment">// 调用加法函数</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"ret = %d\n"</span>, ret); <span class="comment">// 输出计算结果</span></span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"输入操作数:"</span>);</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d %d"</span>, &x, &y); <span class="comment">// 接收用户输入的操作数</span></span><br><span class="line"> ret = sub(x, y); <span class="comment">// 调用减法函数</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"ret = %d\n"</span>, ret); <span class="comment">// 输出计算结果</span></span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="number">3</span>:</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"输入操作数:"</span>);</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d %d"</span>, &x, &y); <span class="comment">// 接收用户输入的操作数</span></span><br><span class="line"> ret = mul(x, y); <span class="comment">// 调用乘法函数</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"ret = %d\n"</span>, ret); <span class="comment">// 输出计算结果</span></span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="number">4</span>:</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"输入操作数:"</span>);</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d %d"</span>, &x, &y); <span class="comment">// 接收用户输入的操作数</span></span><br><span class="line"> ret = div(x, y); <span class="comment">// 调用除法函数</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"ret = %d\n"</span>, ret); <span class="comment">// 输出计算结果</span></span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="number">0</span>:</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"退出程序\n"</span>);</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">default</span>:</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"选择错误\n"</span>);</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> } <span class="keyword">while</span> (input); <span class="comment">// 继续循环,直到用户选择退出</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>(2)</code></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 实现一个计算器</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 菜单函数</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">menu</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 打印菜单选项</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"********************************\n"</span>);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"****** 1. add 2. sub *****\n"</span>);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"****** 3. mul 4. div *****\n"</span>);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"****** 0. exit *****\n"</span>);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"********************************\n"</span>);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 加法函数</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">Add</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 返回两个整数的和</span></span><br><span class="line"> <span class="keyword">return</span> x + y;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 减法函数</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">Sub</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 返回两个整数的差</span></span><br><span class="line"> <span class="keyword">return</span> x - y;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 乘法函数</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">Mul</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 返回两个整数的积</span></span><br><span class="line"> <span class="keyword">return</span> x * y;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 除法函数</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">Div</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 返回两个整数的商</span></span><br><span class="line"> <span class="keyword">return</span> x / y;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> input = <span class="number">0</span>; <span class="comment">// 用户输入的选项</span></span><br><span class="line"> <span class="type">int</span> x = <span class="number">0</span>;</span><br><span class="line"> <span class="type">int</span> y = <span class="number">0</span>;</span><br><span class="line"> <span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">do</span></span><br><span class="line"> {</span><br><span class="line"> menu(); <span class="comment">// 显示菜单</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"请选择:"</span>);</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &input); <span class="comment">// 接收用户选择</span></span><br><span class="line"> <span class="keyword">switch</span> (input)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"请输入两个操作数:"</span>);</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d %d"</span>, &x, &y); <span class="comment">// 输入两个操作数</span></span><br><span class="line"> ret = Add(x, y); <span class="comment">// 调用加法函数</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, ret); <span class="comment">// 显示结果</span></span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"请输入两个操作数:"</span>);</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d %d"</span>, &x, &y); <span class="comment">// 输入两个操作数</span></span><br><span class="line"> ret = Sub(x, y); <span class="comment">// 调用减法函数</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, ret); <span class="comment">// 显示结果</span></span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="number">3</span>:</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"请输入两个操作数:"</span>);</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d %d"</span>, &x, &y); <span class="comment">// 输入两个操作数</span></span><br><span class="line"> ret = Mul(x, y); <span class="comment">// 调用乘法函数</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, ret); <span class="comment">// 显示结果</span></span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="number">4</span>:</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"请输入两个操作数:"</span>);</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d %d"</span>, &x, &y); <span class="comment">// 输入两个操作数</span></span><br><span class="line"> ret = Div(x, y); <span class="comment">// 调用除法函数</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, ret); <span class="comment">// 显示结果</span></span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="number">0</span>:</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"退出计算器\n"</span>); <span class="comment">// 显示退出消息</span></span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">default</span>:</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"选择错误,重新选择\n"</span>); <span class="comment">// 显示错误消息</span></span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> } <span class="keyword">while</span> (input); <span class="comment">// 重复执行,直到用户选择退出</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>运行结果</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">********************************</span><br><span class="line">****** <span class="number">1.</span> add <span class="number">2.</span> sub *****</span><br><span class="line">****** <span class="number">3.</span> mul <span class="number">4.</span> div *****</span><br><span class="line">****** <span class="number">0.</span> <span class="built_in">exit</span> *****</span><br><span class="line">********************************</span><br><span class="line">请选择:<span class="number">1</span></span><br><span class="line">请输入两个操作数:<span class="number">10</span> <span class="number">20</span></span><br><span class="line"><span class="number">30</span></span><br><span class="line">请选择:<span class="number">2</span></span><br><span class="line">请输入两个操作数:<span class="number">30</span> <span class="number">10</span></span><br><span class="line"><span class="number">20</span></span><br><span class="line">请选择:<span class="number">3</span></span><br><span class="line">请输入两个操作数:<span class="number">2</span> <span class="number">3</span></span><br><span class="line"><span class="number">6</span></span><br><span class="line">请选择:<span class="number">4</span></span><br><span class="line">请输入两个操作数:<span class="number">10</span> <span class="number">2</span></span><br><span class="line"><span class="number">5</span></span><br><span class="line">请选择:<span class="number">0</span></span><br><span class="line">退出计算器</span><br></pre></td></tr></table></figure><p><code>特殊情况</code>:</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/49708/image-10.png" alt="alt text"></p><p><code>(1)(2)缺点</code>:</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/49708/image-11.png" alt="alt text"></p><ul><li>代码冗余:代码中有很多重复的函数定义,增加了代码的复杂度。</li><li>效率低:每一次用户输入都需要遍历数组,效率低。</li></ul><p><code>(3)</code>在(2)的基础上,使用<code>函数指针数组</code>来实现转移表。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 菜单函数</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">menu</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 打印菜单选项</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"********************************\n"</span>);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"****** 1. add 2. sub *****\n"</span>);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"****** 3. mul 4. div *****\n"</span>);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"****** 0. exit *****\n"</span>);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"********************************\n"</span>);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 加法函数</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">Add</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span></span><br><span class="line">{</span><br><span class="line"> <span class="keyword">return</span> x + y;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 减法函数</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">Sub</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span></span><br><span class="line">{</span><br><span class="line"> <span class="keyword">return</span> x - y;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 乘法函数</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">Mul</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span></span><br><span class="line">{</span><br><span class="line"> <span class="keyword">return</span> x * y;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 除法函数</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">Div</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span></span><br><span class="line">{</span><br><span class="line"> <span class="keyword">return</span> x / y;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> input = <span class="number">0</span>;</span><br><span class="line"> <span class="type">int</span> x = <span class="number">0</span>;</span><br><span class="line"> <span class="type">int</span> y = <span class="number">0</span>;</span><br><span class="line"> <span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line"> <span class="comment">// 函数指针数组 - 转移表</span></span><br><span class="line"> <span class="type">int</span> (*pfArr[<span class="number">5</span>])(<span class="type">int</span>, <span class="type">int</span>) = {<span class="number">0</span>, Add, Sub, Mul, Div};</span><br><span class="line"> <span class="comment">// 0 1 2 3 4</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">do</span></span><br><span class="line"> {</span><br><span class="line"> menu(); <span class="comment">// 调用菜单函数</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"请选择:"</span>);</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &input);</span><br><span class="line"> <span class="keyword">if</span> (input >= <span class="number">1</span> && input <= <span class="number">4</span>) <span class="comment">// 判断用户输入是否在合法范围内</span></span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"请输入两个操作数:"</span>);</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d %d"</span>, &x, &y);</span><br><span class="line"> ret = pfArr[input](x, y); <span class="comment">// 根据用户输入调用相应的函数,并计算结果</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, ret); <span class="comment">// 输出计算结果</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span> (input == <span class="number">0</span>)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"退出计算器\n"</span>); <span class="comment">// 用户选择退出</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"选择错误,重新选择\n"</span>); <span class="comment">// 用户输入不合法</span></span><br><span class="line"> }</span><br><span class="line"> } <span class="keyword">while</span> (input); <span class="comment">// 循环直到用户选择退出</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>运行结果</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">********************************</span><br><span class="line">****** <span class="number">1.</span> add <span class="number">2.</span> sub *****</span><br><span class="line">****** <span class="number">3.</span> mul <span class="number">4.</span> div *****</span><br><span class="line">****** <span class="number">0.</span> <span class="built_in">exit</span> *****</span><br><span class="line">********************************</span><br><span class="line">请选择:<span class="number">1</span></span><br><span class="line">请输入两个操作数:<span class="number">10</span> <span class="number">20</span></span><br><span class="line"><span class="number">30</span></span><br><span class="line">请选择:<span class="number">2</span></span><br><span class="line">请输入两个操作数:<span class="number">30</span> <span class="number">10</span></span><br><span class="line"><span class="number">20</span></span><br><span class="line">请选择:<span class="number">3</span></span><br><span class="line">请输入两个操作数:<span class="number">2</span> <span class="number">3</span></span><br><span class="line"><span class="number">6</span></span><br><span class="line">请选择:<span class="number">4</span></span><br><span class="line">请输入两个操作数:<span class="number">10</span> <span class="number">2</span></span><br><span class="line"><span class="number">5</span></span><br><span class="line">请选择:<span class="number">0</span></span><br><span class="line">退出计算器</span><br></pre></td></tr></table></figure><p><code>(4)</code>再次优化,<code>Calc()</code>函数可以接收一个函数指针作为参数,并调用该函数。</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/49708/image-12.png" alt="alt text"></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 输出菜单</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">menu</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 显示计算器菜单</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"********************************\n"</span>);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"****** 1. add 2. sub *****\n"</span>);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"****** 3. mul 4. div *****\n"</span>);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"****** 0. exit *****\n"</span>);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"********************************\n"</span>);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 加法函数</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">Add</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 返回输入参数的和</span></span><br><span class="line"> <span class="keyword">return</span> x + y;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 减法函数</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">Sub</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 返回输入参数的差</span></span><br><span class="line"> <span class="keyword">return</span> x - y;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 乘法函数</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">Mul</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 返回输入参数的积</span></span><br><span class="line"> <span class="keyword">return</span> x * y;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 除法函数</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">Div</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 返回输入参数的商</span></span><br><span class="line"> <span class="keyword">return</span> x / y;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 计算函数</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">Calc</span><span class="params">(<span class="type">int</span> (*pf)(<span class="type">int</span>, <span class="type">int</span>))</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 使用用户选择的操作符函数,计算输入的两个操作数</span></span><br><span class="line"> <span class="type">int</span> x = <span class="number">0</span>;</span><br><span class="line"> <span class="type">int</span> y = <span class="number">0</span>;</span><br><span class="line"> <span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"请输入两个操作数:"</span>);</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d %d"</span>, &x, &y);</span><br><span class="line"> ret = pf(x, y);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, ret);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 主函数</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 主函数,可执行计算器操作</span></span><br><span class="line"> <span class="type">int</span> input = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">do</span></span><br><span class="line"> {</span><br><span class="line"> <span class="comment">// 显示菜单,接收用户输入</span></span><br><span class="line"> menu();</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"请选择:"</span>);</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &input);</span><br><span class="line"> <span class="keyword">switch</span> (input)</span><br><span class="line"> {</span><br><span class="line"> <span class="comment">// 根据用户输入选择不同的计算操作</span></span><br><span class="line"> <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line"> Calc(Add); <span class="comment">// 调用加法函数</span></span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line"> Calc(Sub); <span class="comment">// 调用减法函数</span></span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="number">3</span>:</span><br><span class="line"> Calc(Mul); <span class="comment">// 调用乘法函数</span></span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="number">4</span>:</span><br><span class="line"> Calc(Div); <span class="comment">// 调用除法函数</span></span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="number">0</span>:</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"退出计算器\n"</span>); <span class="comment">// 退出程序</span></span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">default</span>:</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"选择错误,重新选择\n"</span>); <span class="comment">// 错误提示</span></span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> } <span class="keyword">while</span> (input);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>运行结果</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">********************************</span><br><span class="line">****** <span class="number">1.</span> add <span class="number">2.</span> sub *****</span><br><span class="line">****** <span class="number">3.</span> mul <span class="number">4.</span> div *****</span><br><span class="line">****** <span class="number">0.</span> <span class="built_in">exit</span> *****</span><br><span class="line">********************************</span><br><span class="line">请选择:<span class="number">1</span></span><br><span class="line">请输入两个操作数:<span class="number">10</span> <span class="number">20</span></span><br><span class="line"><span class="number">30</span></span><br><span class="line">请选择:<span class="number">2</span></span><br><span class="line">请输入两个操作数:<span class="number">30</span> <span class="number">10</span></span><br><span class="line"><span class="number">20</span></span><br><span class="line">请选择:<span class="number">3</span></span><br><span class="line">请输入两个操作数:<span class="number">2</span> <span class="number">3</span></span><br><span class="line"><span class="number">6</span></span><br><span class="line">请选择:<span class="number">4</span></span><br><span class="line">请输入两个操作数:<span class="number">10</span> <span class="number">2</span></span><br><span class="line"><span class="number">5</span></span><br><span class="line">请选择:<span class="number">0</span></span><br><span class="line">退出计算器</span><br></pre></td></tr></table></figure><h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><ul><li>指针变量:指向内存中其他变量的变量,可以用来存放地址、函数指针、数组指针等。</li><li>指针数组:存放指针变量的数组,可以用来存放不同类型的指针变量。</li><li>函数指针数组:存放函数指针的数组,可以用来实现转移表。</li><li>转移表:根据用户输入的选项,调用相应的函数。</li></ul><blockquote><p><strong>完</strong></p></blockquote>]]></content>
<categories>
<category> C语言程序设计 </category>
</categories>
<tags>
<tag> C语言程序设计知识点总结 </tag>
</tags>
</entry>
<entry>
<title>第12讲:深⼊理解指针-通过指针引用数组</title>
<link href="/posts/18731/"/>
<url>/posts/18731/</url>
<content type="html"><![CDATA[<h1 id="目录"><a href="#目录" class="headerlink" title="目录"></a>目录</h1><ol><li><strong>数组名的理解</strong></li><li>*<strong>使用指针访问数</strong></li><li><strong>一维数组传参的本质</strong></li><li><strong>冒泡排序</strong></li><li><strong>二级指针</strong></li><li><strong>指针数组</strong></li><li><strong>指针数组模拟二维数组</strong></li></ol><blockquote><p><strong>正文开始</strong></p></blockquote><h1 id="数组名的理解-补充知识点"><a href="#数组名的理解-补充知识点" class="headerlink" title="数组名的理解(补充知识点)"></a>数组名的理解(补充知识点)</h1><blockquote><p><strong>使用指针访问数组的内容时,有这样的代码:</strong></p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> arr[ <span class="number">10</span> ] = { <span class="number">1</span> , <span class="number">2</span> , <span class="number">3</span> , <span class="number">4</span> , <span class="number">5</span> , <span class="number">6</span> , <span class="number">7</span> , <span class="number">8</span> , <span class="number">9</span> , <span class="number">10</span> };</span><br><span class="line"><span class="type">int</span> *p = &arr[ <span class="number">0</span> ];</span><br></pre></td></tr></table></figure><blockquote><p><strong>这里我们使用&arr[0]的方式拿到了数组第一个元素的地址,但是其实数组名本来就是地址,而且是数组首元素的地址,我们来做个测试</strong>。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{ </span><br><span class="line"><span class="type">int</span> arr[<span class="number">10</span>] = { <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>, <span class="number">10</span> }; <span class="comment">//数组在内存中是连续存放的,所以只用找到数组首元素的地址即可找到整个数组</span></span><br><span class="line"><span class="comment">//int* p = &arr[0]; //数组名是数组首元素的地址</span></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"&arr[0] = %p\n"</span>, &arr[<span class="number">0</span>]); <span class="comment">//取出首元素,得到数组首元素的地址</span></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"arr = %p\n"</span>, arr); <span class="comment">//数组名就是数组首元素的地址</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>输出结果(一下是在x86(打印的地址较短)架构下编译的):</strong></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/18731/image.png" alt="alt text"></p><blockquote><p>我们发现数组名和数组首元素的地址打印出的结果一模一样,<strong>数组名就是数组首元素(第一个元素)的地址</strong>。</p></blockquote><blockquote><p>这时候有同学会有疑问?数组名如果是数组首元素的地址,那下面的代码怎么理解呢?</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> arr[ <span class="number">10</span> ] = { <span class="number">1</span> , <span class="number">2</span> , <span class="number">3</span> , <span class="number">4</span> , <span class="number">5</span> , <span class="number">6</span> , <span class="number">7</span> , <span class="number">8</span> , <span class="number">9</span> , <span class="number">10</span> };</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, <span class="keyword">sizeof</span>(arr)); <span class="comment">//sizeof是就是计算数组大小的,如果数组名表示首元素地址,x86——>4,x64——>8,但是在x86打印出的是40,x64打印出的是80,这时候就有点迷糊了。</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/18731/image-1.png" alt="alt text"></p><p><strong>输出的结果是: 40 ,如果arr是数组首元素的地址,那输出应该的应该是 4 / 8 才对</strong>。</p><blockquote><p><strong>其实数组名就是数组首元素(第一个元素)的地址是对的,但是有两个例外:</strong></p></blockquote><ul><li><p><strong>sizeof(数组名)</strong> ,sizeof中<strong>单独放数组名</strong>,这里的数组名表示<strong>整个数组</strong>,计算的是<strong>整个数组的大小</strong>,单位是<strong>字节</strong>,所以<strong>打印</strong>出来的结果是<strong>40</strong>。</p></li><li><p><strong>&数组名</strong> ,这里的数组名表示<strong>整个数组</strong>,取出的是 <strong>整个数组的地址</strong> (整个数组的地址和数组首元素的地址是有区别的)</p></li></ul><p><strong>除此之外,任何地方使用数组名,数组名都表示首元素的地址</strong>。</p><blockquote><p><strong>这时有好奇的同学,再试一下这个代码:</strong></p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> arr[ <span class="number">10</span> ] = { <span class="number">1</span> , <span class="number">2</span> , <span class="number">3</span> , <span class="number">4</span> , <span class="number">5</span> , <span class="number">6</span> , <span class="number">7</span> , <span class="number">8</span> , <span class="number">9</span> , <span class="number">10</span> };</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"&arr[0] = %p\n"</span>, &arr[ <span class="number">0</span> ]); <span class="comment">//打印数组首元素的地址</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"arr = %p\n"</span>, arr); <span class="comment">//数组名打印数组首元素的地址</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"&arr = %p\n"</span>, &arr); <span class="comment">//打印整个数组的地址</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/18731/image-2.png" alt="alt text"></p><blockquote><p><strong>三个打印结果一模一样,这时候又纳闷了,那arr和&arr有啥区别呢?</strong></p></blockquote><blockquote><p><strong>总结</strong></p></blockquote><p>指针类型<strong>决定了</strong>指针的差异</p><hr><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> arr[ <span class="number">10</span> ] = { <span class="number">1</span> , <span class="number">2</span> , <span class="number">3</span> , <span class="number">4</span> , <span class="number">5</span> , <span class="number">6</span> , <span class="number">7</span> , <span class="number">8</span> , <span class="number">9</span> , <span class="number">10</span> };</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"&arr[0] = %p\n"</span>, &arr[ <span class="number">0</span> ]); <span class="comment">//打印数组首元素的地址</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"&arr[0]+1 = %p\n"</span>, &arr[ <span class="number">0</span> ]+ <span class="number">1</span> ); <span class="comment">//int*类型指针,地址加1,跳过第一个元素,4个字节</span></span><br><span class="line"></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"arr = %p\n"</span>, arr); <span class="comment">//数组名打印数组首元素的地址</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"arr+1 = %p\n"</span>, arr+ <span class="number">1</span> ); <span class="comment">//int*类型指针,地址加1,跳过第一个元素,4个字节</span></span><br><span class="line"></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"&arr = %p\n"</span>, &arr); <span class="comment">//打印整个数组的地址</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"&arr+1 = %p\n"</span>, &arr+ <span class="number">1</span> ); <span class="comment">//int*类型指针,地址加1,跳过整个数组,40个字节</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>输出结果:</strong></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&arr[ <span class="number">0</span> ] = <span class="number">0077F</span> <span class="number">820</span></span><br><span class="line">&arr[ <span class="number">0</span> ]+ <span class="number">1</span> = <span class="number">0077F</span> <span class="number">824</span></span><br><span class="line"></span><br><span class="line">arr = <span class="number">0077F</span> <span class="number">820</span></span><br><span class="line">arr+ <span class="number">1</span> = <span class="number">0077F</span> <span class="number">824</span></span><br><span class="line"></span><br><span class="line">&arr = <span class="number">0077F</span> <span class="number">820</span></span><br><span class="line">&arr+ <span class="number">1</span> = <span class="number">0077F</span> <span class="number">848</span></span><br></pre></td></tr></table></figure><p><strong>这里我们发现&arr[ 0 ]和&arr[ 0 ]+ 1 相差 4 个字节,arr和arr+ 1 相差 4 个字节,是因为&arr[ 0 ] 和 arr都是首元素的地址,+ 1 就是跳过一个元素</strong>。</p><p><strong>但是&arr和&arr+ 1 相差 40 个字节,这就是因为&arr是数组的地址,+ 1 操作是跳过整个数组的</strong>。</p><blockquote><p><strong>到这里大家应该搞清楚数组名的意义了吧</strong>。</p></blockquote><p><strong>数组名是数组首元素的地址,但是有 2 个例外</strong>。</p><h1 id="使用指针访问数组"><a href="#使用指针访问数组" class="headerlink" title="使用指针访问数组"></a>使用指针访问数组</h1><blockquote><p><strong>有了前面知识的支持,再结合数组的特点,我们就可以很方便的使用指针访问数组了</strong>。</p></blockquote><hr><p><strong>以下是使用数组下标访问数组元素的过程:</strong></p><hr><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> arr[ <span class="number">10</span> ] = { <span class="number">0</span> };</span><br><span class="line"> <span class="type">int</span> sz = <span class="keyword">sizeof</span>(arr)/<span class="keyword">sizeof</span>(arr[ <span class="number">0</span> ]); <span class="comment">//整个数组大小/一个元素的大小=数组的元素个数</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">//输入</span></span><br><span class="line"> <span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(i= <span class="number">0</span> ; i<sz; i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &arr[i]);</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//输出</span></span><br><span class="line"> <span class="keyword">for</span>(i= <span class="number">0</span> ; i<sz; i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d "</span>, arr[i]);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>输出结果:</strong></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/18731/image-3.png" alt="alt text"></p><hr><p><strong>以下是使用指针访问数组元素的过程:</strong></p><hr><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> arr[ <span class="number">10</span> ] = { <span class="number">0</span> };</span><br><span class="line"> <span class="type">int</span> sz = <span class="keyword">sizeof</span>(arr)/<span class="keyword">sizeof</span>(arr[ <span class="number">0</span> ]); <span class="comment">//整个数组大小/一个元素的大小=数组的元素个数</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">//输入</span></span><br><span class="line"> <span class="type">int</span> i = <span class="number">0</span> ;</span><br><span class="line"> <span class="type">int</span>* p = arr;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">for</span>(i= <span class="number">0</span> ; i<sz; i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>, p+i);<span class="comment">//p+i是下标为i的元素的地址</span></span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//输出</span></span><br><span class="line"> <span class="keyword">for</span>(i= <span class="number">0</span> ; i<sz; i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d "</span>, *(p+i)); <span class="comment">//对(p+i)进行指针加法,解引用,得到元素的值</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>输出结果:</strong></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/18731/image-4.png" alt="alt text"></p><blockquote><p><strong>数组名arr是数组首元素的地址,可以赋值给p,其实数组名arr和p在这里是等价的。那我们可以使用arr[i]可以访问数组的元素,那p[i]是否也可以访问数组呢?</strong></p></blockquote><hr><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> arr[ <span class="number">10</span> ] = { <span class="number">0</span> };</span><br><span class="line"> <span class="comment">//输入</span></span><br><span class="line"> <span class="type">int</span> i = <span class="number">0</span> ;</span><br><span class="line"> <span class="type">int</span> sz = <span class="keyword">sizeof</span>(arr)/<span class="keyword">sizeof</span>(arr[ <span class="number">0</span> ]); <span class="comment">//整个数组大小/一个元素的大小=数组的元素个数</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">//输入</span></span><br><span class="line"> <span class="type">int</span>* p = arr;</span><br><span class="line"> <span class="keyword">for</span>(i= <span class="number">0</span> ; i<sz; i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="comment">//scanf("%d", p+i); //p+i是下标为i的元素的地址</span></span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>, arr+i);<span class="comment">//也可以这样写</span></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">//输出</span></span><br><span class="line"> <span class="keyword">for</span>(i= <span class="number">0</span> ; i<sz; i++)</span><br><span class="line"> { </span><br><span class="line"> <span class="comment">//printf("%d ", *(p+i));</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d "</span>, *(arr+i)); <span class="comment">//也可以这样写</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>输出结果:</strong></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/18731/image-4.png" alt="alt text"></p><p><em><em>在第 18 行的地方,将</em>(p+i)换成p[i]也是能够正常打印的,所以本质上p[i]是等价于</em>(p+i)**。</p><p><em><em>同理arr[i]应该等价于</em>(arr+i),数组元素的访问在编译器处理的时候,也是转换成首元素的地址+偏移量求出元素的地址,然后解引用来访问的</em>*。</p><blockquote><p><strong>总结</strong></p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/18731/image-5.png" alt="alt text"></p><blockquote><p><strong>内容总结</strong></p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/18731/image-6.png" alt="alt text"></p><h1 id="一维数组传参的本质"><a href="#一维数组传参的本质" class="headerlink" title="一维数组传参的本质"></a>一维数组传参的本质</h1><p><strong>数组我们学过了,之前也讲了,数组是可以传递给函数的,这个小节我们讨论一下数组传参的本质</strong>。</p><blockquote><p><strong>首先从一个问题开始,我们之前都是在函数外部计算数组的元素个数,那我们可以把数组传给一个函数后,函数内部求数组的元素个数吗?</strong></p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">//数组传参的时候,形参可以写成指针形式,也可以写成数组形式</span></span><br><span class="line"><span class="comment">//写成数组形式,最简单,是为了方便理解,容易接受这种语法</span></span><br><span class="line"><span class="comment">//但是即使写成数组形式,本质上还是指针变量</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">//int * arr</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">test</span><span class="params">(<span class="type">int</span> arr[])</span> <span class="comment">//参数写成数组形式,本质上还是指针</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> sz2 = <span class="keyword">sizeof</span>(arr)/<span class="keyword">sizeof</span>(arr[ <span class="number">0</span> ]); <span class="comment">//计算一个指针变量的大小</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"sz2 = %d\n"</span>, sz2);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> arr[ <span class="number">10</span> ] = { <span class="number">1</span> , <span class="number">2</span> , <span class="number">3</span> , <span class="number">4</span> , <span class="number">5</span> , <span class="number">6</span> , <span class="number">7</span> , <span class="number">8</span> , <span class="number">9</span> , <span class="number">10</span> };</span><br><span class="line"> <span class="type">int</span> sz1 = <span class="keyword">sizeof</span>(arr)/<span class="keyword">sizeof</span>(arr[ <span class="number">0</span> ]); <span class="comment">//szl=10</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"sz1 = %d\n"</span>, sz1); <span class="comment">//10</span></span><br><span class="line"> </span><br><span class="line"> test(arr); <span class="comment">//arr是数组名,本质上是数组首元素的地址</span></span><br><span class="line"> <span class="comment">//数组传参的本质,传递的是数组首元素的地址</span></span><br><span class="line"> <span class="comment">//所以形参即使写成数组形式,本质上也是一个指针变量</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>输出的结果:</strong></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/18731/image-7.png" alt="alt text"></p><blockquote><p><strong>我们发现在函数内部是没有正确获得数组的元素个数</strong>。</p></blockquote><blockquote><p>这就要学习数组传参的本质了,上个小节我们学习了:</p></blockquote><blockquote><p>数组名是数组首元素的地址;<br>那么在数组传参的时候,传递的是数组名,也就是说 本质上数组传参传递的是数组首元素的地址 。</p></blockquote><blockquote><p>所以函数形参的部分理论上应该使用指针变量来接收首元素的地址。<br>那么在函数内部我们写sizeof(arr)计算的是一个地址的大小(单位字节)而不是数组的大小(单位字节)。<br>正是因为函数的参数部分是本质是指针,所以在函数内部是没办法求的数组元素个数的。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">test</span><span class="params">(<span class="type">int</span> arr[])</span><span class="comment">//参数写成数组形式,本质上还是指针</span></span><br><span class="line">{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, <span class="keyword">sizeof</span>(arr));</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">test</span><span class="params">(<span class="type">int</span>* arr)</span><span class="comment">//参数写成指针形式</span></span><br><span class="line">{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, <span class="keyword">sizeof</span>(arr));<span class="comment">//计算一个指针变量的大小</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> arr[ <span class="number">10</span> ] = { <span class="number">1</span> , <span class="number">2</span> , <span class="number">3</span> , <span class="number">4</span> , <span class="number">5</span> , <span class="number">6</span> , <span class="number">7</span> , <span class="number">8</span> , <span class="number">9</span> , <span class="number">10</span> };</span><br><span class="line"> test(arr);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><blockquote><p><strong>总结: 一维数组传参,形参的部分可以写成数组的形式,也可以写成指针的形式。</strong></p></blockquote><h1 id="冒泡排序"><a href="#冒泡排序" class="headerlink" title="冒泡排序"></a>冒泡排序</h1><blockquote><p>冒泡排序的核心思想就是:<strong>两两相邻的元素进行比较,不满足顺序就交换位置,满足顺序就找下一个数字</strong>。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">input</span><span class="params">(<span class="type">int</span> *arr, <span class="type">int</span> sz)</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">for</span> (i = <span class="number">0</span>; i < sz; i++)</span><br><span class="line">{</span><br><span class="line"><span class="built_in">scanf</span>(<span class="string">"%d"</span>, arr + i);</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> count = <span class="number">0</span>;</span><br><span class="line"><span class="type">void</span> <span class="title function_">bubble_sort</span><span class="params">(<span class="type">int</span> *arr, <span class="type">int</span> sz)</span></span><br><span class="line">{</span><br><span class="line"><span class="comment">//确定趟数</span></span><br><span class="line"><span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">for</span> (i = <span class="number">0</span>; i < sz - <span class="number">1</span>; i++)</span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> flag = <span class="number">1</span>;<span class="comment">//假设已经满足顺序</span></span><br><span class="line"><span class="comment">//每一趟内部的比较</span></span><br><span class="line"><span class="type">int</span> j = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">for</span> (j = <span class="number">0</span>; j < sz<span class="number">-1</span>-i; j++)</span><br><span class="line">{</span><br><span class="line">count++;</span><br><span class="line"><span class="keyword">if</span> (arr[j] > arr[j + <span class="number">1</span>])</span><br><span class="line">{</span><br><span class="line">flag = <span class="number">0</span>;<span class="comment">//还不是有序</span></span><br><span class="line"><span class="comment">//交换</span></span><br><span class="line"><span class="type">int</span> tmp = arr[j];</span><br><span class="line">arr[j] = arr[j + <span class="number">1</span>];</span><br><span class="line">arr[j + <span class="number">1</span>] = tmp;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="keyword">if</span> (flag == <span class="number">1</span>)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">break</span>;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">print_arr</span><span class="params">(<span class="type">int</span> arr[], <span class="type">int</span> sz)</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">for</span> (i = <span class="number">0</span>; i < sz; i++)</span><br><span class="line">{</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d "</span>, arr[i]);</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> arr[<span class="number">10</span>] = { <span class="number">0</span> };</span><br><span class="line"><span class="comment">//输入一些值</span></span><br><span class="line"><span class="comment">//1 2 3 4 5 6 7 8 9 10</span></span><br><span class="line"><span class="comment">//9 8 7 6 5 4 3 2 1 0</span></span><br><span class="line"><span class="comment">//2 1 9 7 0 3 4 8 5 6 </span></span><br><span class="line"><span class="type">int</span> sz = <span class="keyword">sizeof</span>(arr) / <span class="keyword">sizeof</span>(arr[<span class="number">0</span>]);</span><br><span class="line">input(arr, sz);</span><br><span class="line"></span><br><span class="line"><span class="comment">//排序 - 写一个函数完成数组的排序,排成升序</span></span><br><span class="line">bubble_sort(arr, sz);<span class="comment">//使用冒泡排序</span></span><br><span class="line">print_arr(arr, sz);</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"\ncount = %d\n"</span>, count);</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>画图理解</strong></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/18731/image-8.png" alt="alt text"></p><h1 id="二级指针"><a href="#二级指针" class="headerlink" title="二级指针"></a>二级指针</h1><blockquote><p><strong>什么是二级指针?</strong></p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">char</span>*</span><br><span class="line"><span class="type">int</span>*</span><br><span class="line"><span class="type">double</span>*</span><br><span class="line"><span class="comment">//一级指针</span></span><br></pre></td></tr></table></figure><p><strong>这就是<code>二级指针</code>。</strong></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> a = <span class="number">10</span> ;</span><br><span class="line"> <span class="type">int</span>* pa = &a ;<span class="comment">//pa一级指针变量</span></span><br><span class="line"> <span class="type">int</span>** ppa = &pa ;<span class="comment">//ppa是二级指针变量,指向一级指针</span></span><br><span class="line"></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, **ppa); <span class="comment">//输出10</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">**ppa == *pa == a ==<span class="number">10</span> <span class="comment">//三者等价</span></span><br></pre></td></tr></table></figure><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/18731/image-9.png" alt="alt text"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/18731/image-10.png" alt="alt text"></p><p><code>对于二级指针的运算有:</code></p><ul><li><em>ppa通过对ppa中的地址进行解引用,这样找到的是pa,</em>ppa其实访问的就是pa.</li></ul><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> b = <span class="number">20</span> ;</span><br><span class="line">*ppa = &b;<span class="comment">//等价于 pa = &b;</span></span><br></pre></td></tr></table></figure><ul><li>*<em>ppa先通过</em>ppa找到pa,然后对pa进行解引用操作:*pa,那找到的是a.</li></ul><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">**ppa = <span class="number">30</span> ;</span><br><span class="line"><span class="comment">//等价于*pa = 30;</span></span><br><span class="line"><span class="comment">//等价于a = 30;</span></span><br></pre></td></tr></table></figure><p><strong>总结</strong></p><p><code>二级指针变量是用来存放一级指针变量的地址的。</code></p><h1 id="指针数组"><a href="#指针数组" class="headerlink" title="指针数组"></a>指针数组</h1><blockquote><p><strong>指针数组是指针还是数组?</strong></p></blockquote><p><strong>我们类比一下,整型数组,是存放整型的数组,字符数组是存放字符的数组。</strong></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">char</span> arr[<span class="number">10</span>]; <span class="comment">//字符数组-存储字符的数组</span></span><br><span class="line"><span class="type">int</span> arr[<span class="number">5</span>]; <span class="comment">//整型数组-存储整型的数组</span></span><br><span class="line"></span><br></pre></td></tr></table></figure><blockquote><p><strong>那指针数组呢?</strong></p></blockquote><p><code>指针数组-存放指针的数组,数组的每个元素其实是指针类型,都是用来存放地址的。</code></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">char</span>* arr[<span class="number">5</span>]; <span class="comment">//存放字符指针的数组</span></span><br><span class="line"><span class="type">int</span>* arr[<span class="number">5</span>]; <span class="comment">//存放整型指针的数组</span></span><br></pre></td></tr></table></figure><blockquote><p><strong>是存放指针的数组。</strong></p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/18731/image-11.png" alt="alt text"></p><blockquote><p><strong>指针数组的每个元素都是用来存放地址(指针)的</strong>。</p></blockquote><p><strong>如下图:</strong></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/18731/image-12.png" alt="alt text"></p><p><code>指针数组的每个元素是地址,又可以指向一块区域。</code></p><blockquote><p><strong>通过一个简单的例子来说明:</strong></p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> a = <span class="number">10</span>;</span><br><span class="line"> <span class="type">int</span> b = <span class="number">20</span>;</span><br><span class="line"> <span class="type">int</span> c = <span class="number">30</span>;</span><br><span class="line"> </span><br><span class="line"> <span class="comment">//使用指针变量,这样太麻烦了,所以使用指针数组</span></span><br><span class="line"> <span class="comment">//int* p1 = &a;</span></span><br><span class="line"> <span class="comment">//int* p2 = &b;</span></span><br><span class="line"> <span class="comment">//int* p3 = &c;</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">//使用指针数组 </span></span><br><span class="line"> <span class="type">int</span>* arr[<span class="number">3</span>] = { &a, &b, &c }; <span class="comment">//存放3个整型指针的数组</span></span><br><span class="line"> <span class="comment">// 0 1 2 //通过下标进行指针解引用</span></span><br><span class="line"></span><br><span class="line"> <span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(i = <span class="number">0</span>; i < <span class="number">3</span>; i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d "</span>, *(arr[i])); <span class="comment">//解引用,输出地址的值</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>*<strong>输出结果:</strong></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">10</span> <span class="number">20</span> <span class="number">30</span></span><br></pre></td></tr></table></figure><p><code>说明:</code></p><ol><li><p><strong>指针数组的每个元素是地址,解引用可以得到地址的值。</strong></p></li><li><p><strong>指针数组的本质就是存放指针的数组,数组的每个元素都是指针类型,都是用来存放地址的。</strong></p></li><li><p><strong>指针数组的每个元素都是地址,又可以指向一块区域。</strong></p></li></ol><h1 id="指针数组模拟二维数组"><a href="#指针数组模拟二维数组" class="headerlink" title="指针数组模拟二维数组"></a>指针数组模拟二维数组</h1><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> arr1[<span class="number">5</span>] = { <span class="number">1</span> , <span class="number">2</span> , <span class="number">3</span> , <span class="number">4</span> , <span class="number">5</span> }; <span class="comment">//定义3个整型一维数组</span></span><br><span class="line"> <span class="type">int</span> arr2[<span class="number">5</span>] = { <span class="number">2</span> , <span class="number">3</span> , <span class="number">4</span> , <span class="number">5</span> , <span class="number">6</span> };</span><br><span class="line"> <span class="type">int</span> arr3[<span class="number">5</span>] = { <span class="number">3</span> , <span class="number">4</span> , <span class="number">5</span> , <span class="number">6</span> , <span class="number">7</span> };</span><br><span class="line"> <span class="comment">//arr1 arr2 arr3 是三个数组的数组名,表示三个数组的首元素的地址</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">//数组名是数组首元素的地址,类型是int*的,就可以存放在arr数组中</span></span><br><span class="line"> <span class="type">int</span>* arr[ <span class="number">3</span> ] = {arr1, arr2, arr3};</span><br><span class="line"> <span class="type">int</span> i = <span class="number">0</span> ;</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">for</span>(i= <span class="number">0</span> ; i< <span class="number">3</span> ; i++) <span class="comment">//通过for循环,访问三个数组的数组名</span></span><br><span class="line"> {</span><br><span class="line"> <span class="type">int</span> j = <span class="number">0</span> ;</span><br><span class="line"> <span class="keyword">for</span>(j= <span class="number">0</span> ; j< <span class="number">5</span> ; j++) <span class="comment">//通过for循环,访问三个数组中某个数组的元素</span></span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d "</span>, arr[i][j]); <span class="comment">//*(*(arr+i)+j)</span></span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>输出结果:</strong></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span> <span class="number">2</span> <span class="number">3</span> <span class="number">4</span> <span class="number">5</span></span><br><span class="line"><span class="number">2</span> <span class="number">3</span> <span class="number">4</span> <span class="number">5</span> <span class="number">6</span></span><br><span class="line"><span class="number">3</span> <span class="number">4</span> <span class="number">5</span> <span class="number">6</span> <span class="number">7</span></span><br></pre></td></tr></table></figure><p><code>画图理解:</code></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/18731/image-13.png" alt="alt text"></p><blockquote><p><strong>下面是对上述<code>第10~20行</code>代码的解释:</strong></p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/18731/image-14.png" alt="arr1 arr2 arr3 是三个数组的数组名,表示三个数组的首元素的地址"></p><blockquote><p><strong><code>arr[i]</code>是<code>访问arr数组的元素</code>,<code>arr[i]</code>找到的数组元素<code>指向了整型一维数组</code>,<code>arr[i][j]</code>就是<code>整型一维数组中的元素</code>。</strong></p></blockquote><blockquote><p><strong>上述的代码模拟出二维数组的效果,实际上并非完全是二维数组,因为每一行并非是连续的。</strong></p></blockquote><blockquote><p><strong>完</strong></p></blockquote>]]></content>
<categories>
<category> C语言程序设计 </category>
</categories>
<tags>
<tag> C语言程序设计知识点总结 </tag>
</tags>
</entry>
<entry>
<title>第8章习题-15</title>
<link href="/posts/39958/"/>
<url>/posts/39958/</url>
<content type="html"><![CDATA[<blockquote><p><strong>正文开始</strong></p></blockquote><blockquote><p><strong>题目描述:</strong></p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/39958/image.png" alt="alt text"></p><h1 id="解题思想:"><a href="#解题思想:" class="headerlink" title="解题思想:"></a>解题思想:</h1><p><strong>4个学生5门课程:其实就是一个4x5的矩阵,每一行代表一个学生的5门成绩,float arr[4][5]就可以表示这个矩阵。</strong></p><blockquote><p><strong>第1小题</strong></p></blockquote><p><strong>1. 求第1门课程的平均分:其实就是每个学生的第0门(通过下标进行访问)成绩总和除以4</strong></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(arr[<span class="number">0</span>][<span class="number">0</span>]+arr[<span class="number">1</span>][<span class="number">0</span>]+arr[<span class="number">2</span>][<span class="number">0</span>]+arr[<span class="number">3</span>][<span class="number">0</span>])/<span class="number">4</span> <span class="comment">//结果要求为float类型(双精度浮点数),例如:85.5</span></span><br></pre></td></tr></table></figure><blockquote><p><strong>第2小题</strong></p></blockquote><p><strong>2. 2门课程不及格的同学,输出学号以及全部成绩,以及平均成绩</strong></p><ol><li><p>遍历每个学生的每门成绩,统计小于60分的课程数量,大于2则符合要求</p></li><li><p>输出学号(第n行的下标)</p></li><li><p>输出所有成绩(编写一个成绩输出函数)</p></li><li><p>输出平均成绩(编写一个计算平均成绩的函数,传递一行数据(子数组的地址)),进行总和相加除以5,保留两位小数</p></li></ol><blockquote><p><strong>第3小题</strong></p></blockquote><p><strong>3. 平均成绩90分以上,或者全部成绩在85分以上,输出优秀学生学号</strong></p><ol><li>遍历每个学生的成绩的同时,计算学生的平均成绩,计算成绩在85分以上的课程数量,平均成绩>90 ||(或) course_count ==5(假设每门课程成绩>85分)</li></ol><h1 id="代码呈现:"><a href="#代码呈现:" class="headerlink" title="代码呈现:"></a>代码呈现:</h1><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 函数用于统计指定课程的成绩平均值</span></span><br><span class="line"><span class="comment">// 参数: </span></span><br><span class="line"><span class="comment">// arr - 学生成绩二维数组的指针</span></span><br><span class="line"><span class="comment">// row - 行数(学生数量)</span></span><br><span class="line"><span class="comment">// col - 列数(课程数量)</span></span><br><span class="line"><span class="comment">// course_id - 需要计算平均分的课程编号</span></span><br><span class="line"><span class="comment">// 返回值:</span></span><br><span class="line"><span class="comment">// 指定课程的平均成绩</span></span><br><span class="line"><span class="type">float</span> <span class="title function_">course_avg</span><span class="params">(<span class="type">float</span> (*arr)[<span class="number">5</span>], <span class="type">int</span> row, <span class="type">int</span> col, <span class="type">int</span> course_id)</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">float</span> sum = <span class="number">0</span>;</span><br><span class="line"> <span class="comment">// 遍历所有学生在指定课程的成绩并累加</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i < row; i++)</span><br><span class="line"> {</span><br><span class="line"> sum += *(*(arr + i) + course_id); <span class="comment">// arr[i][course_id]</span></span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 计算平均分并返回</span></span><br><span class="line"> <span class="keyword">return</span> (sum / row);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 函数用于计算给定数组的平均值</span></span><br><span class="line"><span class="comment">// 参数:</span></span><br><span class="line"><span class="comment">// score - 成绩数组的指针</span></span><br><span class="line"><span class="comment">// sz - 数组元素个数</span></span><br><span class="line"><span class="comment">// 返回值:</span></span><br><span class="line"><span class="comment">// 数组元素的平均值</span></span><br><span class="line"><span class="type">float</span> <span class="title function_">stu_avg</span><span class="params">(<span class="type">float</span> *score, <span class="type">int</span> sz)</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">float</span> sum = <span class="number">0</span>;</span><br><span class="line"> <span class="comment">// 遍历数组累加所有成绩</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i < sz; i++)</span><br><span class="line"> {</span><br><span class="line"> sum += *(score + i); <span class="comment">// 相当于 score[i]</span></span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 计算平均值并返回</span></span><br><span class="line"> <span class="keyword">return</span> (sum / sz);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 函数用于打印学生的所有成绩</span></span><br><span class="line"><span class="comment">// 参数:</span></span><br><span class="line"><span class="comment">// score - 指向学生单行成绩数组的指针</span></span><br><span class="line"><span class="comment">// sz - 该学生成绩的数量(本例中为5,代表5门课程)</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">print_stu_score</span><span class="params">(<span class="type">float</span> *score, <span class="type">int</span> sz)</span></span><br><span class="line">{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"此学生的所有成绩如下:"</span>);</span><br><span class="line"> <span class="comment">// 打印每个成绩,保留两位小数</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i < sz; i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%.2f "</span>, *(score + i)); <span class="comment">// 相当于 score[i]</span></span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 函数用于输出不及格学生的信息</span></span><br><span class="line"><span class="comment">// 参数:</span></span><br><span class="line"><span class="comment">// arr - 学生成绩二维数组的指针</span></span><br><span class="line"><span class="comment">// row - 学生数量</span></span><br><span class="line"><span class="comment">// col - 课程数量</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">fail</span><span class="params">(<span class="type">float</span> (*arr)[<span class="number">5</span>], <span class="type">int</span> row, <span class="type">int</span> col)</span></span><br><span class="line">{</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i < row; i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="type">int</span> fail_count = <span class="number">0</span>;</span><br><span class="line"> <span class="comment">// 统计不及格课程数量</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> j = <span class="number">0</span>; j < col; j++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span> (*(*(arr + i) + j) < <span class="number">60</span>)</span><br><span class="line"> {</span><br><span class="line"> fail_count++;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 如果不及格课程超过2门,则打印该学生信息</span></span><br><span class="line"> <span class="keyword">if</span> (fail_count > <span class="number">2</span>)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d 号学生,有两门以上课程不及格\n"</span>, i);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"这个学生的平均成绩是:%f\n"</span>, stu_avg(*(arr + i), col)); <span class="comment">// 计算并打印该生平均成绩</span></span><br><span class="line"> print_stu_score(*(arr + i), col); <span class="comment">// 打印该生所有成绩</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 函数用于输出优秀学生的信息</span></span><br><span class="line"><span class="comment">// 参数:</span></span><br><span class="line"><span class="comment">// arr - 学生成绩二维数组的指针</span></span><br><span class="line"><span class="comment">// row - 学生数量</span></span><br><span class="line"><span class="comment">// col - 课程数量</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">excellent</span><span class="params">(<span class="type">float</span> (*arr)[<span class="number">5</span>], <span class="type">int</span> row, <span class="type">int</span> col)</span></span><br><span class="line">{</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i < row; i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="type">int</span> course_count = <span class="number">0</span>;</span><br><span class="line"> <span class="type">float</span> num = <span class="number">0</span>;</span><br><span class="line"> <span class="comment">// 统计90分以上课程数量及总成绩</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> j = <span class="number">0</span>; j < col; j++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span> (*(*(arr + i) + j) > <span class="number">85</span>)</span><br><span class="line"> {</span><br><span class="line"> course_count++;</span><br><span class="line"> }</span><br><span class="line"> num += arr[i][j]; <span class="comment">// 累加成绩</span></span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 如果所有课程均高于90分或至少一门课程高于85分,认为是优秀学生</span></span><br><span class="line"> <span class="keyword">if</span> (num / col > <span class="number">90</span> || course_count == col)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d 号学生,真是好学生!\n"</span>, i);</span><br><span class="line"> print_stu_score(*(arr + i), col); <span class="comment">// 打印该生所有成绩</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span> </span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 初始化一个4x5的学生成绩数组</span></span><br><span class="line"> <span class="type">float</span> score[<span class="number">4</span>][<span class="number">5</span>] = {</span><br><span class="line"> {<span class="number">32</span>, <span class="number">48</span>, <span class="number">58</span>, <span class="number">36</span>, <span class="number">75</span>},</span><br><span class="line"> {<span class="number">98</span>, <span class="number">70</span>, <span class="number">99</span>, <span class="number">100</span>, <span class="number">90</span>},</span><br><span class="line"> {<span class="number">87</span>, <span class="number">88</span>, <span class="number">89</span>, <span class="number">86</span>, <span class="number">87</span>},</span><br><span class="line"> {<span class="number">68</span>, <span class="number">98</span>, <span class="number">75</span>, <span class="number">78</span>, <span class="number">82</span>},</span><br><span class="line"> };</span><br><span class="line"> <span class="comment">// 计算并输出第1门课程的平均分</span></span><br><span class="line"> <span class="type">float</span> cour_avg_score = course_avg(score, <span class="number">4</span>, <span class="number">5</span>, <span class="number">0</span>);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"第1门课程的平均分:%.2f\n"</span>, cour_avg_score);</span><br><span class="line"> <span class="comment">// 输出不及格学生的信息</span></span><br><span class="line"> fail(score, <span class="number">4</span>, <span class="number">5</span>);</span><br><span class="line"> <span class="comment">// 输出优秀学生的信息</span></span><br><span class="line"> excellent(score, <span class="number">4</span>, <span class="number">5</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>运行结果如下:</strong></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/39958/image-1.png" alt="alt text"></p><blockquote><p><strong>完</strong></p></blockquote>]]></content>
<categories>
<category> C语言程序设计 </category>
</categories>
<tags>
<tag> C语言程序设计课后习题 </tag>
</tags>
</entry>
<entry>
<title>第8章习题-1</title>
<link href="/posts/6117/"/>
<url>/posts/6117/</url>
<content type="html"><![CDATA[<blockquote><p><strong>正文开始</strong></p></blockquote><blockquote><p><strong>题目描述:</strong></p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/6117/image.png" alt="alt text"></p><h1 id="解题思想:"><a href="#解题思想:" class="headerlink" title="解题思想:"></a>解题思想:</h1><p><strong>只需要进行数字间的两两比较,完成数据的有序输出</strong></p><h1 id="解题流程:"><a href="#解题流程:" class="headerlink" title="解题流程:"></a>解题流程:</h1><p><strong>1. 捕捉(定义)三个整数,num1、num2、num3;</strong></p><p><strong>2. 将num1与num2进行比较,将较小的值存储到num1中;</strong></p><p><strong>3. 将num1与num3进行比较,将较小的值存储到num1中,这时候num1中存储的就是三个整数中最小的那个值</strong>;</p><p><strong>4. 将num2与num3进行比较,将较小的值存储到num2中,这时候num2就是次小值。</strong></p><h1 id="代码呈现:"><a href="#代码呈现:" class="headerlink" title="代码呈现:"></a>代码呈现:</h1><h2 id="使用常规方法处理"><a href="#使用常规方法处理" class="headerlink" title="使用常规方法处理"></a>使用常规方法处理</h2><p><strong>代码如下:</strong></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> _CRT_SECURE_NO_WARNINGS <span class="comment">//宏定义,防止VS出现不安全的警告</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span> <span class="comment">//标准输入输出库</span></span></span><br><span class="line"><span class="comment">//#include <stdlib.h> //system的头文件</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> num1, num2, num3; <span class="comment">//定义三个整数</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"请输入三个整数:"</span>); <span class="comment">//提示输入</span></span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d %d %d"</span>, &num1, &num2, &num3); <span class="comment">//输入三个整数</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> (num1 > num2) <span class="comment">//比较num1与num2,将较小的数存入num1</span></span><br><span class="line"> {</span><br><span class="line"> <span class="type">int</span> temp = num1; <span class="comment">//定义一个中间变量temp,用于临时存储num1的值</span></span><br><span class="line"> num1 = num2;</span><br><span class="line"> num2 = temp;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (num1 > num3) <span class="comment">//比较num1与num3,将较小的数存入num1</span></span><br><span class="line"> {</span><br><span class="line"> <span class="type">int</span> temp = num1; <span class="comment">//定义一个中间变量temp,用于临时存储num1的值</span></span><br><span class="line"> num1 = num3;</span><br><span class="line"> num3 = temp;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (num2 > num3) <span class="comment">//比较num2与num3,将较小的数存入num2</span></span><br><span class="line"> {</span><br><span class="line"> <span class="type">int</span> temp = num2; <span class="comment">//定义一个中间变量temp,用于临时存储num2的值</span></span><br><span class="line"> num2 = num3;</span><br><span class="line"> num3 = temp;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"三个整数的排序结果为:%d %d %d"</span>, num1, num2, num3); <span class="comment">//输出三个整数的排序结果</span></span><br><span class="line"> <span class="comment">//system("pause"); //暂停程序,等待用户输入(可要可不要,可以把它注释掉),低版本编译器可能会出现一闪而过的黑框,使用这个解决。</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>运行结果如下:</strong><br><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/6117/image-3.png" alt="alt text"></p><h2 id="使用指针处理"><a href="#使用指针处理" class="headerlink" title="使用指针处理"></a>使用指针处理</h2><blockquote><p><strong>指针的基本概念:</strong></p></blockquote><p><strong>数据都是存储在内存中的,指针就是内存地址</strong><br><strong>如果我们有了一块内存地址,就可以通过这个地址去访问这块内存中的数据</strong></p><p><strong>总结:指针就是内存地址,通过指针可以访问内存中的数据。</strong></p><blockquote><p><strong>指针变量(用于保存内存地址):</strong></p></blockquote><p><strong>指针类型的变量可以保存一个内存地址,它可以用来访问内存中的数据</strong></p><p><strong>例如:</strong></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a=<span class="number">10</span>; <span class="comment">//定义一个整型变量a,并赋值为10</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> *p=&a; <span class="comment">//定义一个指针变量p,并将a的内存地址赋值给p</span></span><br></pre></td></tr></table></figure><p><strong>画图理解:</strong></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/6117/image-1.png" alt="alt text"></p><blockquote><p><strong>指针解引用:</strong></p></blockquote><p><strong>指针解引用就是通过指针变量去访问它指向的内存地址中的数据</strong></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a=<span class="number">10</span>; <span class="comment">//定义一个整型变量a,并赋值为10</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> *p=&a; <span class="comment">//定义一个指针变量p,并将a的内存地址赋值给p</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> b=*p; <span class="comment">//通过指针p去访问a的内存地址中的数据,并将数据赋值给b</span></span><br></pre></td></tr></table></figure><blockquote><p><strong>解题思路:</strong></p></blockquote><ol><li><p>定义三个整型指针变量 *pmin(小值指针), *pmax(大值指针), *pmid(中间值指针)</p></li><li><p>输入三个整数,并将三个指针变量指向这三个整数的内存地址</p></li><li><p>利用指针变量,比较三个整数,将较小的数存入pmin中,将较大的数存入pmax中,将中间的数存入pmid中</p></li><li><p>输出三个整数的排序结果</p></li></ol><p><strong>画图理解:</strong></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/6117/image-2.png" alt="alt text"></p><p><strong>代码如下:</strong></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> _CRT_SECURE_NO_WARNINGS <span class="comment">//宏定义,防止VS出现不安全的警告</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span> <span class="comment">//标准输入输出库</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> num1, num2, num3; <span class="comment">//定义三个整数</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"请输入三个整数:"</span>); <span class="comment">//提示输入</span></span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d %d %d"</span>, &num1, &num2, &num3); <span class="comment">//输入三个整数</span></span><br><span class="line"></span><br><span class="line"> <span class="type">int</span> *pmin = &num1; </span><br><span class="line"> <span class="type">int</span> *pmid = &num2; </span><br><span class="line"> <span class="type">int</span> *pmax = &num3; <span class="comment">//定义三个指针变量</span></span><br><span class="line"> </span><br><span class="line"> <span class="keyword">if</span> (*pmin > *pmid) <span class="comment">//指针解引用,*pmin与*pmid指向交换,也就是地址的交换,所以这里比较的是值</span></span><br><span class="line"> {</span><br><span class="line"> <span class="type">int</span> *temp = pmin; <span class="comment">//定义一个中间指针变量temp,用于临时存储*pmin的地址</span></span><br><span class="line"> pmin = pmid;</span><br><span class="line"> pmid = temp;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (*pmin > *pmax) <span class="comment">//指针解引用,*pmin与*pmax指向交换,也就是地址的交换,所以这里比较的是值</span></span><br><span class="line"> {</span><br><span class="line"> <span class="type">int</span> *temp = pmin; <span class="comment">//定义一个中间指针变量temp,用于临时存储pmin的地址</span></span><br><span class="line"> pmin = pmax;</span><br><span class="line"> pmax = temp;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (*pmid > *pmax) <span class="comment">//指针解引用,*pmid与*pmax指向交换,也就是地址的交换,所以这里比较的是值</span></span><br><span class="line"> {</span><br><span class="line"> <span class="type">int</span> *temp = pmid; <span class="comment">//定义一个中间指针变量temp,用于临时存储pmid的地址</span></span><br><span class="line"> pmid = pmax;</span><br><span class="line"> pmax = temp;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"三个整数的排序结果为:%d %d %d"</span>, *pmin, *pmid, *pmax); <span class="comment">//输出三个整数的排序结果</span></span><br><span class="line"> <span class="comment">//system("pause"); //暂停程序,等待用户输入(可要可不要,可以把它注释掉),低版本编译器可能会出现一闪而过的黑框,使用这个解决。</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>运行结果如下:</strong><br><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/6117/image-4.png" alt="alt text"></p><blockquote><p><strong>完</strong></p></blockquote>]]></content>
<categories>
<category> C语言程序设计 </category>
</categories>
<tags>
<tag> C语言程序设计课后习题 </tag>
</tags>
</entry>
<entry>
<title>第10讲:操作符详解</title>
<link href="/posts/2933/"/>
<url>/posts/2933/</url>
<content type="html"><![CDATA[<h1 id="目录"><a href="#目录" class="headerlink" title="目录"></a>目录</h1><ol><li><p><strong>操作符的分类</strong></p></li><li><p><strong>二进制和进制转换</strong></p></li><li><p><strong>原码、反码、补码</strong></p></li><li><p><strong>移位操作符</strong></p></li><li><p><strong>位操作符:&、|、^、~</strong></p></li><li><p><strong>单目操作符</strong></p></li><li><p><strong>逗号表达式</strong></p></li><li><p><strong>下标访问[]、函数调用()</strong></p></li><li><p><strong>结构成员访问操作符</strong></p></li><li><p><strong>操作符的属性:优先级、结合性</strong></p></li><li><p><strong>表达式求值</strong></p></li></ol><blockquote><p><strong>正文开始</strong></p></blockquote><hr><h1 id="操作符的分类"><a href="#操作符的分类" class="headerlink" title="操作符的分类"></a>操作符的分类</h1><p>• 算术操作符:**+ 、- 、* 、/ 、%**</p><p>• 移位操作符:<strong><< >> //移动的是二进制位</strong></p><p>• 位操作符:<strong>& | ^ //位操作符是对二进制位进行计算</strong></p><p>• 赋值操作符:**= 、+= 、 -= 、 <em>= 、 /= 、%= 、<<= 、>>= 、&= 、|= 、^=</em>*</p><p>• 单目操作符:<strong>!、++、–、&、*、+、-、~ 、sizeof、(类型)</strong></p><p>• 关系操作符:<strong>> 、>= 、< 、<= 、 == 、^ !=</strong></p><p>• 逻辑操作符:**&& 、||**</p><p>• 条件操作符:**? :**</p><p>• 逗号表达式:**,**</p><p>• 下标引用:**[]**</p><p>• 函数调用:**()**</p><p>• 结构成员访问:**. 、->**</p><blockquote><p>上述的操作符,我们已经讲过算术操作符、赋值操作符、逻辑操作符、条件操作符和部分的单目操作符,今天继续介绍一部分,操作符中有一些操作符和二进制有关系,我们先铺垫一下二进制的和进制转换的知识。</p></blockquote><h1 id="二进制和进制转换"><a href="#二进制和进制转换" class="headerlink" title="二进制和进制转换"></a>二进制和进制转换</h1><blockquote><p>其实我们经常能听到<strong>2进制</strong>、 <strong>8 进制</strong>、 <strong>10 进制</strong>、<strong>16</strong> 进制这样的讲法,那是什么意思呢?</p></blockquote><p><strong>其实 2 进制、 8 进制、 10 进制、 16 进制是数值的不同表示形式而已。</strong></p><p><strong>比如:数值 15 的各种进制的表示形式:</strong></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">15</span> 的 <span class="number">2</span> 进制: <span class="number">1111</span></span><br><span class="line"><span class="number">15</span> 的 <span class="number">8</span> 进制: <span class="number">17</span></span><br><span class="line"><span class="number">15</span> 的 <span class="number">10</span> 进制: <span class="number">15</span></span><br><span class="line"><span class="number">15</span> 的 <span class="number">16</span> 进制:F</span><br><span class="line"><span class="comment">//16进制的数值之前写:0x</span></span><br><span class="line"><span class="comment">//8进制的数值之前写: 0</span></span><br></pre></td></tr></table></figure><blockquote><p><strong>2进制</strong>的数字每一位都是0~1的数字组成<br>1111——>1<em>2^0 + 1</em>2^1 + 1<em>2^2 + 1</em>2^3 = 15</p></blockquote><blockquote><p><strong>8进制</strong>的数字每一位是0~7的数字组成<br>1111——>1<em>8^0 + 1</em>8^1 + 1<em>8^2 + 1</em>8^3 = 17</p></blockquote><blockquote><p><strong>10进制</strong>的数字每一位是0~9的数字组成<br>1111——>1<em>10^0 + 1</em>10^1 + 1<em>10^2 + 1</em>10^3 = 15</p></blockquote><blockquote><p><strong>16进制</strong>的数字每一位是0<del>9,a</del>f的数字组成<br>1111——>1<em>16^0 + 1</em>16^1 + 1<em>16^2 + 1</em>16^3 = F</p></blockquote><h2 id="2进制-转-10进制"><a href="#2进制-转-10进制" class="headerlink" title="2进制 转 10进制"></a>2进制 转 10进制</h2><blockquote><p>其实 10 进制的 123 表示的值是一百二十三,为什么是这个值呢?其实 10 进制的每一位是有 <strong>权重</strong>的, 10进制的数字从右向左是个位、十位、百位….,分别每一位的权重是10^0 ,10^1 ,10^2 …</p></blockquote><p>如下图:<br><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/2933/image.png" alt="alt text"></p><p><strong>10进制123 每一位权重的理解</strong></p><blockquote><p>2 进制和 10 进制是类似的,只不过 2 进制的每一位的权重,从右向左是: 2^0 , 2^1 , 2^2 …<br><strong>如果是 2 进制的 1101 ,该怎么理解呢?</strong></p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/2933/image-1.png" alt="alt text"></p><p><strong>2 进制 1101 每一位权重的理解</strong></p><h3 id="10进制转-2进制数字"><a href="#10进制转-2进制数字" class="headerlink" title="10进制转 2进制数字"></a>10进制转 2进制数字</h3><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/2933/image-2.png" alt="alt text"></p><p><strong>10 进制转 2 进制</strong></p><h2 id="2-进制转-8-进制和-16-进制"><a href="#2-进制转-8-进制和-16-进制" class="headerlink" title="2 进制转 8 进制和 16 进制"></a>2 进制转 8 进制和 16 进制</h2><h3 id="2-进制转-8-进制"><a href="#2-进制转-8-进制" class="headerlink" title="2 进制转 8 进制"></a>2 进制转 8 进制</h3><blockquote><p>8 进制的数字每一位是0<del>7的,0</del>7的数字,各自写成 2 进制,最多有 3 个 2 进制位就足够了,比如 7 的二进制是 <strong>111</strong> ,所以在 2 进制转 8 进制数的时候,从 2 进制序列中右边低位开始向左每 3 个 2 进制位会换算一个 8 进制位,剩余不够 3 个 2 进制位的直接换算。</p></blockquote><p>如: 2进制的<strong>01101011</strong>,换成 8 进制:<strong>0153</strong> , <strong>0 开头的数字,会被当做 8 进制。</strong></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/2933/image-3.png" alt="alt text"></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> primntf(<span class="string">"%d\n"</span>, <span class="number">153</span>);</span><br><span class="line"> primntf(<span class="string">"%d\n"</span>, <span class="number">0153</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>运行结果:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">153</span><br><span class="line">107</span><br></pre></td></tr></table></figure><p><strong>153的8进制:3<em>8^0 + 5</em>8^1 + 3*8^2 = 107</strong></p><h4 id="2-进制转-16-进制"><a href="#2-进制转-16-进制" class="headerlink" title="2 进制转 16 进制"></a>2 进制转 16 进制</h4><blockquote><p>16 进制的数字每一位是0<del>9,a</del>f的,0<del>9,a</del>f的数字,各自写成 2 进制,最多有 4 个 2 进制位就足够了,比如<strong>f</strong>的二进制是 <strong>1111</strong> ,所以在 2 进制转 16 进制数的时候,从 2 进制序列中右边低位开始向左每 4 个 2 进制位会换算一个 16 进制位,剩余不够 4 个二进制位的直接换算。</p></blockquote><p>如: <strong>2</strong> 进制的 <strong>01101011</strong> ,换成 16 进制:<strong>0x6b</strong>, 16 进制表示的时候前面加<strong>0x</strong></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/2933/image-4.png" alt="alt text"></p><h1 id="原码、反码、补码"><a href="#原码、反码、补码" class="headerlink" title="原码、反码、补码"></a>原码、反码、补码</h1><p>当你们要把一个数转换成2进制表示的时候,<strong>整数的</strong>2进制表示方法有三种,即<strong>原码、反码和补码</strong></p><p><strong>有符号整数</strong> 的三种表示方法均有 <strong>符号位</strong> 和 <strong>数值位</strong> 两部分, 2 进制序列中,<strong>最高位的 1 位</strong>是被当做<strong>符号位</strong>,<strong>剩余的都是数值位</strong>。</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/2933/image-5.png" alt="alt text"></p><p><strong>符号位都是用 0 表示“正”,用 1 表示“负”。</strong></p><blockquote><p><strong>正整数的原、反、补码都相同。</strong></p></blockquote><blockquote><p><strong>负整数的三种表示方法各不相同。</strong></p></blockquote><p><strong>原码</strong>: 直接将数值按照正负数的形式翻译成二进制得到的就是原码。</p><p><strong>反码</strong>: 将原码的符号位不变,其他位依次按位取反就可以得到反码。</p><p><strong>补码</strong>: 反码+1就得到补码。<br>补码得到原码也是可以使用:<strong>取反,+1的操作。</strong></p><p><strong>图解:</strong><br><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/2933/image-6.png" alt="alt text"></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> b = <span class="number">10</span>; <span class="comment">//正整数的原、反、补码都相同。</span></span><br><span class="line"><span class="comment">//00000000000000000000000000001010-- 原码</span></span><br><span class="line"><span class="comment">//00000000000000000000000000001010-- 反码</span></span><br><span class="line"><span class="comment">//00000000000000000000000000001010-- 补码</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> a = <span class="number">-10</span>; <span class="comment">//原码: 直接将数值按照正负数的形式翻译成二进制得到的就是原码。 反码: 将原码的符号位不变,其他位依次按位取反就可以得到反码。 补码: 反码+1就得到补码。</span></span><br><span class="line"> <span class="comment">//10000000000000000000000000001010 -- 原码</span></span><br><span class="line"><span class="comment">//11111111111111111111111111110101 -- 反码</span></span><br><span class="line"><span class="comment">//11111111111111111111111111110110 -- 补码 //满2进1</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//11111111111111111111111111110110 -- 补码</span></span><br><span class="line"><span class="comment">//10000000000000000000000000001001</span></span><br><span class="line"><span class="comment">//10000000000000000000000000001010 -- 原码</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">//整数在内存中存储的是2进制的补码</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"><span class="comment">////1 - 1;</span></span><br><span class="line"><span class="comment">////1 + (-1)</span></span><br><span class="line"><span class="comment">////尝试用原码计算</span></span><br><span class="line"><span class="comment">////000000000000000000000000000000001 1的原码</span></span><br><span class="line"><span class="comment">////100000000000000000000000000000001 -1的原码</span></span><br><span class="line"><span class="comment">//// </span></span><br><span class="line"><span class="comment">////100000000000000000000000000000010 -2 //err</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">////尝试使用补码计算</span></span><br><span class="line"><span class="comment">////000000000000000000000000000000001 1的补码</span></span><br><span class="line"><span class="comment">////100000000000000000000000000000001 -1的原码</span></span><br><span class="line"><span class="comment">////111111111111111111111111111111110</span></span><br><span class="line"><span class="comment">////111111111111111111111111111111111 -1的补码</span></span><br><span class="line"><span class="comment">////000000000000000000000000000000001</span></span><br><span class="line"><span class="comment">////000000000000000000000000000000000 -- 0</span></span><br><span class="line"><span class="comment">////</span></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><blockquote><p>对于整形来说:数据存放内存中其实存放的是<strong>补码</strong>。</p></blockquote><blockquote><p><strong>为什么呢?</strong></p></blockquote><p><strong>在计算机系统中,数值一律用补码来表示和存储。原因在于,使用补码,可以将符号位和数值域统一处理;同时,加法和减法也可以统一处理( CPU只有加法器 )此外,补码与原码相互转换,其运算过程是相同的,不需要额外的硬件电路。</strong></p><h1 id="移位操作符"><a href="#移位操作符" class="headerlink" title="移位操作符"></a>移位操作符</h1><p><<左移操作符</p><p>.>>右移操作符</p><blockquote><p><strong>注:移位操作符的操作数只能是整数。</strong></p></blockquote><h2 id="左移操作符"><a href="#左移操作符" class="headerlink" title="左移操作符"></a>左移操作符</h2><p>移位规则:左边抛弃、右边补 0 </p><p><strong>整数例子</strong></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> a = <span class="number">10</span>;</span><br><span class="line"><span class="type">int</span> b = a << <span class="number">1</span>; </span><br><span class="line"><span class="comment">//int b = a << 1;</span></span><br><span class="line"><span class="comment">//10</span></span><br><span class="line"><span class="comment">//00000000000000000000000000001010</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"b = %d\n"</span>, b);</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"a = %d\n"</span>, a);</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>运行结果:</strong></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">b=20</span><br><span class="line">a=10</span><br></pre></td></tr></table></figure><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> a = <span class="number">10</span>;</span><br><span class="line">a = a << <span class="number">1</span>; <span class="comment">//a <<= 1;</span></span><br><span class="line"><span class="comment">//int b = a << 1;</span></span><br><span class="line"><span class="comment">//10</span></span><br><span class="line"><span class="comment">//00000000000000000000000000001010</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">//printf("b = %d\n", b);</span></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"a = %d\n"</span>, a);</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>运行结果:</strong></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">a=20</span><br></pre></td></tr></table></figure><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/2933/image-7.png" alt="alt text"></p><p><strong>负数例子</strong></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> a = <span class="number">-1</span>;</span><br><span class="line"><span class="comment">//10000000000000000000000000000001 //原码</span></span><br><span class="line"><span class="comment">//11111111111111111111111111111110 //反码</span></span><br><span class="line"><span class="comment">//11111111111111111111111111111111 //补码</span></span><br><span class="line"><span class="comment">// </span></span><br><span class="line"><span class="comment">//11111111111111111111111111111110 //b的补码</span></span><br><span class="line"><span class="comment">//10000000000000000000000000000001 //反码</span></span><br><span class="line"><span class="comment">//10000000000000000000000000000010 //原码</span></span><br><span class="line"><span class="type">int</span> b = a << <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"a=%d\n"</span>, b);<span class="comment">//-2</span></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"b=%d\n"</span>, a);<span class="comment">//-1</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>运行结果</strong></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">a=<span class="number">-1</span></span><br><span class="line">b=<span class="number">-2</span></span><br></pre></td></tr></table></figure><p><strong>左移操作符演示</strong></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/2933/image-8.png" alt="alt text"></p><h2 id="右移操作符"><a href="#右移操作符" class="headerlink" title="右移操作符"></a>右移操作符</h2><p>移位规则:<code>首先右移运算分两种</code>:</p><ol><li><p>逻辑右移:<code>左边用 0 填充,右边丢弃</code></p></li><li><p>算术右移:<code>左边用原该值的符号位填充,右边丢弃</code></p></li></ol><blockquote><p>右移到底采用算术右移还是逻辑右移?</p></blockquote><ol><li><p><strong>取决于编译器</strong></p></li><li><p><strong>通常采用算术右移</strong></p></li></ol><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/2933/image-9.png" alt="alt text"></p><blockquote><p><code>注意</code>:<strong>右移操作符的操作数只能是整数,不能是浮点数</strong>。</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/2933/image-10.png" alt="alt text"></p><p><code>举例1</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> a = <span class="number">-10</span>;</span><br><span class="line"> </span><br><span class="line"> <span class="comment">//10000000000000000000000000001010 //原码</span></span><br><span class="line"> <span class="comment">//11111111111111111111111111110101 //反码</span></span><br><span class="line"> <span class="comment">//11111111111111111111111111110110 //补码</span></span><br><span class="line"> <span class="comment">//</span></span><br><span class="line"> <span class="type">int</span> b = a >> <span class="number">1</span>; <span class="comment">//算术右移</span></span><br><span class="line"> <span class="comment">//11111111111111111111111111111011 //b的补码</span></span><br><span class="line"> <span class="comment">//10000000000000000000000000000100 //反码</span></span><br><span class="line"> <span class="comment">//10000000000000000000000000000101 //原码</span></span><br><span class="line"> <span class="comment">//</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"a = %d\n"</span>, b);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"b = %d\n"</span>, a);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>运行结果</strong></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="number">-10</span></span><br><span class="line">b = <span class="number">-5</span></span><br></pre></td></tr></table></figure><p><code>举例2</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> num = <span class="number">10</span> ;</span><br><span class="line"> <span class="type">int</span> n = num>> <span class="number">1</span> ;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"n= %d\n"</span>, n);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"num= %d\n"</span>, num);</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/2933/image-11.png" alt="alt text"></p><p><strong>逻辑右移 1 位演示</strong></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/2933/image-12.png" alt="alt text"></p><p><strong>算术右移 1 位演示</strong></p><blockquote><p>警告⚠:<strong>对于移位运算符,不要移动负数位,这个是标准未定义的</strong>。</p></blockquote><p><code>例如</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> num = <span class="number">10</span> ;</span><br><span class="line"></span><br><span class="line">num>><span class="number">-1</span>;<span class="comment">//error</span></span><br></pre></td></tr></table></figure><h1 id="位操作符:-、-、-、"><a href="#位操作符:-、-、-、" class="headerlink" title="位操作符:&、|、^、~"></a>位操作符:&、|、^、~</h1><p><strong>位操作符有</strong>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">& <span class="comment">//按位与</span></span><br><span class="line">| <span class="comment">//按位或</span></span><br><span class="line">^ <span class="comment">//按位异或</span></span><br><span class="line">~ <span class="comment">//按位取反</span></span><br></pre></td></tr></table></figure><p><code>注</code>:<strong>他们的操作数必须是整数。</strong></p><p>**<code>举例</code>**:</p><p><code>按位与</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> a = <span class="number">6</span>;</span><br><span class="line"> <span class="comment">//00000000000000000000000000000110 ——> 6的补码</span></span><br><span class="line"> <span class="comment">//原码和反码与补码相同</span></span><br><span class="line"></span><br><span class="line"> <span class="type">int</span> b = <span class="number">-7</span>;</span><br><span class="line"> <span class="comment">//10000000000000000000000000000111 //b的原码</span></span><br><span class="line"> <span class="comment">//11111111111111111111111111111000 //b的反码</span></span><br><span class="line"> <span class="comment">//11111111111111111111111111111001 ——> -7的补码</span></span><br><span class="line"></span><br><span class="line"> <span class="type">int</span> c = a & b; <span class="comment">//按位与: a和b的补码的二进制位进行运算</span></span><br><span class="line"> <span class="comment">//对应的二进制位规则:有0则为0,两个同时为1,才为1</span></span><br><span class="line"> <span class="comment">//</span></span><br><span class="line"> <span class="comment">//00000000000000000000000000000110 ——> 6的补码</span></span><br><span class="line"> <span class="comment">//11111111111111111111111111111001 ——> -7的补码</span></span><br><span class="line"> <span class="comment">//00000000000000000000000000000000 ——> c的补码, 结果为 0,0的原码和补码与补码相同</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,c);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>运行结果</strong>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">0</span></span><br></pre></td></tr></table></figure><p><code>按位或</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> a = <span class="number">6</span>;</span><br><span class="line"> <span class="comment">//00000000000000000000000000000110 ——> 6的补码</span></span><br><span class="line"> <span class="comment">//原码和反码与补码相同</span></span><br><span class="line"></span><br><span class="line"> <span class="type">int</span> b = <span class="number">-7</span>;</span><br><span class="line"> <span class="comment">//10000000000000000000000000000111 //b的原码</span></span><br><span class="line"> <span class="comment">//11111111111111111111111111111000 //b的反码</span></span><br><span class="line"> <span class="comment">//11111111111111111111111111111001 ——> -7的补码</span></span><br><span class="line"> <span class="comment">//</span></span><br><span class="line"> <span class="type">int</span> c = a | b; <span class="comment">//按位或: a和b的补码的二进制位进行运算</span></span><br><span class="line"> <span class="comment">//规则:只要有1,就是1,两个同时为0,才为0</span></span><br><span class="line"> <span class="comment">//</span></span><br><span class="line"> <span class="comment">//00000000000000000000000000000110 ——> 6的补码</span></span><br><span class="line"> <span class="comment">//11111111111111111111111111111001 ——> -7的补码</span></span><br><span class="line"> <span class="comment">//11111111111111111111111111111111 ——> c的补码,结果为 -1</span></span><br><span class="line"> <span class="comment">//10000000000000000000000000000000 ——> -1的反码</span></span><br><span class="line"> <span class="comment">//10000000000000000000000000000001 ——> -1的原码</span></span><br><span class="line"> <span class="comment">//</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,c);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>运行结果</strong>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">-1</span></span><br></pre></td></tr></table></figure><p><code>按位异或</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> a = <span class="number">6</span>;</span><br><span class="line"> <span class="comment">//0000000000000000000000000000110 ——> 6的补码</span></span><br><span class="line"> <span class="comment">//原码和反码与补码相同</span></span><br><span class="line"></span><br><span class="line"> <span class="type">int</span> b = <span class="number">-7</span>;</span><br><span class="line"> <span class="comment">//10000000000000000000000000000111 //b的原码</span></span><br><span class="line"> <span class="comment">//11111111111111111111111111111000 //b的反码</span></span><br><span class="line"> <span class="comment">//11111111111111111111111111111001 ——> -7的补码</span></span><br><span class="line"> <span class="comment">//</span></span><br><span class="line"> <span class="type">int</span> c = a ^ b; <span class="comment">//按位异或: a和b的补码的二进制位进行运算</span></span><br><span class="line"> <span class="comment">//规则:对应的二进制位,相同为0,相异为1</span></span><br><span class="line"> <span class="comment">//</span></span><br><span class="line"> <span class="comment">//00000000000000000000000000000110 ——> 6的补码</span></span><br><span class="line"> <span class="comment">//11111111111111111111111111111001 ——> -7的补码</span></span><br><span class="line"> <span class="comment">//11111111111111111111111111111111 ——> c的补码,结果为 -1</span></span><br><span class="line"> <span class="comment">//10000000000000000000000000000000 ——> -1的反码</span></span><br><span class="line"> <span class="comment">//10000000000000000000000000000001 ——> -1的原码</span></span><br><span class="line"> <span class="comment">//</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,c);<span class="comment">//-1</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>运行结果</strong>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">-1</span></span><br></pre></td></tr></table></figure><p><code>按位取反</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> a = <span class="number">0</span>;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,~a); <span class="comment">//-1</span></span><br><span class="line"> <span class="comment">//~ 按(2进制)位取反</span></span><br><span class="line"> <span class="comment">//00000000000000000000000000000000 ——> 0的补码</span></span><br><span class="line"> <span class="comment">//11111111111111111111111111111111 ——> -1的补码</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">//10000000000000000000000000000000 ——> -1的反码</span></span><br><span class="line"> <span class="comment">//10000000000000000000000000000001 ——> -1的原码</span></span><br><span class="line"> </span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>运行结果</strong>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">-1</span></span><br></pre></td></tr></table></figure><p><code>直接上代码</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> num1 = <span class="number">-3</span>;</span><br><span class="line"> <span class="type">int</span> num2 = <span class="number">5</span> ;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, num1 & num2);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, num1 | num2);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, num1 ^ num2);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, ~ <span class="number">0</span> );</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">//10000000000000000000000000000011 ——> -3的原码</span></span><br><span class="line"><span class="comment">//11111111111111111111111111111100 ——> -3的反码</span></span><br><span class="line"><span class="comment">//11111111111111111111111111111101 ——> -3的补码</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//00000000000000000000000000000101 ——> 5的原码,反码,补码</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//num1 & num2 ——> 按位与: a和b的补码的二进制位进行运算</span></span><br><span class="line"><span class="comment">//规则:有0则为0,两个同时为1,才为1</span></span><br><span class="line"> <span class="comment">//11111111111111111111111111111101 ——> -3的补码</span></span><br><span class="line"> <span class="comment">//00000000000000000000000000000101 ——> 5的补码</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">//00000000000000000000000000000101 ——> 结果为5 ——> 补码,反码,补码相同</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//num1 | num2 ——> 按位或: a和b的补码的二进制位进行运算</span></span><br><span class="line"><span class="comment">//规则:只要有1,就是1,两个同时为0,才为0</span></span><br><span class="line"> <span class="comment">//11111111111111111111111111111101 ——> -3的补码</span></span><br><span class="line"> <span class="comment">//00000000000000000000000000000101 ——> 5的补码</span></span><br><span class="line"> <span class="comment">//11111111111111111111111111111101 补码</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">//11111111111111111111111111111100 反码</span></span><br><span class="line"> <span class="comment">//10000000000000000000000000000011 原码,结果为 -3</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//num1 ^ num2——> 按位异或: a和b的补码的二进制位进行运算</span></span><br><span class="line"><span class="comment">//规则:相同为0,相异为1</span></span><br><span class="line"> <span class="comment">//11111111111111111111111111111101 ——> -3的补码</span></span><br><span class="line"> <span class="comment">//00000000000000000000000000000101 ——> 5的补码</span></span><br><span class="line"> <span class="comment">//11111111111111111111111111111000 补码</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">//11111111111111111111111111110111 反码</span></span><br><span class="line"> <span class="comment">//10000000000000000000000000001000 原码,结果为8</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//~ 按(2进制)位取反</span></span><br><span class="line"> <span class="comment">//00000000000000000000000000000000 ——> 0的补码</span></span><br><span class="line"> <span class="comment">//11111111111111111111111111111111 ——> -1的补码</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">//10000000000000000000000000000000 ——> -1的反码</span></span><br><span class="line"> <span class="comment">//10000000000000000000000000000001 ——> -1的原码</span></span><br></pre></td></tr></table></figure><p><strong>运行结果</strong>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">5</span></span><br><span class="line"><span class="number">-3</span></span><br><span class="line"><span class="number">-8</span></span><br><span class="line"><span class="number">-1</span></span><br></pre></td></tr></table></figure><p><code>一道变态的面试题</code>:</p><blockquote><p><code>不能创建临时变量(第三个变量),实现两个整数的交换</code>。</p></blockquote><p><code>举例1</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> a = <span class="number">3</span>;</span><br><span class="line"><span class="type">int</span> b = <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> c = <span class="number">0</span>;<span class="comment">//空瓶</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"交换前:a=%d b=%d\n"</span>, a, b);</span><br><span class="line">c = a;</span><br><span class="line">a = b;</span><br><span class="line">b = c;</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"交换后:a=%d b=%d\n"</span>, a, b);</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>运行结果</strong>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">交换前:a=<span class="number">3</span> b=<span class="number">5</span></span><br><span class="line">交换后:a=<span class="number">5</span> b=<span class="number">3</span></span><br></pre></td></tr></table></figure><p><code>举例2</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> a = <span class="number">3</span>;</span><br><span class="line"><span class="type">int</span> b = <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"交换前:a=%d b=%d\n"</span>, a, b);</span><br><span class="line"></span><br><span class="line">a = a + b;</span><br><span class="line">b = a - b;</span><br><span class="line">a = a - b;</span><br><span class="line"></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"交换后:a=%d b=%d\n"</span>, a, b);</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>运行结果</strong>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">交换前:a=<span class="number">3</span> b=<span class="number">5</span></span><br><span class="line">交换后:a=<span class="number">5</span> b=<span class="number">3</span></span><br></pre></td></tr></table></figure><blockquote><p><strong>为什么不能创建临时变量?</strong></p></blockquote><p><code>因为临时变量的生命周期只在当前语句块内,而在当前语句块结束后,临时变量就会被销毁,导致结果错误</code>。</p><blockquote><p><strong>使用异或运算符实现两个整数的交换</strong>:</p></blockquote><p><code>理解使用异或运算符实现两个整数的交换的用法</code>:</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/2933/image-13.png" alt="alt text"></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> a = <span class="number">3</span>;</span><br><span class="line"><span class="type">int</span> b = <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"交换前:a=%d b=%d\n"</span>, a, b);</span><br><span class="line"></span><br><span class="line">a = a ^ b;</span><br><span class="line">b = a ^ b;</span><br><span class="line">a = a ^ b;</span><br><span class="line"></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"交换后:a=%d b=%d\n"</span>, a, b);</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>运行结果</strong>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">交换前:a=<span class="number">3</span> b=<span class="number">5</span></span><br><span class="line">交换后:a=<span class="number">5</span> b=<span class="number">3</span></span><br></pre></td></tr></table></figure><h2 id="练习-1-:-编写代码实现:求一个整数存储在内存中的二进制中-1-的个数。"><a href="#练习-1-:-编写代码实现:求一个整数存储在内存中的二进制中-1-的个数。" class="headerlink" title="练习 1 : 编写代码实现:求一个整数存储在内存中的二进制中 1 的个数。"></a>练习 1 : 编写代码实现:求一个整数存储在内存中的二进制中 1 的个数。</h2><p><code>举例1</code>:</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/2933/image-16.png" alt="alt text"></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">count_bit_one</span><span class="params">(<span class="type">unsigned</span> <span class="type">int</span> n)</span><span class="comment">//unsigned int 无符号整型</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> count = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">while</span> (n)<span class="comment">//当 n 非 0 时,执行循环</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span> ((n % <span class="number">2</span>) == <span class="number">1</span>)</span><br><span class="line">count++;</span><br><span class="line"></span><br><span class="line"> n = n / <span class="number">2</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> count;</span><br><span class="line">}</span><br><span class="line"><span class="comment">//00000000000000000000000000001111</span></span><br><span class="line"><span class="comment">//10000000000000000000000000000001</span></span><br><span class="line"><span class="comment">//11111111111111111111111111111110</span></span><br><span class="line"><span class="comment">//11111111111111111111111111111111</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> num = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">scanf</span>(<span class="string">"%d"</span>, &num);<span class="comment">//-1</span></span><br><span class="line"><span class="type">int</span> ret = count_bit_one(num);</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d\n"</span>, ret);</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>运行结果</strong>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">15</span> <span class="comment">//——>输入</span></span><br><span class="line"><span class="number">4</span></span><br></pre></td></tr></table></figure><p><code>举例2</code>:</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/2933/image-14.png" alt="alt text"></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">count_bit_one</span><span class="params">(<span class="type">int</span> n)</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"><span class="type">int</span> count = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">for</span> (i = <span class="number">0</span>; i < <span class="number">32</span>; i++)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span> (((n >> i) & <span class="number">1</span>) == <span class="number">1</span>)</span><br><span class="line">{</span><br><span class="line">count++;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="keyword">return</span> count;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> num = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">scanf</span>(<span class="string">"%d"</span>, &num);<span class="comment">//-1</span></span><br><span class="line"><span class="type">int</span> ret = count_bit_one(num);</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d\n"</span>, ret);</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>运行结果</strong>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">15</span> <span class="comment">//——>输入</span></span><br><span class="line"><span class="number">4</span></span><br></pre></td></tr></table></figure><p><code>举例3</code>:</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/2933/image-15.png" alt="alt text"></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">count_bit_one</span><span class="params">(<span class="type">int</span> n)</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> count = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">while</span> (n)</span><br><span class="line">{</span><br><span class="line">count++;</span><br><span class="line">n = n & (n - <span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="keyword">return</span> count;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> num = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">scanf</span>(<span class="string">"%d"</span>, &num);<span class="comment">//-1</span></span><br><span class="line"><span class="type">int</span> ret = count_bit_one(num);</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d\n"</span>, ret);</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>运行结果</strong>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">15</span> <span class="comment">//——>输入</span></span><br><span class="line"><span class="number">4</span></span><br></pre></td></tr></table></figure><blockquote><p><code>分析</code>:</p></blockquote><ul><li><p>方法 1:循环求余,判断余数是否为 1 ,是则计数器加 1 ,然后除以 2 ,直到 n 为 0 。</p></li><li><p>方法 2:循环 32 次,判断第 i 位是否为 1 ,是则计数器加 1 。</p></li><li><p>方法 3:循环求余,判断余数是否为 1 ,是则计数器加 1 ,然后与 n 进行位与运算,去掉最后一位,直到 n 为 0 。</p></li></ul><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line">参考代码:</span><br><span class="line"><span class="comment">// 方法 1</span></span><br><span class="line"><span class="comment">// 主函数,计算一个整数的二进制表示中 1 的个数</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> num = <span class="number">10</span> ;</span><br><span class="line"> <span class="type">int</span> count= <span class="number">0</span> ; <span class="comment">// 计数</span></span><br><span class="line"> <span class="keyword">while</span>(num)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(num % <span class="number">2</span> == <span class="number">1</span> )</span><br><span class="line"> count++;</span><br><span class="line"> num = num / <span class="number">2</span> ;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"二进制中 1 的个数 = %d\n"</span>, count);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 思考这样的实现方式有没有问题?</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 方法 2</span></span><br><span class="line"><span class="comment">// 主函数,计算一个整数的二进制表示中 1 的个数</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> num = <span class="number">-1</span>;</span><br><span class="line"> <span class="type">int</span> i = <span class="number">0</span> ;</span><br><span class="line"> <span class="type">int</span> count = <span class="number">0</span> ; <span class="comment">// 计数</span></span><br><span class="line"> <span class="keyword">for</span>(i= <span class="number">0</span> ; i< <span class="number">32</span> ; i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>( num & ( <span class="number">1</span> << i) )</span><br><span class="line"> count++;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"二进制中 1 的个数 = %d\n"</span>,count);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 思考还能不能更加优化,这里必须循环 32 次的。</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 方法 3</span></span><br><span class="line"><span class="comment">// 主函数,计算一个整数的二进制表示中 1 的个数</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> num = <span class="number">-1</span>;</span><br><span class="line"> <span class="type">int</span> i = <span class="number">0</span> ;</span><br><span class="line"> <span class="type">int</span> count = <span class="number">0</span> ; <span class="comment">// 计数</span></span><br><span class="line"> <span class="keyword">while</span>(num)</span><br><span class="line"> {</span><br><span class="line"> count++;</span><br><span class="line"> num = num & (num - <span class="number">1</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"二进制中 1 的个数 = %d\n"</span>,count);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br><span class="line"><span class="comment">// 这种方式是不是很好?达到了优化的效果,但是难以想到。</span></span><br></pre></td></tr></table></figure><h2 id="练习-2-:-二进制位置-0-或者置-1"><a href="#练习-2-:-二进制位置-0-或者置-1" class="headerlink" title="练习 2 : 二进制位置 0 或者置 1"></a>练习 2 : 二进制位置 0 或者置 1</h2><p>编写代码将 13 二进制序列的第 5 位修改为 1 ,然后再改回 0 </p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">13</span> 的 <span class="number">2</span> 进制序列: <span class="number">00000000000000000000000000001101</span></span><br><span class="line">将第 <span class="number">5</span> 位置为 <span class="number">1</span> 后: <span class="number">00000000000000000000000000011101</span></span><br><span class="line">将第 <span class="number">5</span> 位再置为 <span class="number">0</span> : <span class="number">00000000000000000000000000001101</span></span><br></pre></td></tr></table></figure><p>参考代码:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> a = <span class="number">13</span>;</span><br><span class="line"><span class="comment">//00000000000000000000000000001101</span></span><br><span class="line"><span class="comment">//00000000000000000000000000010000</span></span><br><span class="line"> <span class="comment">//00000000000000000000000000000001</span></span><br><span class="line"><span class="comment">// </span></span><br><span class="line"><span class="comment">//00000000000000000000000000011101</span></span><br><span class="line"><span class="comment">//11111111111111111111111111101111</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">//00000000000000000000000000000001</span></span><br><span class="line"><span class="type">int</span> n = <span class="number">5</span>;</span><br><span class="line">a = a | (<span class="number">1</span> << (n - <span class="number">1</span>));</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d\n"</span>, a);</span><br><span class="line"></span><br><span class="line">a &= ~(a << (n - <span class="number">1</span>));</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d\n"</span>, a);</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 改进版:</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> a = <span class="number">13</span> ;</span><br><span class="line"><span class="comment">//00000000000000000000000000001101</span></span><br><span class="line"><span class="comment">//00000000000000000000000000010000</span></span><br><span class="line"> <span class="comment">//00000000000000000000000000000001</span></span><br><span class="line"><span class="comment">// </span></span><br><span class="line"><span class="comment">//00000000000000000000000000011101</span></span><br><span class="line"><span class="comment">//11111111111111111111111111101111</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">//00000000000000000000000000000001</span></span><br><span class="line"> </span><br><span class="line"> a = a | ( <span class="number">1</span> << <span class="number">4</span> );</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"a = %d\n"</span>, a);</span><br><span class="line"></span><br><span class="line"> a = a & ~( <span class="number">1</span> << <span class="number">4</span> );</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"a = %d\n"</span>, a);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h1 id="单目操作符"><a href="#单目操作符" class="headerlink" title="单目操作符"></a>单目操作符</h1><p>单目操作符有这些:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">!、++、--、&、*、+、-、~ 、<span class="keyword">sizeof</span>、(类型)</span><br></pre></td></tr></table></figure><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/2933/image-17.png" alt="alt text"></p><blockquote><p>单目操作符的特点是<code>只有一个操作数</code>,在单目操作符中只有 **&**和 * 没有介绍,这 2 个操作符,我们放在学习指针的时候学习。</p></blockquote><h1 id="逗号表达式"><a href="#逗号表达式" class="headerlink" title="逗号表达式"></a>逗号表达式</h1><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">exp1, exp2, exp3, ...expN</span><br></pre></td></tr></table></figure><blockquote><p>逗号表达式,就是用逗号隔开的多个表达式。</p></blockquote><blockquote><p>逗号表达式,<code>从左向右</code><strong>依次执行</strong>。<code>整个表达式的结果是最后一个表达式的结果</code>。</p></blockquote><blockquote><p><code>注意</code>:逗号表达式,一定要从左向右依次执行,因为前面的表达式的计算,可能会影响后面的表达式的计算。</p></blockquote><p><strong><code>代码1</code></strong>:</p><blockquote><p>定义变量 a、b 并使用逗号表达式给变量 c 赋值<br>逗号表达式中依次执行 a>b、a=b+10、a、b=a+1,最后 c 被赋值为 b 的值,即 13</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> a = <span class="number">1</span>;</span><br><span class="line"><span class="type">int</span> b = <span class="number">2</span>;</span><br><span class="line"><span class="type">int</span> c = (a > b, a = b + <span class="number">10</span>, a, b = a + <span class="number">1</span>);<span class="comment">//逗号表达式,一定要从左向右依次执行</span></span><br><span class="line"> <span class="comment">// 执行 a > b, 1 > 2,表达式为假,结果为0</span></span><br><span class="line"> <span class="comment">// 执行 a = b + 10, a = 12</span></span><br><span class="line"> <span class="comment">// 执行 a, 结果为12</span></span><br><span class="line"> <span class="comment">// 执行 b = a + 1, b = 13</span></span><br><span class="line"> <span class="comment">// 最后 c = 13</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d\n"</span>, c);</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>运行结果</strong>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">13</span></span><br></pre></td></tr></table></figure><p><strong><code>代码2</code></strong>:</p><blockquote><p>使用逗号表达式在条件语句中赋值,但应注意逗号表达式的返回值为最后一个表达式的值</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 主函数:程序入口</span></span><br><span class="line"></span><br><span class="line"> <span class="type">int</span> a = <span class="number">0</span>;</span><br><span class="line"> <span class="type">int</span> b = <span class="number">0</span>;</span><br><span class="line"> <span class="type">int</span> c = <span class="number">0</span>;</span><br><span class="line"> <span class="type">int</span> d = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> (a = b + <span class="number">1</span>, c = a / <span class="number">2</span>, d > <span class="number">0</span>)<span class="comment">//从左向右依次执行,但主要看最后一个表达式的结果</span></span><br><span class="line"> <span class="comment">// 执行 a = b + 1, a = 1</span></span><br><span class="line"> <span class="comment">// 执行 c = a / 2, c = 0</span></span><br><span class="line"> <span class="comment">// 执行 d > 0, 表达式为假,结果为0</span></span><br><span class="line"> <span class="comment">// 最后结果为 0</span></span><br><span class="line"> {</span><br><span class="line"> <span class="comment">// 如果条件成立则执行以下代码块</span></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong><code>代码3</code></strong>:</p><blockquote><p>调用 get_val() 函数获取 a 的值,并进行业务处理直到 a 不大于 0</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="comment">// 主函数,程序入口</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 获取初始数值</span></span><br><span class="line"> a = get_val();</span><br><span class="line"> <span class="comment">// 统计数值</span></span><br><span class="line"> count_val(a);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 当数值大于 0 时执行循环</span></span><br><span class="line"> <span class="keyword">while</span> (a > <span class="number">0</span>)</span><br><span class="line"> {</span><br><span class="line"> <span class="comment">// 业务处理</span></span><br><span class="line"> <span class="comment">// ...</span></span><br><span class="line"> </span><br><span class="line"> <span class="comment">// 获取新的数值</span></span><br><span class="line"> a = get_val();</span><br><span class="line"> <span class="comment">// 统计数值</span></span><br><span class="line"> count_val(a);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">//改进版:</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="comment">// 主函数,程序入口</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 获取初始数值</span></span><br><span class="line"> a = get_val();</span><br><span class="line"> <span class="comment">// 统计数值</span></span><br><span class="line"> count_val(a);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 使用逗号表达式改写循环条件</span></span><br><span class="line"> <span class="comment">// 当数值大于 0 时执行循环</span></span><br><span class="line"> <span class="keyword">while</span> (a = get_val(), count_val(a), a><span class="number">0</span>)<span class="comment">//从左向右依次执行,但主要看最后一个表达式的结果</span></span><br><span class="line"> {</span><br><span class="line"> <span class="comment">// 业务处理</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h1 id="下标访问-、函数调用"><a href="#下标访问-、函数调用" class="headerlink" title="下标访问[]、函数调用()"></a>下标访问[]、函数调用()</h1><h2 id="下标引用操作符"><a href="#下标引用操作符" class="headerlink" title="[ ]下标引用操作符"></a>[ ]下标引用操作符</h2><p>操作数:一个数组名 <strong>+</strong> 一个索引值(<code>下标</code>)</p><p><code>举例</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> arr[<span class="number">10</span>] = { <span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span> };</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> m = arr[<span class="number">4</span>];<span class="comment">//数组中下标是4的元素</span></span><br><span class="line"><span class="comment">//[] 下标引用操作符 - 操作数是:arr,4</span></span><br><span class="line"><span class="comment">//3+5, + 是操作符,3和5是操作数</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d\n"</span>, m);</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>解读</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> arr[ <span class="number">10</span> ];<span class="comment">//创建数组</span></span><br><span class="line">arr[ <span class="number">4</span> ] = <span class="number">10</span> ;<span class="comment">//实用下标引用操作符。</span></span><br><span class="line">[ ]的两个操作数是arr和 <span class="number">4</span> ,其中arr是数组名,<span class="number">4</span>是索引值。</span><br></pre></td></tr></table></figure><h2 id="函数调用操作符"><a href="#函数调用操作符" class="headerlink" title="函数调用操作符()"></a>函数调用操作符()</h2><p>接受<code>一个或者多个</code>操作数:<code>第一个操作数</code>是<code>函数名</code>,<code>剩余的操作数</code>就是<code>传递给函数的参数</code>。</p><p><code>举例</code>1:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">Add</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">return</span> x + y;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"hehe\n"</span>);<span class="comment">//() 就是函数调用操作符,操作数是:</span></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d\n"</span>, <span class="number">100</span>);</span><br><span class="line"><span class="type">int</span> ret = Add(<span class="number">3</span>, <span class="number">5</span>);<span class="comment">//Add 3 5 </span></span><br><span class="line"><span class="comment">//函数调用操作符最少有几个操作数?</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>举例</code>2:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义第一个测试函数,用于打印 "hehe"</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">test1</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"hehe\n"</span>);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义第二个测试函数,用于打印传入的字符串参数</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">test2</span><span class="params">(<span class="type">const</span> <span class="type">char</span> *str)</span></span><br><span class="line">{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%s\n"</span>, str);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 主函数</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> test1(); <span class="comment">// 这里的()就是作为函数调用操作符。</span></span><br><span class="line"> test2(<span class="string">"hello bit."</span>); <span class="comment">// 这里的()就是函数调用操作符。</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h1 id="结构成员访问操作符"><a href="#结构成员访问操作符" class="headerlink" title="结构成员访问操作符"></a>结构成员访问操作符</h1><h2 id="结构体"><a href="#结构体" class="headerlink" title="结构体"></a>结构体</h2><p>C语言已经提供了内置类型,如:<strong>char、short、int、long、float、double</strong>等,但是只有这些内置类型还是不够的,假设我想描述学生,描述一本书,这时单一的内置类型是不行的。</p><p>描述一个学生需要名字、年龄、学号、身高、体重等;</p><p>描述一本书需要作者、出版社、定价等。C语言为了解决这个问题,增加了结构体这种自定义的数据类<br>型,让程序员可以自己创造适合的类型。</p><p>📌结构是一些值的集合,这些值称为成员变量。结构的每个成员可以是不同类型的变量,如:<br><code>标量</code>、<code>数组</code>、<code>指针</code>,甚至是<code>其他结构体</code>。</p><h3 id="结构的声明"><a href="#结构的声明" class="headerlink" title="结构的声明"></a>结构的声明</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">tag</span> //<span class="keyword">struct</span> ——></span> 结构体关键字, tag ——> 结构体的名称(自定义)</span><br><span class="line">{</span><br><span class="line"> member-<span class="built_in">list</span>;<span class="comment">//成员列表</span></span><br><span class="line">}variable-<span class="built_in">list</span>; <span class="comment">//变量列表</span></span><br></pre></td></tr></table></figure><p><code>描述一个学生</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Stu</span></span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="type">char</span> name[ <span class="number">20</span> ];<span class="comment">//名字</span></span><br><span class="line"><span class="type">int</span> age;<span class="comment">//年龄</span></span><br><span class="line"><span class="type">char</span> sex[ <span class="number">5</span> ];<span class="comment">//性别</span></span><br><span class="line"><span class="type">char</span> id[ <span class="number">20</span> ];<span class="comment">//学号</span></span><br><span class="line">}; <span class="comment">//分号不能丢</span></span><br></pre></td></tr></table></figure><h4 id="结构体变量的定义和初始化"><a href="#结构体变量的定义和初始化" class="headerlink" title="结构体变量的定义和初始化"></a>结构体变量的定义和初始化</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//学生类型</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Student</span></span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="comment">//成员变量</span></span><br><span class="line"><span class="type">char</span> name[<span class="number">20</span>];<span class="comment">//名字</span></span><br><span class="line"><span class="type">int</span> age;<span class="comment">//年龄</span></span><br><span class="line"><span class="type">float</span> score;<span class="comment">//成绩</span></span><br><span class="line">}s4, s5, s6;<span class="comment">//全局变量</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Student</span> <span class="title">s3</span> =</span> {<span class="string">"王五"</span>, <span class="number">25</span>, <span class="number">88.8</span>};<span class="comment">//全局变量</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Point</span>//坐标类型</span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="type">int</span> x;</span><br><span class="line"><span class="type">int</span> y;</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">S</span></span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="type">char</span> ch;<span class="comment">//字符型变量</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Point</span> <span class="title">p</span>;</span><span class="comment">//坐标变量</span></span><br><span class="line"><span class="type">int</span> arr[<span class="number">10</span>];<span class="comment">//数组变量</span></span><br><span class="line"><span class="type">double</span> d;<span class="comment">//浮点型变量</span></span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> a;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Student</span> <span class="title">s1</span> =</span> {<span class="string">"翠花"</span>, <span class="number">20</span>, <span class="number">98.0</span>};<span class="comment">//局部变量</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Student</span> <span class="title">s2</span> =</span> {<span class="string">"旺财"</span>, <span class="number">18</span>, <span class="number">69.8</span>};</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Point</span> <span class="title">p</span> =</span> { <span class="number">10</span>, <span class="number">20</span> };</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">S</span> <span class="title">s</span> =</span> { <span class="string">'a'</span>,{<span class="number">4</span>,<span class="number">5</span>}, {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>},<span class="number">3.14</span> };</span><br><span class="line"></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%c\n"</span>, s.ch);<span class="comment">//打印S的结构体变量ch成员的值</span></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"坐标是:%d %d\n"</span>, s.p.x, s.p.y);</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d\n"</span>, s.arr[<span class="number">0</span>]);</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%lf\n"</span>, s.d);</span><br><span class="line"></span><br><span class="line"><span class="comment">//结构体变量.结构体成员名</span></span><br><span class="line"><span class="comment">//-> 这个是依赖指针的,所以放在后期给大家介绍</span></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 代码 1 :变量的定义</span></span><br><span class="line"><span class="comment">// 结构体类型定义,包括两个整型成员x和y</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Point</span></span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"> <span class="type">int</span> x;</span><br><span class="line"> <span class="type">int</span> y;</span><br><span class="line">} p1; <span class="comment">//声明类型的同时定义变量p</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Point</span> <span class="title">p2</span>;</span> <span class="comment">//定义结构体变量p</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 代码2:初始化。</span></span><br><span class="line"><span class="comment">// 结构体类型Stu包括名字和年龄两个成员</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Stu</span> //类型声明</span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"> <span class="type">char</span> name[<span class="number">15</span>];<span class="comment">//名字</span></span><br><span class="line"> <span class="type">int</span> age; <span class="comment">//年龄</span></span><br><span class="line">};</span><br><span class="line"><span class="comment">// 初始化结构体变量s1,名字为zhangsan,年龄为20</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Stu</span> <span class="title">s1</span> =</span> {<span class="string">"zhangsan"</span>, <span class="number">20</span> };<span class="comment">//初始化</span></span><br><span class="line"><span class="comment">// 按照指定顺序初始化结构体变量s2,年龄为20,名字为lisi</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Stu</span> <span class="title">s2</span> =</span> {.age=<span class="number">20</span>, .name=<span class="string">"lisi"</span>};<span class="comment">//指定顺序初始化</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 代码 3</span></span><br><span class="line"><span class="comment">// 结构体类型Node包括整型成员data,Point类型成员p和指向Node类型的指针成员next</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Node</span></span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"> <span class="type">int</span> data;</span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">Point</span> <span class="title">p</span>;</span></span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">Node</span>* <span class="title">next</span>;</span></span><br><span class="line">} n1 = {<span class="number">10</span>, {<span class="number">4</span>, <span class="number">5</span>}, <span class="literal">NULL</span>}; <span class="comment">//结构体嵌套初始化</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Node</span> <span class="title">n2</span> =</span> {<span class="number">20</span>, {<span class="number">5</span>, <span class="number">6</span>}, <span class="literal">NULL</span>};<span class="comment">//结构体嵌套初始化</span></span><br></pre></td></tr></table></figure><h2 id="结构成员访问操作符-1"><a href="#结构成员访问操作符-1" class="headerlink" title="结构成员访问操作符"></a>结构成员访问操作符</h2><h3 id="结构体成员的直接访问"><a href="#结构体成员的直接访问" class="headerlink" title="结构体成员的直接访问"></a>结构体成员的直接访问</h3><p><code>结构体成员</code>的<code>直接访问</code>是通过点操作符(<code>.</code>)访问的。<strong>点操作符接受两个操作数</strong>。如下所示:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义一个结构体 Point,包括两个整型成员 x 和 y</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Point</span></span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"> <span class="type">int</span> x; <span class="comment">// 横坐标</span></span><br><span class="line"> <span class="type">int</span> y; <span class="comment">// 纵坐标</span></span><br><span class="line">} p = { <span class="number">1</span> , <span class="number">2</span> }; <span class="comment">// 创建一个结构体变量 p,并初始化 x 和 y</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 打印 p 的 x 和 y 值</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"x: %d y: %d\n"</span>, p.x, p.y);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>; <span class="comment">// 返回 0,表示程序执行成功</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>使用方式</strong>: <code>结构体变量.成员名</code></p><h4 id="结构体成员的间接访问"><a href="#结构体成员的间接访问" class="headerlink" title="结构体成员的间接访问"></a>结构体成员的间接访问</h4><p><strong>有时候我们得到的不是一个结构体变量,而是得到了一个指向结构体的指针。</strong>如下所示:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义结构体Point,包含整型变量x和y</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Point</span></span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"> <span class="type">int</span> x;</span><br><span class="line"> <span class="type">int</span> y;</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 创建Point类型的结构体变量p,并初始化x和y的值为3和4</span></span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">Point</span> <span class="title">p</span> =</span> { <span class="number">3</span> , <span class="number">4</span> };</span><br><span class="line"> <span class="comment">// 创建指向Point类型结构体变量p的指针ptr</span></span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">Point</span> *<span class="title">ptr</span> =</span> &p;</span><br><span class="line"> <span class="comment">// 通过指针ptr修改p的成员变量x和y的值为10和20</span></span><br><span class="line"> ptr->x = <span class="number">10</span> ;</span><br><span class="line"> ptr->y = <span class="number">20</span> ;</span><br><span class="line"> <span class="comment">// 打印修改后的x和y的值</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"x = %d y = %d\n"</span>, ptr->x, ptr->y);</span><br><span class="line"> <span class="comment">// 返回0表示程序正常结束</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>使用方式</strong>: <code>结构体指针->成员名</code></p><h2 id="综合举例:"><a href="#综合举例:" class="headerlink" title="综合举例:"></a>综合举例:</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 定义一个结构体Stu,包含名字和年龄两个成员变量</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Stu</span></span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"> <span class="type">char</span> name[ <span class="number">15</span> ]; <span class="comment">// 名字</span></span><br><span class="line"> <span class="type">int</span> age; <span class="comment">// 年龄</span></span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="comment">// 打印结构体Stu变量的名字和年龄</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">print_stu</span><span class="params">(<span class="keyword">struct</span> Stu s)</span></span><br><span class="line">{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%s %d\n"</span>, s.name, s.age);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 设置结构体Stu变量的名字为"李四",年龄为28</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">set_stu</span><span class="params">(<span class="keyword">struct</span> Stu* ps)</span></span><br><span class="line">{</span><br><span class="line"> <span class="built_in">strcpy</span>(ps->name, <span class="string">"李四"</span>);</span><br><span class="line"> ps->age = <span class="number">28</span> ;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 主函数</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 创建一个Stu结构体变量s,并初始化为"张三"和20</span></span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">Stu</span> <span class="title">s</span> =</span> { <span class="string">"张三"</span>, <span class="number">20</span> };</span><br><span class="line"> <span class="comment">// 打印s结构体变量的名字和年龄</span></span><br><span class="line"> print_stu(s);</span><br><span class="line"> <span class="comment">// 调用set_stu函数设置s的名字和年龄</span></span><br><span class="line"> set_stu(&s);</span><br><span class="line"> <span class="comment">// 再次打印s结构体变量的名字和年龄</span></span><br><span class="line"> print_stu(s);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><u>更多关于结构体的知识,后期在《第 19 讲:自定义类型:结构体》中讲解。</u></p><h1 id="操作符的属性:优先级、结合性"><a href="#操作符的属性:优先级、结合性" class="headerlink" title="操作符的属性:优先级、结合性"></a>操作符的属性:优先级、结合性</h1><p><strong>C语言的操作符有 2 个重要的属性:<code>优先级</code>、<code>结合性</code>,这两个属性<code>决定了表达式求值的计算顺序</code>。</strong></p><h2 id="优先级"><a href="#优先级" class="headerlink" title="优先级"></a>优先级</h2><p><strong>优先级指的是,如果一个表达式包含多个运算符,哪个运算符应该优先执行。各种运算符的优先级是不一样的。</strong></p><p><strong>相邻的运算符,优先级<code>由高到低</code>依次是</strong>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> r = (<span class="number">3</span>+<span class="number">4</span>) * <span class="number">5</span>; <span class="comment">// 先计算加法,再计算乘法</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">3</span> + <span class="number">4</span> * <span class="number">5</span> ;<span class="comment">// 先计算乘法,再计算加法</span></span><br></pre></td></tr></table></figure><p><code>上面示例中,表达式3 + 4 * 5里面既有加法运算符(+),又有乘法运算符(*)。由于乘法的优先级高于加法,所以会先计算4 * 5,而不是先计算3 + 4</code>。</p><h2 id="结合性"><a href="#结合性" class="headerlink" title="结合性"></a>结合性</h2><p>**如果<code>两个运算符优先级相同</code>,优先级没办法确定先计算哪个了,这时候就看结合性了,则根据运算符是<code>左结合</code>,还是<code>右结合</code>,决定执行顺序。大部分运算符是<code>左结合(从左到右执行)</code>,少数运算符是<code>右结合(从右到左执行)</code>,<code>比如赋值运算符(=)</code>**。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">5</span> * <span class="number">6</span> / <span class="number">2</span> ;</span><br></pre></td></tr></table></figure><p><code>上面示例中,*和/的优先级相同,它们都是左结合运算符,所以从左到右执行,先计算5 * 6,再计算/ 2</code>。</p><blockquote><p>运算符的优先级顺序很多,下面是部分运算符的优先级顺序(按照优先级从高到低排列),建议大概记住这些操作符的优先级就行,其他操作符在使用的时候查看下面表格就可以了。</p></blockquote><ul><li>圆括号(())</li><li>自增运算符(++),自减运算符(–)</li><li>单目运算符(+和-)</li><li>乘法(*),除法(/)</li><li>加法(+),减法(-)</li><li>关系运算符(<、>等)</li><li>赋值运算符(=)</li></ul><p>由于圆括号的优先级最高,可以使用它改变其他运算符的优先级。</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/2933/image-18.png" alt="alt text"></p><p>参考:<a href="https://zh.cppreference.com/w/c/language/operator_precedence">https://zh.cppreference.com/w/c/language/operator_precedence</a></p><h1 id="表达式求值"><a href="#表达式求值" class="headerlink" title="表达式求值"></a>表达式求值</h1><h2 id="整型提升"><a href="#整型提升" class="headerlink" title="整型提升"></a>整型提升</h2><p>C语言中整型算术运算<code>总是至少以缺省(默认)整型类型的精度</code>来进行的。</p><p>为了获得这个精度,表达式中的<code>字符和短整型</code>操作数在使用之前被<strong>转换</strong>为<code>普通整型</code>,这种转换称为 <strong>整型提升</strong>。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">//int a = 10;</span></span><br><span class="line"> <span class="comment">//int b = 20;</span></span><br><span class="line"> <span class="comment">//int c = a + b;</span></span><br><span class="line"></span><br><span class="line"> <span class="type">char</span> a = <span class="number">20</span>;</span><br><span class="line"> <span class="type">char</span> b = <span class="number">130</span>;</span><br><span class="line"></span><br><span class="line"> <span class="type">char</span> c = a +b; <span class="comment">// 整型提升,a和b被转换为int类型</span></span><br><span class="line"></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, c);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>整型提升的意义</strong> :</p><blockquote><p>表达式的整型运算要在CPU的相应运算器件内执行,CPU内整型运算器(ALU)的操作数的字节⻓度一般就是int的字节⻓度,同时也是CPU的通用寄存器的⻓度。</p></blockquote><blockquote><p>因此,即使两个char类型的相加,在CPU执行时实际上也要先转换为CPU内整型操作数的标准⻓度。</p></blockquote><blockquote><p>通用CPU(general-purposeCPU)是难以直接实现两个 8 比特字节直接相加运算(虽然机器指令中可能有这种字节相加指令)。所以,表达式中各种⻓度可能小于int⻓度的整型值,都必须先转换为int或unsignedint,然后才能送入CPU去执行运算。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//实例 1</span></span><br><span class="line"><span class="type">char</span> a,b,c;</span><br><span class="line">***</span><br><span class="line">a = b + c;</span><br></pre></td></tr></table></figure><p><code>b和c的值被提升为普通整型,然后再执行加法运算</code>。</p><p><code>加法运算完成之后,结果将被截断,然后再存储于a中</code>。</p><blockquote><p><strong>如何进行整体提升呢?</strong></p></blockquote><ol><li><p><strong>有符号整数提升是按照变量的数据类型的符号位来提升的</strong></p></li><li><p><strong>无符号整数提升,高位补 0</strong></p></li></ol><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//负数的整形提升</span></span><br><span class="line"><span class="type">char</span> c1 = <span class="number">-1</span>;</span><br><span class="line">变量c1的二进制位(补码)中只有 <span class="number">8</span> 个比特位:</span><br><span class="line"><span class="number">1111111</span></span><br><span class="line">因为 <span class="type">char</span> 为有符号的 <span class="type">char</span></span><br><span class="line">所以整形提升的时候,高位补充符号位,即为 <span class="number">1</span></span><br><span class="line">提升之后的结果是:</span><br><span class="line"><span class="number">11111111111111111111111111111111</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//正数的整形提升</span></span><br><span class="line"><span class="type">char</span> c2 = <span class="number">1</span> ;</span><br><span class="line">变量c2的二进制位(补码)中只有 <span class="number">8</span> 个比特位:</span><br><span class="line"><span class="number">00000001</span></span><br><span class="line">因为 <span class="type">char</span> 为有符号的 <span class="type">char</span></span><br><span class="line">所以整形提升的时候,高位补充符号位,即为 <span class="number">0</span></span><br><span class="line">提升之后的结果是:</span><br><span class="line"><span class="number">00000000000000000000000000000001</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//无符号整形提升,高位补 0</span></span><br></pre></td></tr></table></figure><h2 id="算术转换"><a href="#算术转换" class="headerlink" title="算术转换"></a>算术转换</h2><p>如果某个操作符的各个操作数属于不同的类型,那么除非其中一个操作数的转换为另一个操作数的类型,否则操作就无法进行。下面的层次体系称为<strong>寻常算术转换</strong>。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">long</span> <span class="type">double</span></span><br><span class="line"><span class="type">double</span></span><br><span class="line"><span class="type">float</span></span><br><span class="line"><span class="type">unsigned</span> <span class="type">long</span> <span class="type">int</span></span><br><span class="line"><span class="type">long</span> <span class="type">int</span></span><br><span class="line"><span class="type">unsigned</span> <span class="type">int</span></span><br><span class="line"><span class="type">int</span></span><br></pre></td></tr></table></figure><p>如果某个操作数的类型在上面这个列表中排名靠后,那么首先要转换为另外一个操作数的类型后执行运算。</p><h2 id="问题表达式解析"><a href="#问题表达式解析" class="headerlink" title="问题表达式解析"></a>问题表达式解析</h2><h3 id="表达式-1"><a href="#表达式-1" class="headerlink" title="表达式 1"></a>表达式 1</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//表达式的求值部分由操作符的优先级决定。</span></span><br><span class="line"><span class="comment">//表达式 1</span></span><br><span class="line">a*b + c*d + e*f</span><br></pre></td></tr></table></figure><p>表达式 1 在计算的时候,由于<em>比+的优先级高,只能保证,</em>的计算是比+早,但是优先级并不能决定第三个*比第一个+早执行。</p><p>所以表达式的计算机顺序就可能是:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">a*b</span><br><span class="line">c*d</span><br><span class="line">a*b + c*d</span><br><span class="line">e*f</span><br><span class="line">a*b + c*d + e*f</span><br></pre></td></tr></table></figure><p>或者</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">a*b</span><br><span class="line">c*d</span><br><span class="line">e*f</span><br><span class="line">a*b + c*d</span><br><span class="line">a*b + c*d + e*f</span><br></pre></td></tr></table></figure><h3 id="表达式-2"><a href="#表达式-2" class="headerlink" title="表达式 2"></a>表达式 2</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//表达式 2</span></span><br><span class="line">c + --c;</span><br></pre></td></tr></table></figure><p>#同上,操作符的优先级只能决定自减–的运算在+的运算的前面,但是我们并没有办法得知,+操作符的左操作数的获取在右操作数之前还是之后求值,所以结果是不可预测的,是有歧义的。</p><h3 id="表达式-3"><a href="#表达式-3" class="headerlink" title="表达式 3"></a>表达式 3</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="comment">//表达式 3</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> i = <span class="number">10</span> ;</span><br><span class="line"> i = i-- - --i * ( i = <span class="number">-3</span> ) * i++ + ++i;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"i = %d\n"</span>, i);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>表达式 3 在不同编译器中测试结果:非法表达式程序的结果</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/2933/image-19.png" alt="alt text"></p><h3 id="表达式-4"><a href="#表达式-4" class="headerlink" title="表达式 4"></a>表达式 4</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">fun</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">static</span> <span class="type">int</span> count = <span class="number">1</span> ;</span><br><span class="line"> <span class="keyword">return</span> ++count;</span><br><span class="line">}</span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> answer;</span><br><span class="line"> answer = fun() - fun() * fun();</span><br><span class="line"> <span class="built_in">printf</span>( <span class="string">"%d\n"</span>, answer);<span class="comment">//输出多少?</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><blockquote><p>这个代码有没有实际的问题?<strong>有问题!</strong></p></blockquote><p>虽然在大多数的编译器上求得结果都是相同的。</p><p>但是上述代码<strong>answer = fun() - fun() * fun()</strong>;中我们只能通过操作符的优先级得知:先<br>算乘法,再算减法。</p><p>函数的调用先后顺序无法通过操作符的优先级确定。</p><h3 id="表达式-5"><a href="#表达式-5" class="headerlink" title="表达式 5 :"></a>表达式 5 :</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//表达式 5</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> i = <span class="number">1</span> ;</span><br><span class="line"><span class="type">int</span> ret = (++i) + (++i) + (++i);</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d\n"</span>, ret);</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d\n"</span>, i);</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br><span class="line"><span class="comment">//尝试在linux 环境gcc编译器,VS2013环境下都执行,看结果。</span></span><br></pre></td></tr></table></figure><p>gcc编译器执行结果:</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/2933/image-20.png" alt="alt text"></p><p>VS2022运行结果:</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/2933/image-21.png" alt="alt text"></p><blockquote><p>看看同样的代码产生了不同的结果,这是为什么?</p></blockquote><p>简单看一下汇编代码,就可以分析清楚.</p><p>这段代码中的第一个+在执行的时候,第三个++是否执行,这个是不确定的,因为依靠操作符的优先级和结合性是无法决定第一个+和第三个前置++的先后顺序。</p><h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>即使有了操作符的优先级和结合性,我们写出的表达式依然有可能不能通过操作符的属性确定唯一的计算路径,那这个表达式就是存在潜在⻛险的,建议不要写出特别复杂的表达式。</p><hr><blockquote><p><strong>完</strong></p></blockquote>]]></content>
<categories>
<category> C语言程序设计 </category>
</categories>
<tags>
<tag> C语言程序设计知识点总结 </tag>
</tags>
</entry>
<entry>
<title>第2讲:C语言数据类型和变量</title>
<link href="/posts/39254/"/>
<url>/posts/39254/</url>
<content type="html"><![CDATA[<h1 id="⽬录"><a href="#⽬录" class="headerlink" title="⽬录"></a>⽬录</h1><ol><li><p>数据类型介绍</p></li><li><p>signed和unsigned</p></li><li><p>数据类型的取值范围</p></li><li><p>变量</p></li><li><p>算术操作符:+、-、*、/、%</p></li><li><p>赋值操作符:=和复合赋值</p></li><li><p>单⽬操作符:++、–、+、-</p></li><li><p>强制类型转换</p></li><li><p>scanf和printf介绍</p></li></ol><blockquote><p><strong>正文开始</strong></p></blockquote><h1 id="数据类型介绍"><a href="#数据类型介绍" class="headerlink" title="数据类型介绍"></a>数据类型介绍</h1><ol><li><p>C语言提供了丰富的<strong>数据类型</strong>来描述生活中的各种数据。</p></li><li><p>使用<strong>整型类型</strong> 来描述整数,使用 <strong>字符类型</strong> 来描述字符,使用 <strong>浮点型类型</strong> 来描述小数。</p></li><li><p>所谓 <strong>“类型”</strong> ,就是相似的数据所拥有的共同特征,编译器只有知道了数据的类型,才知道怎么操作数据。</p></li></ol><blockquote><p><strong>下面盘点一下C语言提供的各种数据类型,本章节主要探讨内置数据类型。</strong></p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/39254/image.png" alt="内置数据类型"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/39254/image-7.png" alt="内置数据类型"></p><blockquote><p>列出C语言中的基本数据类型,并简述它们的用途及输出格式的调用。</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/39254/image-8.png" alt="列出C语言中的基本数据类型,并简述它们的用途及输出格式的调用"></p><h2 id="字符型"><a href="#字符型" class="headerlink" title="字符型"></a>字符型</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">char</span> <span class="comment">//character</span></span><br><span class="line">[<span class="type">signed</span>] <span class="type">char</span> <span class="comment">//有符号的</span></span><br><span class="line"><span class="type">unsigned</span> <span class="type">char</span> <span class="comment">//无符号的</span></span><br></pre></td></tr></table></figure><h2 id="整型"><a href="#整型" class="headerlink" title="整型"></a>整型</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//短整型</span></span><br><span class="line"><span class="type">short</span> [<span class="type">int</span>]</span><br><span class="line">[<span class="type">signed</span>] <span class="type">short</span> [<span class="type">int</span>]</span><br><span class="line"><span class="type">unsigned</span> <span class="type">short</span> [<span class="type">int</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment">//整型</span></span><br><span class="line"><span class="type">int</span></span><br><span class="line">[<span class="type">signed</span>] <span class="type">int</span></span><br><span class="line"><span class="type">unsigned</span> <span class="type">int</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//⻓整型</span></span><br><span class="line"><span class="type">long</span> [<span class="type">int</span>]</span><br><span class="line">[<span class="type">signed</span>] <span class="type">long</span> [<span class="type">int</span>]</span><br><span class="line"><span class="type">unsigned</span> <span class="type">long</span> [<span class="type">int</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment">//更⻓的整型</span></span><br><span class="line"><span class="comment">//C99中引入</span></span><br><span class="line"><span class="comment">//长整型</span></span><br><span class="line"><span class="type">long</span> <span class="type">long</span> [<span class="type">int</span>]</span><br><span class="line">[<span class="type">signed</span>] <span class="type">long</span> <span class="type">long</span> [<span class="type">int</span>]</span><br><span class="line"><span class="type">unsigned</span> <span class="type">long</span> <span class="type">long</span> [<span class="type">int</span>]</span><br></pre></td></tr></table></figure><h2 id="浮点型"><a href="#浮点型" class="headerlink" title="浮点型"></a>浮点型</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">float</span> <span class="comment">//单精度浮点型</span></span><br><span class="line"><span class="type">double</span> <span class="comment">//双精度浮点型</span></span><br><span class="line"><span class="type">long</span> <span class="type">double</span> <span class="comment">//长双精度浮点型</span></span><br></pre></td></tr></table></figure><h2 id="布尔类型"><a href="#布尔类型" class="headerlink" title="布尔类型"></a>布尔类型</h2><p>表示真假的变量,只有两个取值:<strong>true</strong> 和 <strong>false</strong>。整数 0 表示假(false),非零值(包括负数)表示真(true)。</p><blockquote><blockquote><p>注:<strong>C89</strong>中没有布尔类型,在<strong>C99</strong>中引入了<strong>布尔类型</strong>,是专⻔表示真假的。</p></blockquote></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">_Bool</span></span><br></pre></td></tr></table></figure><p>布尔类型的使用得包含头文件 <strong><stdbool.h></strong> .</p><p>布尔类型变量的取值是:<strong>true</strong> 或者 <strong>false</strong> .</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> bool _Bool</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> false 0</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> true 1</span></span><br></pre></td></tr></table></figure><p><code>代码演示</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdbool.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">bool</span> flag = <span class="literal">true</span>; <span class="comment">//将true定义为1,false定义为0</span></span><br><span class="line"> <span class="keyword">if</span> (flag) <span class="comment">//判断flag是否为真</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"i like C\n"</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>运行结果</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">i like C</span><br></pre></td></tr></table></figure><h2 id="各种数据类型的⻓度"><a href="#各种数据类型的⻓度" class="headerlink" title="各种数据类型的⻓度"></a>各种数据类型的⻓度</h2><blockquote><p>每一种数据类型都有自己的⻓度,使用不同的数据类型,能够创建出⻓度不同的变量,变量⻓度的不<br>同,存储的数据范围就有所差异。</p></blockquote><h3 id="sizeof运算符"><a href="#sizeof运算符" class="headerlink" title="sizeof运算符"></a>sizeof运算符</h3><ol><li><p>sizeof是一个关键字,也是操作符,专⻔是用来计算sizeof的操作符数的类型⻓度的,单位是字节。</p></li><li><p>sizeof操作符的操作数可以是类型,也可是变量或者表达式。</p></li></ol><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">sizeof</span>( 类型名 )</span><br><span class="line"><span class="keyword">sizeof</span>( 类型 )</span><br><span class="line"><span class="keyword">sizeof</span> 表达式</span><br></pre></td></tr></table></figure><p><code>sizeof</code>的操作数如果不是类型,是表达式的时候,可以省略掉后边的括号的。</p><p><code>sizeof</code>后边的表达式是不真实参与运算的,根据表达式的类型来得出大小。</p><p><code>sizeof</code>的计算结果是<code>size_t</code>类型的。</p><blockquote><p>📌<code>sizeof</code>运算符的返回值,C语言只规定是无符号整数,并没有规定具体的类型,而是留给<br>系统自己去决定,<code>sizeof</code>到底返回什么类型。不同的系统中,返回值的类型有可能是<br><code>unsigned int</code>,也有可能是<code>unsigned long</code>,甚至是<code>unsigned long long</code>,<br>对应的<code>printf()</code>占位符分别是<code>%u</code>、<code>%lu</code>和<code>%llu</code>。这样不利于程序的可移植性。<br>C语言提供了一个解决方法,创造了一个类型别名<code>size_t</code>,用来统一表示<code>sizeof</code>的返<br>回值类型。对应当前系统的<code>sizeof</code>的返回值类型,可能是<code>unsigned int</code>,也可能是<br><code>unsigned long long</code>。</p></blockquote><p><strong>比如:</strong></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> a = <span class="number">10</span> ;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%zd\n"</span>, <span class="keyword">sizeof</span>(a));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%zd\n"</span>, <span class="keyword">sizeof</span> a);<span class="comment">//a是变量的名字,可以省略掉sizeof后边的()</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%zd\n"</span>, <span class="keyword">sizeof</span>(<span class="type">int</span>));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%zd\n"</span>, <span class="keyword">sizeof</span>( <span class="number">3</span> + <span class="number">3.5</span>));</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h3 id="数据类型⻓度"><a href="#数据类型⻓度" class="headerlink" title="数据类型⻓度"></a>数据类型⻓度</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%zd\n"</span>, <span class="keyword">sizeof</span>(<span class="type">char</span>));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%zd\n"</span>, <span class="keyword">sizeof</span>(<span class="type">_Bool</span>));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%zd\n"</span>, <span class="keyword">sizeof</span>(<span class="type">short</span>));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%zd\n"</span>, <span class="keyword">sizeof</span>(<span class="type">int</span>));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%zd\n"</span>, <span class="keyword">sizeof</span>(<span class="type">long</span>));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%zd\n"</span>, <span class="keyword">sizeof</span>(<span class="type">long</span> <span class="type">long</span>));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%zd\n"</span>, <span class="keyword">sizeof</span>(<span class="type">float</span>));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%zd\n"</span>, <span class="keyword">sizeof</span>(<span class="type">double</span>));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%zd\n"</span>, <span class="keyword">sizeof</span>(<span class="type">long</span> <span class="type">double</span>));</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>在VS2022X64配置下的输出:</strong></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">1</span><br><span class="line">1</span><br><span class="line">2 </span><br><span class="line">4 </span><br><span class="line">4 </span><br><span class="line">8 </span><br><span class="line">4 </span><br><span class="line">8 </span><br><span class="line">8</span><br></pre></td></tr></table></figure><h3 id="sizeof中表达式不计算"><a href="#sizeof中表达式不计算" class="headerlink" title="sizeof中表达式不计算"></a>sizeof中表达式不计算</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//测试:sizeof中表达式不计算</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">short</span> s = <span class="number">2</span> ;</span><br><span class="line"> <span class="type">int</span> b = <span class="number">10</span> ;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, <span class="keyword">sizeof</span>(s = b+ <span class="number">1</span> ));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"s = %d\n"</span>, s);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><blockquote><p><code>sizeof</code>的表达式不计算,sizeof在代码进行编译的时候,就根据表达式的类型确定了,类型的常用,而表达式的执行却要在<br>程序运行期间才能执行,在编译期间已经将sizeof处理掉了,所以在运行期间就不会执行表达式了。</p></blockquote><h2 id="signed-和-unsigned"><a href="#signed-和-unsigned" class="headerlink" title="signed 和 unsigned"></a>signed 和 unsigned</h2><blockquote><p>C语言使用signed和unsigned关键字修饰<strong>字符型和整型</strong> 类型的。<br>signed关键字,表示一个类型带有正负号,包含负值;<br>unsigned关键字,表示该类型不带有正负号,只能表示零和正整数。<br>对于int类型,默认是带有正负号的,也就是说int等同于signed int。<br>由于这是默认情况,关键字signed一般都省略不写,但是写了也不算错。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">signed</span> <span class="type">int</span> a;</span><br><span class="line"><span class="comment">// 等同于int a;</span></span><br></pre></td></tr></table></figure><blockquote><p>对于int类型也可以不带正负号,只表示非负整数。这时就必须使用关键字unsigned声明变量。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">unsigned</span> <span class="type">int</span> a;</span><br></pre></td></tr></table></figure><blockquote><p>整数变量声明为unsigned的好处是,同样⻓度的内存能够表示的最大整数值,增大了一倍。<br>比如, 16 位的signed short int的取值范围是:-32768<del>32767,最大是 32767 ;而<br>unsigned short int的取值范围是:0</del>65535,最大值增大到了65,535。 32 位的signed<br>int的取值范围可以参看limits.h中给出的定义。<br>下面的定义是VS2022环境中,limits.h中相关定义。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> SHRT_MIN (-32768) <span class="comment">//有符号 16 位整型的最小值</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> SHRT_MAX 32767 <span class="comment">//有符号 16 位整型的最大值</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> USHRT_MAX 0xffff <span class="comment">//无符号 16 位整型的最大值</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> INT_MIN (-2147483647 - 1) <span class="comment">//有符号整型的最小值</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> INT_MAX 2147483647 <span class="comment">//有符号整型的最大值</span></span></span><br></pre></td></tr></table></figure><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">unsigned</span> <span class="type">int</span>里面的<span class="type">int</span>可以省略,所以上面的变量声明也可以写成下面这样。</span><br></pre></td></tr></table></figure><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">unsigned</span> a;</span><br></pre></td></tr></table></figure><p>字符类型char也可以设置signed和unsigned。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">signed</span> <span class="type">char</span> c; <span class="comment">// 范围为 -128 到 127</span></span><br><span class="line"><span class="type">unsigned</span> <span class="type">char</span> c; <span class="comment">// 范围为 0 到 255</span></span><br></pre></td></tr></table></figure><p>注意,C语言规定char类型默认是否带有正负号,由当前系统决定。<br>这就是说,char不等同于signed char,它有可能是signed char,也有可能是<br>unsigned char。<br>这一点与int不同,int就是等同于signed int。</p><h1 id="数据类型的取值范围"><a href="#数据类型的取值范围" class="headerlink" title="数据类型的取值范围"></a>数据类型的取值范围</h1><blockquote><p><strong>上述的数据类型很多,尤其数整型类型就有short、int、long、longlong四种,为什么呢?</strong></p></blockquote><blockquote><p>其实每一种数据类型有自己的取值范围,也就是存储的数值的最大值和最小值的区间,有了丰富的类<br>型,我们就可以在适当的场景下去选择适合的类型。如果要查看当前系统上不同数据类型的极限值:</p></blockquote><ul><li>整型:limits.h头文件中定义了整型的极限值。</li><li>浮点型:float.h头文件中定义了浮点型的极限值。</li></ul><blockquote><p>为了代码的可移植性,需要知道某种整数类型的极限值时,应该尽量使用这些常量。</p></blockquote><ul><li><code>SCHAR_MIN</code>,<code>SCHAR_MAX</code>:signed char的最小值和最大值。</li><li><code>SHRT_MIN</code>,<code>SHRT_MAX</code>:short的最小值和最大值。</li><li><code>INT_MIN</code>,<code>INT_MAX</code>:int的最小值和最大值。</li><li><code>LONG_MIN</code>,<code>LONG_MAX</code>:long的最小值和最大值。</li><li><code>LLONG_MIN</code>,<code>LLONG_MAX</code>:longlong的最小值和最大值。</li><li><code>UCHAR_MAX</code>:`unsigned char的最大值。</li><li><code>USHRT_MAX</code>:unsigned short的最大值。</li><li><code>UINT_MAX</code>:unsigned int的最大值。</li><li><code>ULONG_MAX</code>:unsigned long的最大值。</li><li><code>ULLONG_MAX</code>:unsigned long long的最大值。</li></ul><h1 id="变量"><a href="#变量" class="headerlink" title="变量"></a>变量</h1><h2 id="变量的创建"><a href="#变量的创建" class="headerlink" title="变量的创建"></a>变量的创建</h2><blockquote><p>了解清楚了类型,我们使用类型做什么呢?</p></blockquote><p><strong>类型是用来<code>创建变量</code>的。</strong></p><blockquote><p>什么是变量呢?</p></blockquote><p><strong>C语言中把经常变化的值称为<code>变量</code> ,不变的值称为<code>常量</code> 。</strong></p><p>变量创建的语法形式是这样的:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">数据类型 变量名 = 初始值 ;</span><br><span class="line">data_type name;</span><br><span class="line"> | |</span><br><span class="line"> | |</span><br><span class="line">数据类型 变量名</span><br></pre></td></tr></table></figure><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> age; <span class="comment">//整型变量</span></span><br><span class="line"><span class="type">char</span> ch; <span class="comment">//字符变量</span></span><br><span class="line"><span class="type">double</span> weight; <span class="comment">//浮点型变量</span></span><br><span class="line"><span class="type">unsigned</span> <span class="type">int</span> height; <span class="comment">//无符号整型变量</span></span><br></pre></td></tr></table></figure><blockquote><p><strong>变量在创建的时候就给一个初始值,就叫初始化。</strong></p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> age = <span class="number">18</span> ;</span><br><span class="line"><span class="type">char</span> ch = <span class="string">'w'</span>;</span><br><span class="line"><span class="type">double</span> weight = <span class="number">48.0</span>;</span><br><span class="line"><span class="type">unsigned</span> <span class="type">int</span> height = <span class="number">100</span> ;</span><br></pre></td></tr></table></figure><h2 id="变量的分类"><a href="#变量的分类" class="headerlink" title="变量的分类"></a>变量的分类</h2><ul><li><p><strong>全局变量:</strong> 在大括号外部定义的变量就是全局变量<br>全局变量的使用范围更广,整个工程中想使用,都是有办法使用的。</p></li><li><p><strong>局部变量:</strong> 在大括号内部定义的变量就是局部变量<br>局部变量的使用范围是比较局限,只能在自己所在的局部范围内使用的。</p></li><li><p><strong>静态变量:</strong> 在函数体外定义的变量就是静态变量<br>静态变量的作用域是整个函数体,在函数体内使用,只能在函数体内使用。</p></li></ul><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> global = <span class="number">2023</span> ;<span class="comment">//全局变量</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> local = <span class="number">2018</span> ;<span class="comment">//局部变量</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, local);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, global);</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><blockquote><p><strong>如果局部和全局变量,名字相同呢?</strong></p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> n = <span class="number">1000</span> ;</span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> n = <span class="number">10</span> ;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span> n);<span class="comment">//打印的结果是多少呢?</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><blockquote><p><strong>其实当局部变量和全局变量同名的时候,<code>局部变量优先使用</code>。</strong></p></blockquote><blockquote><p><strong><code>全局变量和局部变量在内存中存储在哪里呢?</code></strong></p></blockquote><p>一般我们在学习C/C++语言的时候,我们会关注</p><p>内存中的三个区域:<code>栈区</code>、<code>堆区</code>、<code>静态区</code> 。</p><ol><li><p><strong>局部变量</strong>是放在内存的 ——> <strong>栈区</strong></p></li><li><p><strong>全局变量</strong>是放在内存的——> <strong>静态区</strong></p></li><li><p>堆区是用来动态内存管理的(后期会介绍)</p></li></ol><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/39254/image-1.png" alt="全局变量和局部变量在内存中存储的位置"></p><h1 id="算术操作符:-、-、-、-、"><a href="#算术操作符:-、-、-、-、" class="headerlink" title="算术操作符:+、-、*、/、%"></a>算术操作符:+、-、*、/、%</h1><p>在写代码时候,一定会涉及到计算。</p><p>C语言中为了方便运算,提供了一系列操作符,其中有一组操作符叫: 算术操作符 。分别是:**<code>+</code> ,<code>-</code> , <code>*</code> ,<code>/</code> , <code>%</code>**,这些操作符都是 双目操作符 。</p><blockquote><p>注:操作符也被叫做: 运算符 ,是不同的翻译,意思是一样的。</p></blockquote><h2 id="和"><a href="#和" class="headerlink" title="+ 和 -"></a><code>+</code> 和 <code>-</code></h2><p>+和-用来完成加法和减法。</p><p>+和-都是有 2 个操作数的,位于操作符两端的就是它们的操作数,这种操作符也叫 双目操作符 。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> x = <span class="number">4</span> + <span class="number">22</span> ;</span><br><span class="line"> <span class="type">int</span> y = <span class="number">61</span> - <span class="number">23</span> ;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, x);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, y);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id><a href="#" class="headerlink" title="*"></a><code>*</code></h2><p>运算符*用来完成乘法。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> num = <span class="number">5</span> ;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, num * num); <span class="comment">// 输出 25</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="-1"><a href="#-1" class="headerlink" title="/"></a><code>/</code></h2><p>运算符/用来完成除法。</p><p>除号的两端如果是整数,执行的是整数除法,得到的结果也是整数。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">float</span> x = <span class="number">6</span> / <span class="number">4</span> ;</span><br><span class="line"> <span class="type">int</span> y = <span class="number">6</span> / <span class="number">4</span> ;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%f\n"</span>, x); <span class="comment">// 输出 1.</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, y); <span class="comment">// 输出 1</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><blockquote><p>上面示例中,尽管变量<code>x</code>的类型是<code>float</code>(浮点数),但是<code>6 / 4</code>得到的结果是<code>1.0</code>,而不是<code>1.5</code>。原因就在于<strong>C语言</strong>里面的整数除法是整除,只会返回整数部分,丢弃小数部分。</p></blockquote><p>如果希望得到浮点数的结果,<strong>两个运算数<code>必须至少有一个浮点数</code></strong> ,这时C语言就会进行浮点数除法。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">float</span> x = <span class="number">6.0</span> / <span class="number">4</span> ; <span class="comment">// 或者写成 6 / 4.</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%f\n"</span>, x); <span class="comment">// 输出 1.</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>上面示例中,<code>6.0 / 4</code>表示进行浮点数除法,得到的结果就是1.5。</p><blockquote><p>再看一个例子:</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> score = <span class="number">5</span> ;</span><br><span class="line"> score = (score / <span class="number">20</span> ) * <span class="number">100</span> ;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><blockquote><p>上面的代码,你可能觉得经过运算,<code>score</code>会等于<code>25</code> ,但是实际上<code>score</code>等于<code>0</code> 。这是因为<code>score / 20</code>是整除,会得到一个整数值 <code>0</code> ,所以乘以 <code>100</code> 后得到的也是<code>0</code>。</p></blockquote><p>为了得到预想的结果,可以将除数 20 改成20.0,让整除变成浮点数除法。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> score = <span class="number">5</span> ;</span><br><span class="line"> score = (score / <span class="number">20.0</span>) * <span class="number">100</span> ;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="-2"><a href="#-2" class="headerlink" title="%"></a>%</h2><p>运算符<code>%</code>表示求模(余)运算,即返回两个整数相除的余值。这个运算符只能用于整数,不能用于浮点数。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> x = <span class="number">6</span> % <span class="number">4</span> ; <span class="comment">// 2</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><blockquote><p>负数求模的规则是,结果的正负号由第一个运算数的正负号决定。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, <span class="number">11</span> % <span class="number">-5</span>); <span class="comment">// 1</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,<span class="number">-11</span> % <span class="number">-5</span>); <span class="comment">// -</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,<span class="number">-11</span> % <span class="number">5</span> ); <span class="comment">// -</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><blockquote><p>上面示例中,第一个运算数的正负号( <code>11</code> 或<code>-11</code>)决定了结果的正负号。</p></blockquote><h1 id="赋值操作符:-和复合赋值"><a href="#赋值操作符:-和复合赋值" class="headerlink" title="赋值操作符:=和复合赋值"></a>赋值操作符:=和复合赋值</h1><p>在变量创建的时候给一个初始值叫初始化,在变量创建好后,再给一个值,这叫赋值。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a = <span class="number">100</span> ;<span class="comment">//初始化</span></span><br></pre></td></tr></table></figure><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="number">200</span> ;<span class="comment">//赋值,这里使用的就是赋值操作符</span></span><br></pre></td></tr></table></figure><p>赋值操作符=是一个随时可以给变量赋值的操作符。</p><h2 id="连续赋值"><a href="#连续赋值" class="headerlink" title="连续赋值"></a>连续赋值</h2><p>赋值操作符也可以连续赋值,如:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a = <span class="number">3</span> ;</span><br><span class="line"><span class="type">int</span> b = <span class="number">5</span> ;</span><br><span class="line"><span class="type">int</span> c = <span class="number">0</span> ;</span><br><span class="line">c = b = a+ <span class="number">3</span> ;<span class="comment">//连续赋值,从右向左依次赋值的。</span></span><br></pre></td></tr></table></figure><blockquote><p>C语言虽然支持这种连续赋值,但是写出的代码不容易理解,建议还是拆开来写,这样方便观察代码的执行细节。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a = <span class="number">3</span> ;</span><br><span class="line"><span class="type">int</span> b = <span class="number">5</span> ;</span><br><span class="line"><span class="type">int</span> c = <span class="number">0</span> ;</span><br><span class="line">b = a+ <span class="number">3</span> ;</span><br><span class="line">c = b;</span><br></pre></td></tr></table></figure><p>这样写,在调试的是,每一次赋值的细节都是可以很方便的观察的。</p><h2 id="复合赋值符"><a href="#复合赋值符" class="headerlink" title="复合赋值符"></a>复合赋值符</h2><p><code>在写代码时,我们经常可能对一个数进行自增、自减的操作</code>,如下代码:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a = <span class="number">10</span> ;</span><br><span class="line">a = a+ <span class="number">3</span> ;</span><br><span class="line">a = a<span class="number">-2</span>;</span><br></pre></td></tr></table></figure><p>这样代码C语言给提供了更加方便的写法:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a = <span class="number">10</span> ;</span><br><span class="line">a += <span class="number">3</span> ;</span><br><span class="line">a -= <span class="number">2</span> ;</span><br></pre></td></tr></table></figure><blockquote><p>C语言中提供了复合赋值符,方便我们编写代码,这些赋值符有:</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">+= -=</span><br><span class="line">*= /= %=</span><br><span class="line"><span class="comment">//下面的操作符后期讲解</span></span><br><span class="line">>>= <<=</span><br><span class="line">&= |= ^=</span><br></pre></td></tr></table></figure><h1 id="单目操作符:-、–、-、"><a href="#单目操作符:-、–、-、" class="headerlink" title="单目操作符:++、–、+、-"></a>单目操作符:++、–、+、-</h1><p>前面介绍的操作符都是双目操作符,有 2 个操作数的。C语言中还有一些操作符只有一个操作数,被称</p><p>为单目操作符。**<code>++、--、+(正)、-(负)</code>**就是单目操作符的。</p><h2 id="和–"><a href="#和–" class="headerlink" title="++和–"></a>++和–</h2><p>++是一种自增的操作符,又分为前置++和后置++,–是一种自减的操作符,也分为前置–和后置–</p><h3 id="前置"><a href="#前置" class="headerlink" title="前置++"></a>前置++</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a = <span class="number">10</span> ;</span><br><span class="line"><span class="type">int</span> b = ++a;<span class="comment">//++的操作数是a,是放在a的前面的,就是前置++</span></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"a=%d b=%d\n"</span>,a , b);</span><br></pre></td></tr></table></figure><p><code>计算口诀</code>:先+1,后使用;</p><p>a原来是 10 ,先+1,后a变成了 11 ,再使用就是赋值给b,b得到的也是 11 ,所以计算技术后,a和b都是 11 ,相当于这样的代码:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a = <span class="number">10</span> ;</span><br><span class="line">a = a+ <span class="number">1</span> ;</span><br><span class="line">b = a;</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"a=%d b=%d\n"</span>,a , b);</span><br></pre></td></tr></table></figure><h3 id="后置"><a href="#后置" class="headerlink" title="后置++"></a>后置++</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a = <span class="number">10</span> ;</span><br><span class="line"><span class="type">int</span> b = a++;<span class="comment">//++的操作数是a,是放在a的后面的,就是后置++</span></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"a=%d b=%d\n"</span>,a , b);</span><br></pre></td></tr></table></figure><p><code>计算口诀</code> :先使用,后+1</p><p>a原来是 10 ,先使用,就是先赋值给b,b得到了 10 ,然后再+1,然后a变成了1 1,所以直接结束后a是11 ,b是 10 ,相当于这样的代码:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a = <span class="number">10</span> ;</span><br><span class="line"><span class="type">int</span> b = a;</span><br><span class="line">a = a+ <span class="number">1</span> ;</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"a=%d b=%d\n"</span>,a , b);</span><br></pre></td></tr></table></figure><h3 id="前置–"><a href="#前置–" class="headerlink" title="前置–"></a>前置–</h3><p>如果你听懂了前置++,那前置–是同理的,只是把加 1 ,换成了减1 ;</p><p><code>计算口诀</code>: 先-1,后使用</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a = <span class="number">10</span> ;</span><br><span class="line"><span class="type">int</span> b = --a;<span class="comment">//--的操作数是a,是放在a的前面的,就是前置--</span></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"a=%d b=%d\n"</span>,a , b);<span class="comment">//输出的结果是:9 9</span></span><br></pre></td></tr></table></figure><h3 id="后置–"><a href="#后置–" class="headerlink" title="后置–"></a>后置–</h3><p>同理后置–类似于后置++,只是把加一换成了减一</p><p><code>计算口诀</code>: 先使用,后-1</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a = <span class="number">10</span> ;</span><br><span class="line"><span class="type">int</span> b = a--;<span class="comment">//--的操作数是a,是放在a的后面的,就是后置--</span></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"a=%d b=%d\n"</span>,a , b);<span class="comment">//输出的结果是:9 10</span></span><br></pre></td></tr></table></figure><h1 id="和-1"><a href="#和-1" class="headerlink" title="+和-"></a>+和-</h1><p>这里的+是正号,-是负号,都是<code>单目操作符</code>。</p><blockquote><p>运算符<code>+</code>对正负值没有影响,是一个完全可以省略的运算符,但是写了也不会报错。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span> <span class="type">int</span> a = + <span class="number">10</span> ; 等价于 <span class="type">int</span> a = <span class="number">10</span> ;</span><br></pre></td></tr></table></figure><blockquote><p>运算符<code>-</code>用来改变一个值的正负号,负数的前面加上<code>-</code>就会得到正数,正数的前面加上<code>-</code>会得到负数。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a = <span class="number">10</span> ;</span><br><span class="line"><span class="type">int</span> b = -a;</span><br><span class="line"><span class="type">int</span> c = <span class="number">-10</span>;</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"b=%d c=%d\n"</span>, b, c);<span class="comment">//这里的b和c都是-</span></span><br><span class="line"><span class="type">int</span> a = <span class="number">-10</span>;</span><br><span class="line"><span class="type">int</span> b = -a;</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"b=%d\n"</span>, b); <span class="comment">//这里的b是 10</span></span><br></pre></td></tr></table></figure><h1 id="强制类型转换"><a href="#强制类型转换" class="headerlink" title="强制类型转换"></a>强制类型转换</h1><p>在操作符中还有一种特殊的操作符是强制类型转换,语法形式很简单,形式如下:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(类型)</span><br></pre></td></tr></table></figure><p><code>请看代码</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a = <span class="number">3.14</span>;</span><br><span class="line"><span class="comment">//a的是int类型, 3.14是double类型,两边的类型不一致,编译器会报警告</span></span><br></pre></td></tr></table></figure><p><code>为了消除这个警告,我们可以使用强制类型转换</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a = (<span class="type">int</span>)<span class="number">3.14</span>;<span class="comment">//意思是将3.14强制类型转换为int类型,这种强制类型转换只取整数部分</span></span><br></pre></td></tr></table></figure><p><strong>俗话说,强扭的瓜不甜,我们使用强制类型转换都是万不得已的时候使用,如果不需要强制类型转化就能实现代码,这样自然更好的</strong>。</p><h1 id="scanf-和-printf介绍"><a href="#scanf-和-printf介绍" class="headerlink" title="scanf 和 printf介绍"></a>scanf 和 printf介绍</h1><h2 id="printf"><a href="#printf" class="headerlink" title="printf"></a>printf</h2><h3 id="基本用法"><a href="#基本用法" class="headerlink" title="基本用法"></a>基本用法</h3><blockquote><p><code>printf()</code>的作用是将参数文本输出到屏幕。它名字里面的<code>f</code>代表<code>format(格式化</code>,表示可以定制输出文本的格式。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"Hello World"</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><blockquote><p>上面命令会在屏幕上输出一行文字“Hello World”。</p></blockquote><p><code>printf()</code>不会在行尾自动添加换行符,运行结束后,光标就停留在输出结束的地方,不会自动换行。</p><p>为了让光标移到下一行的开头,可以在输出文本的结尾,添加一个换行符<code>\n</code>。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">{</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"Hello World\n"</span>);</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>如果文本内部有换行,也是通过插入换行符来实现,如下方代码</strong>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"Hello\nWorld\n"</span>);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"Hello\n"</span>);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"World\n"</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>printf()</code>是在标准库的头文件stdio.h定义的。使用这个函数之前,必须在源码文件头部引入这个头文件。</p><h3 id="占位符"><a href="#占位符" class="headerlink" title="占位符"></a>占位符</h3><p><code>printf()</code>可以在输出文本中指定占位符。<br>所谓“ <strong>占位符</strong> ”,就是这个位置可以用其他值代入。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 输出 There are 3 apples</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"There are %d apples\n"</span>, <span class="number">3</span> );</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><blockquote><p>上面示例中,<code>There are %d apples\n</code>是输出文本,里面的<code>%d</code>就是占位符,表示这个位置要用其他值来替换。 <strong>占位符的第一个字符一律为百分号</strong> %,第二个字符表示占位符的类型,<code>%d</code><strong>表示这里代入的值必须是一个整数</strong> 。</p></blockquote><p><code>printf()</code>的第二个参数就是替换占位符的值,上面的例子是整数<code>3</code> 替换<code>%d</code>。执行后的输出结果<br>就是<code>There are 3 apples</code>。</p><p>常用的占位符除了<code>%d</code>,还有<code>%s</code>表示代入的是字符串。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%s will come tonight\n"</span>, <span class="string">"zhangsan"</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><blockquote><p>上面示例中,<code>%s</code><strong>表示代入的是一个字符串</strong> ,所以<code>printf()</code>的第二个参数就必须是字符串,这个例子是<code>zhangsan</code>。执行后的输出就是<code>zhangsan will come tonight</code>。</p></blockquote><p><strong>输出文本里面可以使用 多个占位符</strong>。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%s says it is %d o'clock\n"</span>, <span class="string">"lisi"</span>, <span class="number">21</span> );</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><blockquote><p>上面示例中,输出文本<code>%s says it is %d o'clock</code>有两个占位符,**<code>第一个</code>是字符串占位符<code>%s</code>,<code>第二个</code>是整数占位符<code>%d</code>**,分别对应<code>printf()</code>的第二个参数(<code>lisi</code>)和第三个参数( <code>21</code> )。执行后的输出就是<code>lisi says it is 21 o'clock</code>。</p></blockquote><p><code>printf()</code>参数与占位符是一一对应关系,如果有<code>n</code>个占位符,printf()的参数就应该有<code>n+1</code>个。如果参数个数少于对应的占位符,<code>printf()</code>可能会输出内存中的任意值。</p><h3 id="占位符列举"><a href="#占位符列举" class="headerlink" title="占位符列举"></a>占位符列举</h3><p><code>printf()</code>的占位符有许多种类,与C语言的数据类型相对应。下面按照字母顺序,列出常用的占位符,方便查找,具体含义在后面章节介绍。</p><ul><li><code>%a</code>:十六进制浮点数,字母输出为小写。</li><li><code>%A</code>:十六进制浮点数,字母输出为大写。</li><li><strong><code>%c</code></strong> :<strong>字符。</strong></li><li><strong><code>%d</code></strong> :<strong>十进制整数。//int</strong> </li><li><code>%e</code>:使用科学计数法的浮点数,指数部分的e为小写。</li><li><code>%E</code>:使用科学计数法的浮点数,指数部分的E为大写。</li><li><code>%i</code>:整数,基本等同于%d。</li><li><strong><code>%f</code></strong> :<strong>小数(包含 float 类型和double 类型)。//float %f double-%lf</strong></li><li><code>%g</code>:^6 个有效数字的浮点数。整数部分一旦超过^6 位,就会自动转为科学计数法,指数部分的e为小写。</li><li><code>%G</code>:等同于%g,唯一的区别是指数部分的E为大写。</li><li><strong><code>%hd</code></strong> :<strong>十进制 short int类型。</strong> </li><li><code>%ho</code>:八进制short int类型。</li><li><code>%hx</code>:十六进制 short int类型。</li><li><strong><code>%hu</code></strong> :<strong>unsigned short int类型。</strong> </li><li><strong><code>%ld</code><strong>:</strong>十进制longint类型。</strong> </li><li><code>%lo</code>:八进制longint类型。</li><li><code>%lx</code>:十六进制longint类型。</li><li><strong><code>%lu</code><strong>:</strong>unsigned long int类型。</strong> </li><li><code>%lld</code>:十进制 long long int类型。</li><li><code>%llo</code>:八进制 long long int类型。</li><li><code>%llx</code>:十六进制 long long int类型。</li><li><code>%llu</code>:unsigned long long int类型。</li><li><code>%Le</code>:科学计数法表示的 long double类型浮点数。</li><li><strong><code>%Lf</code></strong> :<strong>long double 类型浮点数。</strong> </li><li><code>%n</code>:已输出的字符串数量。该占位符本身不输出,只将值存储在指定变量之中。</li><li><code>%o</code>:八进制整数。</li><li><strong><code>%p</code><strong>:</strong>指针(用来打印地址)。</strong></li><li><strong><code>%s</code><strong>:</strong>字符串。</strong></li><li><strong><code>%u</code><strong>:</strong>无符号整数(unsigned int)</strong> 。</li><li>**<code>%x</code><strong>:十六进制整数。</strong></li><li><strong><code>%zd</code><strong>:</strong>size_t 类型。</strong></li><li><code>%%</code>:输出一个百分号。</li></ul><h3 id="输出格式"><a href="#输出格式" class="headerlink" title="输出格式"></a>输出格式</h3><p><code>printf()</code>可以定制占位符的输出格式。</p><h4 id="限定宽度"><a href="#限定宽度" class="headerlink" title="限定宽度"></a>限定宽度</h4><p><code>printf()</code>允许限定占位符的**<code>最小宽度</code>**。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%5d\n"</span>, <span class="number">123</span> ); <span class="comment">// 输出为 " 123"</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><blockquote><p>上面示例中,<code>%5d</code>表示这个占位符的宽度至少为 5 位。如果不满 5 位,对应的值的前面会添加空格。<br>输出的值 <strong>默认是右对⻬</strong> ,即输出内容前面会有空格;如果希望改成左对⻬,在输出内容后面添加空<br>格,可以在占位符的<code>%</code>的后面插入一个<code>-</code>号。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%-5d\n"</span>, <span class="number">123</span> ); <span class="comment">// 输出为 "123 "</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>输出结果</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">123</span> </span><br></pre></td></tr></table></figure><blockquote><p>上面示例中,输出内容<code>123</code>的后面添加了空格。</p></blockquote><blockquote><p>对于小数,这个限定符会限制所有数字的最小显示宽度。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 输出 " 123.450000"</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%12f\n"</span>, <span class="number">123.45</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>输出结果</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">__123<span class="number">.450000</span></span><br></pre></td></tr></table></figure><blockquote><p>上面示例中,<code>%12f</code>表示输出的浮点数最少要占据 12 位。由于小数的默认显示精度是小数点后 6 位,所以<code>123.45</code>输出结果的头部会添加 2个空格。</p></blockquote><h4 id="总是显示正负号"><a href="#总是显示正负号" class="headerlink" title="总是显示正负号"></a>总是显示正负号</h4><blockquote><p>默认情况下,printf()不对正数显示+号,只对负数显示-号。如果想让正数也输出+号,可以在占位符的%后面加一个+。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%+d\n"</span>, <span class="number">12</span> ); <span class="comment">// 输出 +12</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%+d\n"</span>, <span class="number">-12</span>); <span class="comment">// 输出 -12</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>输出结果</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">+<span class="number">12</span></span><br><span class="line"><span class="number">-12</span></span><br></pre></td></tr></table></figure><blockquote><p>上面示例中,%+d可以确保输出的数值,总是带有正负号。</p></blockquote><h4 id="限定小数位数"><a href="#限定小数位数" class="headerlink" title="限定小数位数"></a>限定小数位数</h4><blockquote><p>输出小数时,有时希望限定小数的位数。举例来说,希望小数点后面只保留两位,占位符可以写成%.2f。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 输出 Number is 0.50</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"Number is %.2f\n"</span>, <span class="number">0.5</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>输出结果</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Number is <span class="number">0.500</span></span><br></pre></td></tr></table></figure><blockquote><p>上面示例中,如果希望小数点后面<code>输出3位(0.500</code>,占位符就要写成<code>%.3f</code>。</p></blockquote><blockquote><p>这种写法可以与限定宽度占位符,结合使用。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 输出为 " 0.50"</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%6.2f\n"</span>, <span class="number">0.5</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>输出结果</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">__0<span class="number">.50</span></span><br></pre></td></tr></table></figure><blockquote><p>上面示例中,<code>%6.2f</code>表示输出字符串<code>最小宽度为 6</code>,<code>小数位数为 2</code>。所以,输出字符串的<code>头部有两个空格</code>。</p></blockquote><blockquote><p>最小宽度和小数位数这两个限定值,都可以用<code>*</code>代替,通过<code>printf()</code>的参数传入。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%*.*f\n"</span>, <span class="number">6</span> , <span class="number">2</span> , <span class="number">0.5</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br><span class="line"><span class="comment">// 等同于printf("%6.2f\n", 0.5);</span></span><br></pre></td></tr></table></figure><p><code>输出结果</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">__0<span class="number">.50</span></span><br></pre></td></tr></table></figure><blockquote><p>上面示例中,<code>%*.*f</code>表示输出字符串,其中<code>*</code>表示第一个参数是最小宽度,第二个参数是小数位数。及<code>%*.*f</code>的两个星号通过printf()的两个参数 <code>6</code> 和<code>2</code> 传入。</p></blockquote><h4 id="输出部分字符串"><a href="#输出部分字符串" class="headerlink" title="输出部分字符串"></a>输出部分字符串</h4><p><code>%s</code>占位符用来输出字符串,默认是全部输出。如果只想输出开头的部分,可以用<code>%.[m]s</code>指定输出的⻓度,其中<code>[m]</code>代表一个数字,表示所要输出的⻓度。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 输出 hello</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%.5s\n"</span>, <span class="string">"hello world"</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>输出结果</code>:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hello</span><br></pre></td></tr></table></figure><p>上面示例中,占位符<code>%.5s</code>表示只输出字符串<code>“hello world”</code>的<code>前5 个字符</code>,即<code>“hello”</code>。</p><h2 id="scanf"><a href="#scanf" class="headerlink" title="scanf"></a>scanf</h2><p>当我们有了变量,我们需要给变量输入值就可以使用<code>scanf</code>函数,如果需要将变量的值输出在屏幕上的时候可以使用<code>printf</code>函数,下面看一个例子:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> score = <span class="number">0</span> ;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"请输入成绩:"</span>);</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &score);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"成绩是:%d\n"</span>, score);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>运行截图</code>:</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/39254/image-2.png" alt="运行截图"></p><p><code>画图演示</code>:</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/39254/image-3.png" alt="画图演⽰"></p><blockquote><p><code>注</code>:<strong>标准输入一般指的就是键盘,标准输出一般指的就是屏幕</strong></p></blockquote><blockquote><p>那接下来我们介绍一下<code>scanf</code>函数。</p></blockquote><h3 id="基本用法-1"><a href="#基本用法-1" class="headerlink" title="基本用法"></a>基本用法</h3><p><code>scanf()</code>函数用于读取用戶的键盘输入。<br>程序运行到这个语句时,会停下来,等待用戶从键盘输入。<br>用戶输入数据、按下回⻋键后,<code>scanf()</code>就会处理用戶的输入,将其存入变量。<br>它的原型定义在头文件<code>stdio.h</code>。<br><code>scanf()</code>的语法跟<code>printf()</code>类似。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">scanf</span>(<span class="string">"%d"</span>, &i);</span><br></pre></td></tr></table></figure><p>它的第一个参数是一个格式字符串,里面会放置占位符(与printf()的占位符基本一致),告诉编译器如何解读用戶的输入,需要提取的数据是什么类型。</p><p>这是因为C语言的数据都是有类型的,scanf()必须提前知道用戶输入的数据类型,才能处理数<br>据。</p><p>它的其余参数就是存放用戶输入的变量,格式字符串里面有多少个占位符,就有多少个变量。</p><p>上面示例中,scanf()的第一个参数%d,表示用戶输入的应该是一个整数。%d就是一个占位符,%是占位符的标志,d表示整数。第二个参数&i表示,将用戶从键盘输入的整数存入变量i。</p><blockquote><p>注意:变量前面必须加上&运算符(指针变量除外),因为scanf()传递的不是值,而是地址,即将变量i的地址指向用戶输入的值。</p></blockquote><p>如果这里的变量是指针变量(比如字符串变量),那就不用加&运算符。</p><p><code>下面是一次将键盘输入读入多个变量的例子</code>。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">scanf</span>(<span class="string">"%d%d%f%f"</span>, &i, &j, &x, &y);</span><br></pre></td></tr></table></figure><p>上面示例中,格式字符串%d%d%f%f,表示用戶输入的前两个是整数,后两个是浮点数,比如 1-20 3.4 -4.0e3。这四个值依次放入i、j、x、y四个变量。<br>scanf()处理数值占位符时,会 <strong>自动过滤空白字符,包括空格、制表符、换行符</strong> 等。<br>所以,用戶输入的数据之间,有一个或多个空格不影响scanf()解读数据。另外,用戶使用回⻋<br>键,将输入分成几行,也不影响解读。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span></span><br><span class="line"><span class="number">-20</span></span><br><span class="line"><span class="number">3.4</span></span><br><span class="line"><span class="number">-4.0e3</span></span><br></pre></td></tr></table></figure><p>上面示例中,用戶分成四行输入,得到的结果与一行输入是完全一样的。每次按下回⻋键以后,<code>scanf()</code>就会开始解读,如果第一行匹配第一个占位符,那么下次按下回⻋键时,就会从第二个占位符开始解读。</p><p><code>scanf()</code>处理用戶输入的原理是,用戶的输入先放入缓存,等到按下回⻋键后,按照占位符对缓存进行解读。</p><p>解读用戶输入时,会从上一次解读遗留的第一个字符开始,直到读完缓存,或者遇到第一个不符合条件的字符为止。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> x;</span><br><span class="line"><span class="type">float</span> y;</span><br><span class="line"><span class="comment">// 用戶输入 " -13.45e12# 0"</span></span><br><span class="line"><span class="built_in">scanf</span>(<span class="string">"%d"</span>, &x);</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d\n"</span>, x);</span><br><span class="line"><span class="built_in">scanf</span>(<span class="string">"%f"</span>, &y);</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%f\n"</span>, y);</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>上面示例中,<code>scanf()</code>读取用戶输入时,<code>%d</code>占位符会忽略起首的空格,从<code>-</code>处开始获取数据,读取到<code>-13</code>停下来,因为后面的<code>.</code>不属于整数的有效字符。这就是说,占位符<code>%d</code>会读到<code>-13</code>。<br>第二次调用<code>scanf()</code>时,就会从上一次停止解读的地方,继续往下读取。这一次读取的首字符<br>是<code>.</code>,由于对应的占位符是<code>%f</code>,会读取到<code>.45e12</code>,这是采用科学计数法的浮点数格式。后面的<code>#</code>不属于浮点数的有效字符,所以会停在这里。<br>由于<code>scanf()</code>可以连续处理多个占位符,所以上面的例子也可以写成下面这样。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> x;</span><br><span class="line"> <span class="type">float</span> y;</span><br><span class="line"> <span class="comment">// 用戶输入 " -13.45e12# 0"</span></span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d%f"</span>, &x, &y);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h3 id="scanf的返回值"><a href="#scanf的返回值" class="headerlink" title="scanf的返回值"></a>scanf的返回值</h3><p><code>scanf()</code>的返回值是一个整数,表示成功读取的变量个数。<br>如果没有读取任何项,或者匹配失败,则返回<code>0</code>。<br>如果在成功读取任何数据之前,发生了读取错误或者遇到读取到文件结尾,则返回常量<code>EOF(-1)</code>。<br><code>EOF -end off ile文件结束标志</code></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> a = <span class="number">0</span> ;</span><br><span class="line"> <span class="type">int</span> b = <span class="number">0</span> ;</span><br><span class="line"> <span class="type">float</span> f = <span class="number">0.0f</span>;</span><br><span class="line"> <span class="type">int</span> r = <span class="built_in">scanf</span>(<span class="string">"%d %d %f"</span>, &a, &b, &f);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"a=%d b=%d f=%f\n"</span>, a, b, f);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"r = %d\n"</span>, r);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>输入输出测试</code>:</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/39254/image-4.png" alt="输入输出测试"></p><p><code>如果输入 2 个数后,按ctrl+z,提前结束输入</code>:</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/39254/image-5.png" alt="如果输入 2 个数后,按ctrl+z,提前结束输入"></p><p>在VS环境中按 3 次<code>ctrl+z</code>,才结束了输入,我们可以看到r是 2 ,表示正确读取了 2 个数值。</p><p>如果一个数字都不输入,直接按 3 次ctrl+z,输出的r是-1,也就是EOF</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/39254/image-6.png" alt="如果一个数字都不输入,直接按 3 次ctrl+z,输出的r是-1,也就是EOF"></p><h3 id="占位符-1"><a href="#占位符-1" class="headerlink" title="占位符"></a>占位符</h3><p><code>scanf()</code>常用的占位符如下,与<code>printf()</code>的占位符基本一致。</p><ul><li><code>%c</code>:字符。</li><li><code>%d</code>:整数。</li><li><code>%f</code>:float类型浮点数。</li><li><code>%lf</code>:double类型浮点数。</li><li><code>%Lf</code>:long double类型浮点数。</li><li><code>%s</code>:字符串。</li><li><code>%[]</code>:在方括号中指定一组匹配的字符(比如%[0-9]),遇到不在集合之中的字符,匹配将会停止。</li></ul><p>上面所有占位符之中, <strong>除了<code>%c</code>以外,都会自动忽略起首的空白字符 。<code>%c</code>不忽略空白字符,总是返回当前第一个字符,无论该字符是否为空格</strong>。</p><p><strong>如果要强制跳过字符前的空白字符</strong> ,可以写成<code>scanf(" %c", &ch)</code>,即%c前加上一个空格,表示跳过零个或多个空白字符。</p><p>下面要 <strong>特别说一下占位符</strong> <code>%s</code>,它其实不能简单地等同于字符串。它的规则是,从当前第一个非空白字符开始读起,直到遇到空白字符(即空格、换行符、制表符等)为止。</p><p>因为<code>%s</code>不会包含空白字符,所以无法用来读取多个单词,除非多个%s一起使用。这也意味着,<code>scanf()</code>不适合读取可能包含空格的字符串,比如书名或歌曲名。另外,<code>scanf()</code>遇到<code>%s</code>占位符,会在字符串变量末尾存储一个空字符<code>\0</code>。</p><p><code>scanf()</code>将字符串读入字符数组时,不会检测字符串是否超过了数组⻓度。所以,储存字符串时,很可能会超过数组的边界,导致预想不到的结果。为了防止这种情况,使用<code>%s</code>占位符时,应该指定读入字符串的最⻓⻓度,即写成<code>%[m]s</code>,其中的<code>[m]</code>是一个整数,表示读取字符串的最大⻓度,后面的字符将被丢弃。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">char</span> name[ <span class="number">11</span> ];</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%10s"</span>, name);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>上面示例中,<code>name</code>是一个⻓度为 11 的字符数组,<code>scanf()</code>的占位符%10s表示最多读取用戶输入<br>的 10 个字符,后面的字符将被丢弃,这样就不会有数组溢出的⻛险了。</p><h3 id="赋值忽略符"><a href="#赋值忽略符" class="headerlink" title="赋值忽略符"></a>赋值忽略符</h3><blockquote><p>有时,用戶的输入可能不符合预定的格式。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> year = <span class="number">0</span> ;</span><br><span class="line"> <span class="type">int</span> month = <span class="number">0</span> ;</span><br><span class="line"> <span class="type">int</span> day = <span class="number">0</span> ;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d-%d-%d"</span>, &year, &month, &day);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d %d %d\n"</span>, year, month, day);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>上面示例中,如果用戶输入<code>2020-01-01</code>,就会正确解读出年、月、日。问题是用戶可能输入其他格式,比如2020/01/01,这种情况下,scanf()解析数据就会失败。</p><p>为了避免这种情况,scanf()提供了一个 <strong>赋值忽略符</strong> (assignment suppression character)<code>*</code>。只要把*加在任何占位符的百分号后面,该占位符就不会返回值,解析后将被丢弃。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> year = <span class="number">0</span> ;</span><br><span class="line"> <span class="type">int</span> month = <span class="number">0</span> ;</span><br><span class="line"> <span class="type">int</span> day = <span class="number">0</span> ;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d%*c%d%*c%d"</span>, &year, &month, &day);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>上面示例中,<code>%*c</code>就是在占位符的百分号后面,加入了赋值忽略符<code>*</code>,表示这个占位符没有对应的变量,解读后不必返回。</p><blockquote><p><strong>完</strong></p></blockquote>]]></content>
<categories>
<category> C语言程序设计 </category>
</categories>
<tags>
<tag> C语言程序设计知识点总结 </tag>
</tags>
</entry>
<entry>
<title>第9讲:函数递归</title>
<link href="/posts/32969/"/>
<url>/posts/32969/</url>
<content type="html"><![CDATA[<blockquote><p><strong>正文开始</strong></p></blockquote><h1 id="递归是什么?"><a href="#递归是什么?" class="headerlink" title="递归是什么?"></a>递归是什么?</h1><blockquote><p>递归是学习 C 语言函数绕不开的一个话题,那什么是递归呢?</p></blockquote><p>递归其实是一种解决问题的方法,在 C 语言中,递归就是 函数自己调用自己 。</p><blockquote><p>写一个史上最简单的 C 语言递归代码:(><strong>这是一个错误的示范,会导致死循环,导致栈溢出。</strong>)</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"hehe\n"</span>);</span><br><span class="line">main();<span class="comment">//main函数中又调用了main函数</span></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>运行结果:</strong></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">hehe</span><br><span class="line">hehe</span><br><span class="line">hehe</span><br><span class="line">hehe</span><br><span class="line">hehe</span><br><span class="line">hehe</span><br><span class="line">hehe</span><br><span class="line"> *</span><br><span class="line"> *</span><br><span class="line"> * //死循环打印hehe</span><br></pre></td></tr></table></figure><blockquote><p><strong>这个代码的作用是什么呢?</strong></p></blockquote><ul><li>它会导致死循环,导致栈溢出。</li><li>它没有任何意义,只是打印”hehe”</li></ul><blockquote><p><strong>上述就是一个简单的递归程序,只不过上面的递归只是为了演示递归的基本形式,不是为了解决问题,代码最终也会陷入死递归,导致栈溢出(Stackoverflow)。</strong></p></blockquote><blockquote><p><strong>所以,递归的基本形式是:每一次函数调用,都会在栈上开辟一块内存,当递归层数太多时,会导致栈溢出。</strong></p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/32969/image.png" alt="alt text"></p><h2 id="递归的思想:"><a href="#递归的思想:" class="headerlink" title="递归的思想:"></a>递归的思想:</h2><blockquote><p>递归的思想是:把一个大型复杂问题——》分解成规模较小的子问题来求解</p></blockquote><blockquote><p>递归中的 <strong>递</strong>就是<strong>递推</strong> 的意思, <strong>归</strong>就是<strong>回归</strong> 的意思。</p></blockquote><h2 id="递归的限制条件"><a href="#递归的限制条件" class="headerlink" title="递归的限制条件"></a>递归的限制条件</h2><blockquote><p>递归在书写的时候,有 2 个必要条件:</p></blockquote><ul><li><p>递归存在限制条件,当满足这个限制条件的时候,递归便不再继续。</p></li><li><p>每次递归调用之后越来越接近这个限制条件。</p></li></ul><blockquote><p><strong>在下面的例子中,我们逐步体会这 2 个限制条件。</strong></p></blockquote><h1 id="递归举例"><a href="#递归举例" class="headerlink" title="递归举例"></a>递归举例</h1><h2 id="举例-1-:求-n-的阶乘"><a href="#举例-1-:求-n-的阶乘" class="headerlink" title="举例 1 :求 n 的阶乘"></a>举例 1 :求 n 的阶乘</h2><p>一个正整数的 <strong>阶乘</strong> ( <strong>factorial</strong> )是所有小于及等于该数的正整数的积,并且 0 的阶乘为 1 。自然数 n 的阶乘写作 n!。<br>题目:计算 n 的阶乘(不考虑溢出),n 的阶乘就是 1~n 的数字累积相乘。</p><h3 id="分析和代码实现"><a href="#分析和代码实现" class="headerlink" title="分析和代码实现"></a>分析和代码实现</h3><p>我们知道 n 的阶乘的公式:<strong>n !=^ n ∗( n −^1 )!</strong></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">举例:</span><br><span class="line"> 5! = 5 * 4 * 3 * 2 * 1</span><br><span class="line"> 4! = 4 * 3 * 2 * 1</span><br><span class="line">所以: 5! = 5 * 4!</span><br></pre></td></tr></table></figure><blockquote><p><strong>这样的思路就是把一个较大的问题,转换为一个与原问题相似,但规模较小的问题来求解的。</strong></p></blockquote><blockquote><p><strong>当 n==0 的时候,n 的阶乘是 1 ,其余 n 的阶乘都是可以通过公式计算。</strong></p></blockquote><blockquote><p><strong>n 的阶乘的递归公式如下:</strong></p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/32969/image-1.png" alt="alt text"></p><p><strong>那我们就可以写出函数 Fact 求 n 的阶乘,假设 Fact(n)就是求 n 的阶乘,那么 Fact(n-1)就是求 n-1 的阶乘,函数如下:</strong></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">Fact</span><span class="params">(<span class="type">int</span> n)</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span> (n == <span class="number">0</span>)</span><br><span class="line"><span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line"><span class="keyword">return</span> n * Fact(n - <span class="number">1</span>);</span><br><span class="line">}</span><br></pre></td></tr></table></figure><blockquote><p><strong>测试:</strong></p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">* 函数名: Fact</span></span><br><span class="line"><span class="comment">* 功能: 计算给定数的阶乘</span></span><br><span class="line"><span class="comment">* 参数:</span></span><br><span class="line"><span class="comment">* - n: int类型,要计算阶乘的非负整数</span></span><br><span class="line"><span class="comment">* 返回值: </span></span><br><span class="line"><span class="comment">* - int类型,n的阶乘值。如果n为0,则返回1。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">Fact</span><span class="params">(<span class="type">int</span> n)</span></span><br><span class="line">{</span><br><span class="line"> <span class="keyword">if</span> (n == <span class="number">0</span>) <span class="comment">// 当输入为0时,返回1</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="keyword">return</span> n * Fact(n - <span class="number">1</span>); <span class="comment">// 递归调用,计算n的阶乘</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> n = <span class="number">0</span>;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &n); <span class="comment">// 从标准输入读取一个整数</span></span><br><span class="line"> <span class="type">int</span> ret = Fact(n); <span class="comment">// 调用Fact函数,计算输入整数的阶乘</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, ret); <span class="comment">// 输出计算结果</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><blockquote><p><strong>运行结果:(这里不考虑 n 太大的情况,n 太大存在溢出):</strong></p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/32969/image-2.png" alt="alt text"></p><h4 id="画图推演"><a href="#画图推演" class="headerlink" title="画图推演"></a>画图推演</h4><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/32969/image-3.png" alt="alt text"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/32969/image-4.png" alt="alt text"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/32969/image-5.png" alt="alt text"></p><h2 id="举例-2-:顺序打印一个整数的每一位"><a href="#举例-2-:顺序打印一个整数的每一位" class="headerlink" title="举例 2 :顺序打印一个整数的每一位"></a>举例 2 :顺序打印一个整数的每一位</h2><blockquote><p><strong>输入一个整数 m,按照顺序打印整数的每一位。</strong></p></blockquote><p><strong>比如:</strong></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">输入:1234 输出:1 2 3 4</span><br><span class="line"></span><br><span class="line">输入:520 输出:5 2 0</span><br></pre></td></tr></table></figure><h3 id="分析和代码实现-1"><a href="#分析和代码实现-1" class="headerlink" title="分析和代码实现"></a>分析和代码实现</h3><blockquote><p><strong>这个题目,放在我们面前,首先想到的是,怎么得到这个数的每一位呢?</strong></p></blockquote><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">如果n是一位数,n的每一位就是n自己</span><br><span class="line">n是超过 1 位数的话,就得拆分每一位</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">1234%10就能得到 4 ,然后1234/10得到 123 ,这就相当于去掉了 4</span><br><span class="line">然后继续对123%10,就得到了 3 ,再除 10 去掉 3 ,以此类推</span><br><span class="line">不断的%10和/10操作,直到 1234 的每一位都得到;</span><br><span class="line">但是这里有个问题就是得到的数字顺序是倒着的</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">//假设n是1234</span><br><span class="line">1234 % 10 = 4</span><br><span class="line">1234 / 10 = 123</span><br><span class="line">123 % 10 = 3</span><br><span class="line">123 / 10 = 12</span><br><span class="line">12 % 10 = 2</span><br><span class="line">12 / 10 = 1</span><br><span class="line">1 % 10 = 1</span><br><span class="line">1 / 10 = 0</span><br></pre></td></tr></table></figure><blockquote><p><strong>但是我们有了灵感,我们发现其实一个数字的最低位是最容易得到的,通过% 10 就能得到那我们假设想写一个函数 Print 来打印 n 的每一位,如下表示:</strong></p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">Print(n)</span><br><span class="line">如果n是 <span class="number">1234</span> ,那表示为</span><br><span class="line">Print( <span class="number">1234</span> ) <span class="comment">//打印 1234 的每一位</span></span><br><span class="line"></span><br><span class="line">其中 <span class="number">1234</span> 中的 <span class="number">4</span> 可以通过% <span class="number">10</span> 得到,那么</span><br><span class="line">Print( <span class="number">1234</span> )就可以拆分为两步:</span><br><span class="line"><span class="number">1.</span> Print( <span class="number">1234</span> / <span class="number">10</span> ) <span class="comment">//打印 123 的每一位</span></span><br><span class="line"><span class="number">2.</span> <span class="built_in">printf</span>( <span class="number">1234</span> % <span class="number">10</span> ) <span class="comment">//打印 4</span></span><br><span class="line">完成上述 <span class="number">2</span> 步,那就完成了 <span class="number">1234</span> 每一位的打印</span><br><span class="line"></span><br><span class="line">那么Print( <span class="number">123</span> )又可以拆分为Print( <span class="number">123</span> / <span class="number">10</span> ) + <span class="built_in">printf</span>( <span class="number">123</span> % <span class="number">10</span> )</span><br></pre></td></tr></table></figure><blockquote><p><strong>以此类推下去,就有</strong></p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"> Print( <span class="number">1234</span> )</span><br><span class="line">==>Print( <span class="number">123</span> ) + <span class="built_in">printf</span>( <span class="number">4</span> )</span><br><span class="line">==>Print( <span class="number">12</span> ) + <span class="built_in">printf</span>( <span class="number">3</span> )</span><br><span class="line">==>Print( <span class="number">1</span> ) + <span class="built_in">printf</span>( <span class="number">2</span> )</span><br><span class="line">==><span class="built_in">printf</span>( <span class="number">1</span> )</span><br></pre></td></tr></table></figure><p><strong>直到被打印的数字变成一位数的时候,就不需要再拆分,递归结束。 那么代码完成也就比较清楚:</strong></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Print - 递归打印整数的每一位</span></span><br><span class="line"><span class="comment"> * @param n: 待打印的整数</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * 该函数通过递归的方式,从高位到低位打印出整数n的每一位数字。</span></span><br><span class="line"><span class="comment"> * 每个数字之间以一个空格分隔。</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">Print</span><span class="params">(<span class="type">int</span> n)</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 当n大于9时,递归打印n的每一位</span></span><br><span class="line"> <span class="keyword">if</span>(n > <span class="number">9</span>)</span><br><span class="line"> {</span><br><span class="line"> Print(n / <span class="number">10</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 打印n的最低位,并在后面加上一个空格</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d "</span>, n % <span class="number">10</span>);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> n = <span class="number">0</span>;</span><br><span class="line"> <span class="comment">// 从标准输入读取一个整数</span></span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &n);</span><br><span class="line"> <span class="comment">// 调用Print函数打印输入的整数的每一位</span></span><br><span class="line"> Print(n);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>输入和输出结果:</strong></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">123</span><br><span class="line">1 2 3</span><br></pre></td></tr></table></figure><p><strong>在这个解题的过程中,我们就是使用了大事化小的思路</strong></p><blockquote><p>把 Print(1234)打印 1234 每一位,拆解为首先 Print(123)打印 123 的每一位,再打印得到的 4<br>把 Print(123)打印 123 每一位,拆解为首先 Print(12)打印 12 的每一位,再打印得到的 3<br>直到 Print 打印的是一位数,直接打印就行。</p></blockquote><h4 id="画图推演-1"><a href="#画图推演-1" class="headerlink" title="画图推演"></a>画图推演</h4><p><strong>以 1234 每一位的打印来推演一下</strong></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/32969/image-6.png" alt="alt text"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/32969/image-7.png" alt="alt text"></p><h1 id="递归与迭代"><a href="#递归与迭代" class="headerlink" title="递归与迭代"></a>递归与迭代</h1><p>递归是一种很好的编程技巧,但是和很多技巧一样,也是可能被误用的,就像举例 1 一样,看到推导的<br>公式,很容易就被写成递归的形式:</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/32969/image-1.png" alt="alt text"></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">Fact</span><span class="params">(<span class="type">int</span> n)</span></span><br><span class="line">{</span><br><span class="line"> <span class="keyword">if</span>(n== <span class="number">0</span> )</span><br><span class="line"> <span class="keyword">return</span> <span class="number">1</span> ;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="keyword">return</span> n*Fact(n<span class="number">-1</span>);</span><br><span class="line">}</span><br></pre></td></tr></table></figure><blockquote><p>Fact 函数是可以产生正确的结果,但是在递归函数调用的过程中涉及一些运行时的开销。</p></blockquote><blockquote><p>递归函数调用的过程,是栈的一种数据结构,每一次函数调用,都会在栈上开辟一块内存,当递归层数太多时,会导致栈溢出。<br>在 C 语言中每一次函数调用,都需要为本次函数调用在内存的栈区,申请一块内存空间来保存函数调用期间的各种局部变量的值,这块空间被称为 <strong>运行时堆栈</strong> ,或者 <strong>函数栈帧</strong> 。<br>函数不返回,函数对应的栈帧空间就一直占用,所以如果函数调用中存在递归调用的话,每一次递归函数调用都会开辟属于自己的栈帧空间,直到函数递归不再继续,开始回归,才逐层释放栈帧空间。<br>所以如果采用函数递归的方式完成代码,递归层次太深,就会浪费太多的栈帧空间,也可能引起栈溢出(stackoverflow)的问题。</p></blockquote><blockquote><p><strong>注:关于 <strong>函数栈帧</strong> 的详细内容,鹏哥录制了视频专⻔讲解的,下课导入课程,自行学习。</strong></p></blockquote><blockquote><p>所以如果不想使用递归,就得想其他的办法,通常就是迭代的方式(通常就是循环的方式)。</p></blockquote><p>比如:计算<strong>n</strong>的阶乘,也是可以产生<strong>1~n</strong>的数字累计乘在一起的。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 计算给定数的阶乘。</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * @param n 需要计算阶乘的非负整数。</span></span><br><span class="line"><span class="comment"> * @return 返回 n 的阶乘结果。如果 n 为 0,则结果为 1。</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">Fact</span><span class="params">(<span class="type">int</span> n)</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> i = <span class="number">0</span>; <span class="comment">// 初始化循环变量 i 为 0</span></span><br><span class="line"> <span class="type">int</span> ret = <span class="number">1</span>; <span class="comment">// 初始化结果变量 ret 为 1,用于累积乘积</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">// 循环从 1 到 n,将每个数乘以累积的结果</span></span><br><span class="line"> <span class="keyword">for</span>(i = <span class="number">1</span>; i <= n; i++)</span><br><span class="line"> {</span><br><span class="line"> ret *= i; <span class="comment">// 累积乘积</span></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> ret; <span class="comment">// 返回最终的累积结果</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure><blockquote><p>上述代码是能够完成任务,并且效率是比递归的方式更好的。<br>事实上,我们看到的许多问题是以递归的形式进行解释的,这只是因为它比非递归的形式更加清晰,<br>但是这些问题的迭代实现往往比递归实现效率更高。<br>当一个问题非常复杂,难以使用迭代的方式实现时,此时递归实现的简洁性便可以补偿它所带来的运行时开销。</p></blockquote><h1 id="举例-3-:求第-n-个斐波那契数"><a href="#举例-3-:求第-n-个斐波那契数" class="headerlink" title="举例 3 :求第 n 个斐波那契数"></a>举例 3 :求第 n 个斐波那契数</h1><p>我们也能举出更加极端的例子,就像计算第 n 个斐波那契数,是不适合使用递归求解的,但是斐波那契<br>数的问题通过是使用递归的形式描述的,如下:</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/32969/image-8.png" alt="alt text"></p><blockquote><p>看到这公式,很容易诱导我们将代码写成递归的形式,如下所示:</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">Fib</span><span class="params">(<span class="type">int</span> n)</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span> (n <= <span class="number">2</span>)</span><br><span class="line"><span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line"><span class="keyword">return</span> Fib(n - <span class="number">1</span>) + Fib(n - <span class="number">2</span>);</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>测试代码:</strong></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> n = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">scanf</span>(<span class="string">"%d"</span>, &n);</span><br><span class="line"><span class="type">int</span> ret = Fib(n);</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d\n"</span>, ret);</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><blockquote><p>当我们 n 输入为 50 的时候,需要很⻓时间才能算出结果,这个计算所花费的时间,是我们很难接受的,<br>这也说明递归的写法是非常低效的,那是为什么呢?</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/32969/image-9.png" alt="alt text"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/32969/image-10.png" alt="alt text"></p><blockquote><p><strong>其实递归程序会不断的展开,在展开的过程中,我们很容易就能发现,在递归的过程中会有重复计算,而且递归层次越深,冗余计算就会越多。我们可以作业测试:</strong></p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> count = <span class="number">0</span>; <span class="comment">// 用于统计第3个斐波那契数被计算的次数</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 计算第n个斐波那契数</span></span><br><span class="line"><span class="comment"> * @param n 斐波那契数的序号,从0开始计数</span></span><br><span class="line"><span class="comment"> * @return 返回第n个斐波那契数的值</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">Fib</span><span class="params">(<span class="type">int</span> n)</span></span><br><span class="line">{</span><br><span class="line"> <span class="keyword">if</span> (n == <span class="number">3</span>)</span><br><span class="line"> count++; <span class="comment">// 统计第3个斐波那契数被计算的次数</span></span><br><span class="line"> <span class="keyword">if</span> (n <= <span class="number">2</span>)</span><br><span class="line"> <span class="keyword">return</span> <span class="number">1</span>; <span class="comment">// 当n小于等于2时,直接返回1</span></span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="keyword">return</span> Fib(n - <span class="number">1</span>) + Fib(n - <span class="number">2</span>); <span class="comment">// 递归计算第n个斐波那契数</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> n = <span class="number">0</span>;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &n); <span class="comment">// 从输入读取要计算的斐波那契数的序号</span></span><br><span class="line"> <span class="type">int</span> ret = Fib(n); <span class="comment">// 调用Fib函数计算斐波那契数</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, ret); <span class="comment">// 输出计算结果</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"\ncount = %d\n"</span>, count); <span class="comment">// 输出第3个斐波那契数被计算的次数</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>输出结果:</strong></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/32969/image-11.png" alt="alt text"></p><blockquote><p><strong>这里我们看到了,在计算第 40 个斐波那契数的时候,使用递归方式,第 3 个斐波那契数就被重复计算了<br>39088169 次,这些计算是非常冗余的。所以斐波那契数的计算,使用递归是非常不明智的,我们就得想迭代的方式解决。</strong></p></blockquote><blockquote><p><strong>我们知道斐波那契数的前 2 个数都 1 ,然后前 2 个数相加就是第 3 个数,那么我们从前往后,从小到大计算就行了。</strong></p></blockquote><p><strong>这样就有下面的代码:</strong></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 计算斐波那契数列的第n个数。</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * @param n 斐波那契数列的位置,从0开始计数。</span></span><br><span class="line"><span class="comment"> * @return 返回斐波那契数列的第n个数值。</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">Fib</span><span class="params">(<span class="type">int</span> n)</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 初始化斐波那契数列的前三个数</span></span><br><span class="line"><span class="type">int</span> a = <span class="number">1</span>;</span><br><span class="line"><span class="type">int</span> b = <span class="number">1</span>;</span><br><span class="line"><span class="type">int</span> c = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 当n大于2时,循环计算斐波那契数列的值</span></span><br><span class="line"><span class="keyword">while</span> (n > <span class="number">2</span>)</span><br><span class="line">{</span><br><span class="line">c = a + b; <span class="comment">// 计算新的斐波那契数值</span></span><br><span class="line">a = b; <span class="comment">// 更新a为前一个斐波那契数值</span></span><br><span class="line">b = c; <span class="comment">// 更新b为当前斐波那契数值</span></span><br><span class="line">n--; <span class="comment">// n减1,继续下一次循环</span></span><br><span class="line">}</span><br><span class="line"><span class="keyword">return</span> c; <span class="comment">// 返回第n个斐波那契数值</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure><blockquote><p><strong>迭代的方式去实现这个代码,效率就要高出很多了。</strong></p></blockquote><blockquote><p><strong>有时候,递归虽好,但是也会引入一些问题,所以我们一定不要迷恋递归,适可而止就好。</strong></p></blockquote><h1 id="拓展学习:"><a href="#拓展学习:" class="headerlink" title="拓展学习:"></a>拓展学习:</h1><ul><li>⻘蛙跳台阶问题</li></ul><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/32969/image-12.png" alt="alt text"></p><ul><li>汉诺塔问题</li></ul><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/32969/image-13.png" alt="alt text"></p><blockquote><p>以上 2 个问题都可以使用递归很好的解决,有兴趣可以研究。</p></blockquote><blockquote><p>课堂板书:</p></blockquote><p><a href="https://gitee.com/bitpg/class114training-camp-phase-5/blob/master/2024-04-22-%E6%9D%BF%E4%B9%A6.png">https://gitee.com/bitpg/class114training-camp-phase-5/blob/master/2024-04-22-%E6%9D%BF%E4%B9%A6.png</a></p><blockquote><p>课堂代码:</p></blockquote><p><a href="https://gitee.com/bitpg/class114training-camp-phase-5/blob/master/test_4_22/test_4_22/test.c">https://gitee.com/bitpg/class114training-camp-phase-5/blob/master/test_4_22/test_4_22/test.c</a></p><blockquote><p><strong>完</strong></p></blockquote>]]></content>
<categories>
<category> C语言程序设计 </category>
</categories>
<tags>
<tag> C语言程序设计知识点总结 </tag>
</tags>
</entry>
<entry>
<title>第1讲:C语言常⻅概念</title>
<link href="/posts/26817/"/>
<url>/posts/26817/</url>
<content type="html"><![CDATA[<blockquote><p><strong>正文开始</strong></p></blockquote><h1 id="C语言是什么?"><a href="#C语言是什么?" class="headerlink" title="C语言是什么?"></a>C语言是什么?</h1><p>人和人交流使用的是自然语言,如:汉语、英语、日语</p><p>那人和计算机是怎么交流的呢?使用 计算机语言 。</p><p>目前已知已经有上千种计算机语言,人们是通过计算机语言写的程序,给计算机下达指令,让计算机工作的。</p><p>C语言就是众多计算机语言中的一种,当然C++/Java/Go/Python都是计算机语言。</p><h1 id="C语言的历史和辉煌"><a href="#C语言的历史和辉煌" class="headerlink" title="C语言的历史和辉煌"></a>C语言的历史和辉煌</h1><p>C语言最初是作为Unix系统的开发工具而发明的。</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/26817/image.png" alt="alt text"><br><a href="https://www.tiobe.com/tiobe-index/">https://www.tiobe.com/tiobe-index/</a></p><h1 id="编译器的选择VS-2022"><a href="#编译器的选择VS-2022" class="headerlink" title="编译器的选择VS 2022"></a>编译器的选择VS 2022</h1><h2 id="编译和链接"><a href="#编译和链接" class="headerlink" title="编译和链接"></a>编译和链接</h2><p>C语言是一⻔ 编译型 计算机语言,C语言源代码都是文本文件,文本文件本身无法执行,必须通过 编译器 翻译和 链接器 的链接,生成 二进制的可执行文件 ,可执行文件才能执行。</p><p>C语言代码是放在 **.c ***<em>为后缀的文件中的,要得到最终运行的可执行程序,中间要经过 <strong>编译</strong></em> 和 *<strong>链接</strong> 2 个<br>过程。</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/26817/image-1.png" alt="alt text"><br>VS2022项目中的.c文件 </p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/26817/image-2.png" alt="alt text"><br>.c文件的展示</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/26817/image-3.png" alt="alt text"><br>C语言代码</p><p>一个工程一般都会有多个源文件组成,如下图所示,演示了源程序经过编译器和链接器处理的过程。<br><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/26817/image-21.png" alt="alt text"> <img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/26817/image-4.png" alt="alt text"><br>注:</p><ol><li>每个源文件(.c)单独经过 编译器 处理生成对应的目标文件(.obj为后缀的文件)</li><li>多个目标文件和库文件经过 链接器 处理生成对应的可执行程序(.exe文件)</li></ol><p>这就是,在Windows电脑上C语言程序生成的exe可执行文件<br><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/26817/image-5.png" alt="alt text"><br>可执行程序</p><h3 id="编译器的对比"><a href="#编译器的对比" class="headerlink" title="编译器的对比"></a>编译器的对比</h3><p>C语言是一⻔ 编译型 的计算机语言,需要依赖编译器将计算机语言转换成机器能够执行的机器指令。<br>那我们常⻅的C语言编译器都有哪些呢?</p><p>比如: <strong>msvc</strong> 、<strong>clang</strong>、<strong>gcc</strong>就是一些常⻅的编译器,当然也有一些 集成开发环境如<strong>VS2022</strong>、<br><strong>XCode</strong>、<strong>CodeBlocks</strong>、<strong>DevC++<strong>、</strong>Clion</strong>等。</p><blockquote><p>集成开发环境(IDE)用于提供程序开发环境的应用程序,一般包括代码编辑器、编译器、调试器和图形用戶界面等工具。集成了代码编写功能、分析功能、编译功能、调试功能等一体化的开发软件服务套。</p></blockquote><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/26817/image-6.png" alt="alt text"> VS2022 <img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/26817/image-7.png" alt="alt text"> XCode <img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/26817/image-8.png" alt="alt text"> CodeBlocks <img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/26817/image-9.png" alt="alt text"> DevC++ <img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/26817/image-10.png" alt="alt text"> Clion</p><ul><li><strong>VS2022</strong> 集成了MSVC(安装报包较大一些,安装简单,无需多余配置,使用起来非常方便)</li><li><strong>XCode</strong> 集成了clang(苹果电脑上的开发工具)</li><li><strong>CodeBlocks</strong> 集成了gcc(这个工具比较小众,需要配置环境,不太推荐)</li><li><strong>DevC++</strong> 集成了gcc(小巧,但是工具过于简单,对于代码⻛格的养成不好,一些竞赛使用)</li><li><strong>Clion</strong>是默认使用CMake,编译器是可以配置的(工具是收费,所以暂时推荐大家使用)</li></ul><p>整体考虑,推荐大家安装** VS2022 **的社区版本学习 ,免费,使用方便,工作中常⻅。<br>VS2022的安装教程:<a href="https://www.bilibili.com/video/BV11R4y1s7jz/">https://www.bilibili.com/video/BV11R4y1s7jz/</a></p><h4 id="VS2022的优缺点"><a href="#VS2022的优缺点" class="headerlink" title="VS2022的优缺点"></a>VS2022的优缺点</h4><p><strong>优点:</strong></p><ul><li><strong>VS2022</strong>是一个主流的集成开发环境,企业中使用较为普遍</li><li><strong>VS2022</strong>包含了:编辑器+编译器+调试器,功能强大</li><li>直接安装即可使用,基本不用额外配置环境,上手容易</li><li>默认界面是中文的,初学者友好</li></ul><p><strong>缺点:</strong></p><ul><li>功能丰富,安装包大,占用空间多。</li></ul><h1 id="VS项目和源文件、头文件介绍"><a href="#VS项目和源文件、头文件介绍" class="headerlink" title="VS项目和源文件、头文件介绍"></a>VS项目和源文件、头文件介绍</h1><p>在VS上写代码,我们是需要创建项目的,直接新建项目就可以了。<br>在项目中就可以添加源文件和头文件。<br>C语言把 <strong>.c</strong>为后缀的文件称为 <strong>源文件</strong> ,把 <strong>.h</strong>为后缀的文件称为 <strong>头文件</strong> 。</p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/26817/image-11.png" alt="alt text"> 头文件和源文件在VS中展示 <img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/26817/image-12.png" alt="alt text"> 头文件和源文件在磁盘上</p><h1 id="第一个C语言程序"><a href="#第一个C语言程序" class="headerlink" title="第一个C语言程序"></a>第一个C语言程序</h1><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"hello C\n"</span>);</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>这里演示VS2022中创建项目和编写C代码的过程,并运行出结果。<br>在VS2022上运行代码的快捷键:<strong>Ctrl+f5</strong></p><h1 id="main函数"><a href="#main函数" class="headerlink" title="main函数"></a>main函数</h1><p>每个C语言程序不管有多少行代码,都是从<strong>main</strong>函数开始执行的,<strong>main</strong>函数是程序的入口,<br><strong>main</strong>函数也被叫做: <strong>主函数</strong> 。<strong>main</strong>前面的<strong>int</strong>表示<strong>main</strong>函数执行结束的时候返回一个整型类型的值。所以在<strong>main</strong>函数的最后写<strong>return 0</strong>;正好前后呼应。</p><ul><li>main函数是程序的入口</li><li>main函数有且仅有一个</li><li>即使一个项目中有多个.c文件,但是只能有一个main函数(因为程序的入口只能有一个)<br><em><strong>第一次写代码,一些常⻅的错误总结:</strong></em></li><li><strong>main</strong>被写成了<strong>mian</strong></li><li><strong>main</strong>后边的 **()**漏掉了</li><li><strong>代码中不能使用中文符号,比如括号和分号</strong></li><li><strong>一条语句结束后,有分号</strong></li></ul><h1 id="printf和库函数"><a href="#printf和库函数" class="headerlink" title="printf和库函数"></a>printf和库函数</h1><p> 在上面的代码中有一句代码如下:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span> <span class="built_in">printf</span>(<span class="string">"hello C\n"</span>);</span><br></pre></td></tr></table></figure><p>代码中使用了<strong>printf</strong>函数,实现了在屏幕上的信息的打印。<br>这里简单的介绍一下<strong>printf</strong>,<strong>printf</strong>是一个 <strong>库函数</strong> ,它的功能是在标准输出设备(一般指屏<br>幕)上进行信息的打印。上面的代码是使用<strong>printf</strong>函数打印字符串。只要把想要打印的一串字符放<br>在双引号中并传递给printf函数就可以打印。</p><p>printf函数也可以用来打印其他类型的数据,比如:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> n = <span class="number">100</span> ;</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d\n"</span>, n); <span class="comment">//printf打印整型</span></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%c\n"</span>, <span class="string">'q'</span>); <span class="comment">//printf打印字符</span></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%lf\n"</span>, <span class="number">3.14</span>); <span class="comment">//printf打印双精度浮点型</span></span><br></pre></td></tr></table></figure><p>这里的 <strong>%d</strong>,<strong>%c</strong>等是 <strong>占位符</strong> ,会被后边的值替换。(后期课程再介绍)<br>同时我们在使用库函数的时候,是需要包含头文件的,比如:<strong>printf</strong>函数需要包含的就是<br><strong>stdio.h</strong>这个头文件,具体的方法就是:</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br></pre></td></tr></table></figure><p><strong>那什么是库函数呢?</strong><br>为了不再重复实现常⻅的代码,让程序员提升开发效率,C语言标准规定了一组函数,这些函数再由不同的编译器厂商根据标准进行实现,提供给程序员使用。这些函数组成了一个函数库,被称为 标准库 ,这些函数也被称为库函数。在这个基础上一些编译器厂商可能会额外扩展提供部分函数(这些函数其他编译器不一定支持)。</p><p>一个系列的库函数一般会声明在同一个头文件中,所以库函数的使用,要包含对应的头文件。<br>库函数比较多,后期慢慢来介绍,提前了解可参考链接:<a href="https://cplusplus.com/reference/clibrary/">https://cplusplus.com/reference/clibrary/</a></p><h1 id="关键字介绍"><a href="#关键字介绍" class="headerlink" title="关键字介绍"></a>关键字介绍</h1><p>C语言中有一批保留的名字的符号,比如:int、if、return,这些符号被称为 <strong>保留字</strong> 或者关键<br>字。</p><ul><li>关键字都有特殊的意义,是保留给C语言使用的</li><li>程序员自己在创建标识符的时候是不能和关键字重复的</li><li>关键字也是不能自己创建的。<br>C语言的 32 个关键字如下:</li></ul><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">auto</span> <span class="keyword">break</span> <span class="keyword">case</span> <span class="type">char</span> <span class="type">const</span> <span class="keyword">continue</span> <span class="keyword">default</span> <span class="keyword">do</span> <span class="type">double</span> <span class="keyword">else</span> <span class="keyword">enum</span></span><br><span class="line"><span class="keyword">extern</span></span><br><span class="line"><span class="type">float</span> <span class="keyword">for</span> <span class="keyword">goto</span> <span class="keyword">if</span> <span class="type">int</span> <span class="type">long</span> <span class="keyword">register</span> <span class="keyword">return</span> <span class="type">short</span> <span class="type">signed</span> <span class="keyword">sizeof</span></span><br><span class="line"><span class="type">static</span></span><br><span class="line"><span class="keyword">struct</span> <span class="keyword">switch</span> <span class="keyword">typedef</span> <span class="keyword">union</span> <span class="type">unsigned</span> <span class="type">void</span> <span class="keyword">volatile</span> <span class="keyword">while</span></span><br></pre></td></tr></table></figure><p>注:在C99标准中加入了inline、restrict、_Bool、_Complex、_Imaginary等关键<br>字。<br>一些关键字大家可以去了解一下,不过使用最多的还是上面的 32 个关键字。<br>注:<a href="https://zh.cppreference.com/w/c/keyword%EF%BC%88C%E8%AF%AD%E8%A8%80%E5%85%B3%E9%94%AE%E5%AD%97%E7%9A%84%E5%85%A8%E9%83%A8%E4%BB%8B%E7%BB%8D%EF%BC%89">https://zh.cppreference.com/w/c/keyword(C语言关键字的全部介绍)</a></p><p>后期我们讲课的过程中,会慢慢介绍这些常用的关键字的。</p><h1 id="字符和ASCII编码"><a href="#字符和ASCII编码" class="headerlink" title="字符和ASCII编码"></a>字符和ASCII编码</h1><p>在键盘上可以敲出各种字符,如:a,q,@,#等,这些符号都被称为 <strong>字符</strong> ,C语言中字符是用单引号括起来的,如:’a’,’b’,‘@’。</p><blockquote><p>我们知道在计算机中所有的数据都是以二进制的形式存储的,那这些字符在内存中分别以什么样的二进制存储的呢?</p></blockquote><p>如果我们每个人自己给这些字符中的每个字符编一个二进制序列,这个叫做 <strong>编码</strong> ,为了方便大家相互通信,不造成混乱,后来美国国家标准学会(ANSI)出台了一个标准 <strong>ASCII编码</strong> ,C语言中的字符就遵循了ASCII编码的方式。</p><p>ASCII编码表<br><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/26817/image-13.png" alt="alt text"><br>参考:<a href="https://zh.cppreference.com/w/cpp/language/ascii">https://zh.cppreference.com/w/cpp/language/ascii</a></p><p>我们不需要记住所有的ASCII码表中的数字,使用时查看就可以,不过我们最好能掌握几组特殊的数据:</p><ul><li>字符A<del>Z的ASCII码值从65</del>90</li><li>字符a<del>z的ASCII码值从97</del>122</li><li>对应的大小写字符(a和A)的ASCII码值的差值是32</li><li>数字字符0<del>9的ASCII码值从48</del>57</li><li>换行\n的ASCII值是:10 </li><li>在这些字符中ASCII码值从0~31这32 个字符是不可打印字符,无法打印在屏幕上观察</li></ul><blockquote><p>单个字符的打印可以使用%c来指定格式:</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 主函数</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * 本函数无参数。</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * @return 返回0,表示程序正常结束。</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 打印字符Q</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%c\n"</span>, <span class="string">'Q'</span>);</span><br><span class="line"> <span class="comment">// 打印字符Q的ASCII码值</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%c\n"</span>, <span class="number">81</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><blockquote><p>可打印字符展示:</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 主函数</span></span><br><span class="line"><span class="comment"> * 该函数没有参数。</span></span><br><span class="line"><span class="comment"> * 返回值: 总是返回0,表示程序正常结束。</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> i = <span class="number">0</span>; <span class="comment">// 初始化循环变量i为0</span></span><br><span class="line"><span class="keyword">for</span> (i = <span class="number">32</span>; i <= <span class="number">127</span>; i++) <span class="comment">// 循环遍历ASCII码从32到127的所有字符</span></span><br><span class="line">{</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%c "</span>, i); <span class="comment">// 打印当前ASCII码对应的字符</span></span><br><span class="line"><span class="keyword">if</span> (i % <span class="number">16</span> == <span class="number">15</span>) <span class="comment">// 检查当前字符是否是每行的最后一个字符(每行打印16个字符)</span></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"\n"</span>); <span class="comment">// 如果是,则换行</span></span><br><span class="line">}</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>; <span class="comment">// 程序正常结束,返回0</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>运行结果如下</strong><br><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/26817/image-14.png" alt="alt text"></p><h1 id="字符串和-0"><a href="#字符串和-0" class="headerlink" title="字符串和\ 0"></a>字符串和\ 0</h1><p>C语言中如何表示字符串呢? <strong>使用双引号括起来的一串字符就被称为字符串</strong> ,如:”abcdef”,就是一个<br>字符串。</p><blockquote><p>字符串的打印格式可以使用%s来指定,也可以直接打印如下:</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 程序入口主函数</span></span><br><span class="line"><span class="comment"> * 无参数</span></span><br><span class="line"><span class="comment"> * 返回值: 总是返回0,表示程序正常结束</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 输出字符串"hello C"</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%s\n"</span>, <span class="string">"hello C"</span>);</span><br><span class="line"> </span><br><span class="line"> <span class="comment">// 再次输出字符串"hello c"</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"hello c"</span>);</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>; <span class="comment">// 程序正常结束,返回0</span></span><br><span class="line">}```</span><br><span class="line">**C语言字符串中一个特殊的知识,就是在字符串的末尾隐藏放着一个\<span class="number">0</span>字符,这个\<span class="number">0</span>字符是字符串的结束标志。**</span><br><span class="line">![alt text](./第<span class="number">1</span>讲:C语言常⻅概念/image<span class="number">-15.</span>png)</span><br><span class="line">VS2022的监视窗口观察字符串</span><br><span class="line"></span><br><span class="line">对于字符串<span class="string">"abcdef"</span>,我们实际上看到了 <span class="number">6</span> 个字符:a,b,c,d,e,f,但是实际上在末尾还隐藏一个 **\<span class="number">0</span>**的</span><br><span class="line">转义字符,**\<span class="number">0</span>**是 **字符串的结束标志** 。所以我们在使用库函数**<span class="built_in">printf</span>()**打印字符串或者</span><br><span class="line">**<span class="built_in">strlen</span>()**计算字符串⻓度的时候,遇到 **\<span class="number">0</span>** 的时候就自动停止了。</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">>C语言中也可以把一个字符串放在一个字符数组中,我们在这里利用下面的代码验证一下 **\<span class="number">0</span>**的功能。</span><br><span class="line"></span><br><span class="line">```c</span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 主函数</span></span><br><span class="line"><span class="comment"> * 该函数无参数,无返回值。主要功能是演示字符数组和字符串的输出。</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 定义并初始化一个字符数组arr1,包含字符 'a', 'b', 'c'</span></span><br><span class="line"> <span class="type">char</span> arr1[] = { <span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span> };</span><br><span class="line"> <span class="comment">// 定义并初始化一个字符数组arr2,内容为字符串"abc"</span></span><br><span class="line"> <span class="type">char</span> arr2[] = <span class="string">"abc"</span>;</span><br><span class="line"> </span><br><span class="line"> <span class="comment">// 输出字符数组arr1,实际上由于末尾没有'\0',输出会直到遇到'\0'或内存的边界</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%s\n"</span>, arr1);</span><br><span class="line"> <span class="comment">// 输出字符数组arr2,这是一个标准字符串,会正确输出到'\0'为止</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%s\n"</span>, arr2);</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>; <span class="comment">// 程序正常结束</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>这样的代码,我调试的时候,观察一下arr1和arr2的内容:<br><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/26817/image-16.png" alt="alt text"><br>arr1和arr2中内容的对比</p><p>运行结果:<br><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/26817/image-17.png" alt="alt text"></p><blockquote><p>我们可以看到,<strong>arr1</strong>字符数组在打印的时候,打印了<strong>a</strong>、<strong>b</strong>、<strong>c</strong>后还打印了一些随机值,这就是<br>因为<strong>arr1</strong>在末尾的地方没有\0字符作为结束标志,在打印的时候没有停止。<br>但是<strong>arr2</strong>的打印就是完全正常的,就是因为<strong>arr2</strong>数组是使用字符串常量初始化的,数组中有<br>作为技术标志,打印可以正常停止。</p></blockquote><blockquote><p>如果我们在arr1数组中单独放一个 **’\0’**字符会怎么样呢?</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 主函数</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * 本函数无参数。</span></span><br><span class="line"><span class="comment"> * 本函数返回一个整数,表示程序执行的结果。</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 定义并初始化一个包含字符 'a', 'b', 'c' 和空字符的字符数组</span></span><br><span class="line"><span class="type">char</span> arr1[] = { <span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>, <span class="string">'\0'</span> };</span><br><span class="line"> <span class="comment">// 定义并初始化一个包含字符串 "abc" 的字符数组</span></span><br><span class="line"><span class="type">char</span> arr2[] = <span class="string">"abc"</span>;</span><br><span class="line"> <span class="comment">// 打印 arr1 数组中的字符串</span></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%s\n"</span>, arr1);</span><br><span class="line"> <span class="comment">// 打印 arr2 数组中的字符串</span></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%s\n"</span>, arr2);</span><br><span class="line"> <span class="comment">// 打印 字符串字面量 "abc\0def",其中的 '\0' 作为字符串结束符</span></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%s\n"</span>, <span class="string">"abc\0def"</span>);</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>; <span class="comment">// 程序执行成功,返回 0</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>运行结果:<br><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/26817/image-18.png" alt="alt text"></p><blockquote><p>看到三次打印的结果是一样的了,都是打印到 <strong>\0</strong>的时候就停止了,那从上述的例子我们确实能够观察到 <strong>\0</strong> 的作用和重要性的。</p></blockquote><h1 id="转义字符"><a href="#转义字符" class="headerlink" title="转义字符"></a>转义字符</h1><p>也许在前面的代码中你看到 <strong>\n</strong>,<strong>\0</strong>很纳闷是啥。其实在字符中有一组特殊的字符是 <strong>转义字符</strong> ,转义字符顾名思义:转变原来的意思的字符。</p><blockquote><p>比如:我们有字符<strong>n</strong>,在字符串中打印的时候自然能打印出这个字符,如下:</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 主函数</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * 本函数无参数。</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * @return 返回0,表示程序正常结束。</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 打印字符串"abcndef"</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"abcndef"</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>运行结果:<br><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/26817/image-19.png" alt="alt text"></p><blockquote><p>如果我们修改一下代码,在<strong>n</strong>的前面加上*,变成如下代码:</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 主函数</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * 本函数无参数。</span></span><br><span class="line"><span class="comment"> * 本函数返回一个整数,固定为0,表示程序正常结束。</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 打印字符串"abc"后跟一个换行符,接着打印"def"</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"abc\ndef"</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span> ; <span class="comment">// 程序正常结束,返回0</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>输出的结果:<br><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/26817/image-20.png" alt="alt text"></p><blockquote><p>我们可以看到修改的前后代码输出的结果,截然不同的,那这是为什么呢?</p></blockquote><p>这就是转义字符的问题,<strong>\n</strong>是一个转义字符表示换行的意思,我们可以简单的理解为*让<strong>n</strong>的意思发生了转变,<strong>n</strong>本来是一个普通的字符,被*转义为换行的意思。</p><p>C语言中像这样的转义字符还有一些,具体如下:</p><ul><li><strong>?</strong>:在书写连续多个问号时使用,防止他们被解析成三字母词,在新的编译器上没法验证了。</li><li><strong>'</strong>:用于表示字符常量’</li><li><strong>"</strong>:用于表示一个字符串内部的双引号</li><li><strong>\</strong>:用于表示一个反斜杠,防止它被解释为一个转义序列符。</li><li><strong>\a</strong>:警报,这会使得终端发出警报声或出现闪烁,或者两者同时发生。</li><li><strong>\b</strong>:退格键,光标回退一个字符,但不删除字符。</li><li><strong>\f</strong>:换⻚符,光标移到下一⻚。在现代系统上,这已经反映不出来了,行为改成类似于 <strong>\v</strong>。</li><li><strong>\n</strong>:换行符。</li><li><strong>\r</strong>:回⻋符,光标移到同一行的开头。</li><li><strong>\t</strong>:制表符,光标移到下一个水平制表位,通常是下一个4/8的倍数。</li><li><strong>\v</strong>:垂直分隔符,光标移到下一个垂直制表位,通常是下一行的同一列。</li></ul><p>下面 2 种转义字符可以理解为:字符的 8 进制或者 16 进制表示形式</p><ul><li><strong>\ddd</strong>:ddd表示1~3个八进制的数字。如:\130表示字符X</li><li><strong>\xdd</strong>:dd表示^2 个十六进制数字。如:\x30表示字符^0<br>\ 0 :null字符,代表没有内容,<strong>\0</strong>就是 <strong>\ddd</strong>这类转义字符的一种,用于字符串的结束标志,其ASCII码值是0.</li></ul><blockquote><p>代码演示:</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 主函数</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 打印单引号</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%c\n"</span>, <span class="string">'\''</span>);</span><br><span class="line"> <span class="comment">// 打印双引号</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%s\n"</span>, <span class="string">"\""</span>);</span><br><span class="line"> <span class="comment">// 打印路径</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"c:\\test\\code\\test.c\n"</span>);</span><br><span class="line"> <span class="comment">// 发出警报声</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"\a"</span>);</span><br><span class="line"> <span class="comment">// 打印字符'X'</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%c\n"</span>, <span class="string">'\130'</span>); <span class="comment">//130是 8 进制,转换成 10 进制是 88 ,以 88 作为ASCII码值的字符是'X'</span></span><br><span class="line"> <span class="comment">// 打印字符'0'</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%c\n"</span>, <span class="string">'\x30'</span>); <span class="comment">//x30中的 30 是 16 进制,转换成 10 进制是 48 ,以 48 作为ASCII码值的字符是'0'</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>这些ASCII码值是可以自己写代码验证的,大家也可以自己验证。<br>关于转义字符我们首先要了解,然后要能在字符串中识别出来。</p><p>转义字符参考:<a href="https://zh.cppreference.com/w/c/language/escape">https://zh.cppreference.com/w/c/language/escape</a></p><h1 id="语句和语句分类"><a href="#语句和语句分类" class="headerlink" title="语句和语句分类"></a>语句和语句分类</h1><p>C语言的代码是由一条一条的 语句 构成的,C语言中的语句可为以下五类:</p><ul><li>空语句</li><li>表达式语句</li><li>函数调用语句</li><li>复合语句</li><li>控制语句</li></ul><h2 id="空语句"><a href="#空语句" class="headerlink" title="空语句"></a>空语句</h2><p><strong>空语句是最简单的,一个分号就是一条语句,是空语句。</strong></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line">;<span class="comment">//空语句</span></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><blockquote><p>空语句,一般出现的地方是:这里需要一条语句,但是这个语句不需要做任何事,就可以写一个空语句。</p></blockquote><h3 id="表达式语句"><a href="#表达式语句" class="headerlink" title="表达式语句"></a>表达式语句</h3><blockquote><p>表达式语句就是在表达式的后边加上分号。如下所示:</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> a = <span class="number">20</span>;</span><br><span class="line"><span class="type">int</span> b = <span class="number">0</span>;</span><br><span class="line">b = a + <span class="number">5</span>; <span class="comment">//表达式语句</span></span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h3 id="函数调用语句"><a href="#函数调用语句" class="headerlink" title="函数调用语句"></a>函数调用语句</h3><blockquote><p>函数调用的时候,也会加上分号,就是函数调用语句。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 函数:Add</span></span><br><span class="line"><span class="comment"> * 功能:对两个整数进行相加并返回结果。</span></span><br><span class="line"><span class="comment"> * 参数:</span></span><br><span class="line"><span class="comment"> * - x: 整数,要相加的第一个数。</span></span><br><span class="line"><span class="comment"> * - y: 整数,要相加的第二个数。</span></span><br><span class="line"><span class="comment"> * 返回值:两个整数的和。</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">Add</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span></span><br><span class="line">{</span><br><span class="line"> <span class="keyword">return</span> x + y;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 主函数</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">// 打印"hehe"</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"hehe\n"</span>);</span><br><span class="line"> </span><br><span class="line"> <span class="comment">// 调用Add函数,并将结果赋给ret</span></span><br><span class="line"> <span class="type">int</span> ret = Add(<span class="number">2</span>, <span class="number">3</span>);</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h4 id="复合语句"><a href="#复合语句" class="headerlink" title="复合语句"></a>复合语句</h4><blockquote><p>复合语句其实就是前面讲过的代码块,成对括号中的代码就构成一个代码块,也被称为复合语句。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 打印数组元素</span></span><br><span class="line"><span class="comment"> * @param arr 整型数组,待打印的数组</span></span><br><span class="line"><span class="comment"> * @param sz 数组大小,表示arr数组中元素的个数</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">print</span><span class="params">(<span class="type">int</span> arr[], <span class="type">int</span> sz)</span> </span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">for</span> (i = <span class="number">0</span>; i < sz; i++) <span class="comment">// 遍历数组,打印每个元素</span></span><br><span class="line">{</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d "</span>, arr[i]);</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">{</span><br><span class="line"><span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"><span class="type">int</span> arr[<span class="number">10</span>] = { <span class="number">0</span> }; <span class="comment">// 初始化一个长度为10的数组,初始值全为0</span></span><br><span class="line"><span class="keyword">for</span> (i = <span class="number">0</span>; i < <span class="number">10</span>; i++) <span class="comment">// 填充数组,倒序从10到1</span></span><br><span class="line">{</span><br><span class="line">arr[i] = <span class="number">10</span> - i;</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d\n"</span>, arr[i]); <span class="comment">// 打印每个填充后的元素</span></span><br><span class="line">}</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h5 id="控制语句"><a href="#控制语句" class="headerlink" title="控制语句"></a>控制语句</h5><p>控制语句 用于控制程序的执行流程,以实现程序的各种结构方式(C语言支持三种结构:顺序结构、选择结构、循环结构),它们由特定的语句定义符组成,C语言有 九种控制语句 。</p><blockquote><p><strong>可分成以下三类:</strong></p></blockquote><ol><li><strong>条件判断语句也叫分支语句</strong> :if语句、switch语句;</li><li><strong>循环执行语句</strong> :do-while语句、while语句、for语句;</li><li><strong>转向语句</strong> :break语句、goto语句、continue语句、return语句。</li></ol><blockquote><p>后期会给大家一一介绍控制语句。</p></blockquote><h1 id="注释是什么?为什么写注释?"><a href="#注释是什么?为什么写注释?" class="headerlink" title="注释是什么?为什么写注释?"></a>注释是什么?为什么写注释?</h1><blockquote><p>注释是对代码的说明,编译器会忽略注释,也就是说,注释对实际代码没有影响。<br>注释是给程序员自己,或者其他程序员看的。<br>好的注释可以帮我们更好的理解代码,但是也不要过度注释,不要写没必要的注释。<br>当然不写注释可能会让后期阅读代码的人抓狂。<br>写注释一定程度上反应了程序作者的素质,建议大家写必要的注释,在未来找工作的时候,写代码时留下必要的注释也会给面试官留下更好的印象。</p></blockquote><h2 id="注释的-2-种形式"><a href="#注释的-2-种形式" class="headerlink" title="注释的 2 种形式"></a>注释的 2 种形式</h2><p>C语言的注释有两种表示方法。</p><h3 id="的形式"><a href="#的形式" class="headerlink" title="/**/的形式"></a>/**/的形式</h3><p>第一种方法是将注释放在 **/<em>…</em>/**之间,内部可以分行。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 注释 */</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">这是一行注释</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure><blockquote><p>这种注释可以插在行内。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span> <span class="type">int</span> <span class="title function_">fopen</span><span class="params">(<span class="type">char</span>* s <span class="comment">/* file name */</span>, <span class="type">int</span> mode)</span>;</span><br></pre></td></tr></table></figure><blockquote><p>上面示例中,/* file name <em>/用来对函数参数进行说明,跟在它后面的代码依然会有效执行。<br>这种注释一定不能忘记写结束符号</em>/,否则很容易导致错误。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">printf</span>(<span class="string">"a "</span>); <span class="comment">/* 注释一</span></span><br><span class="line"><span class="comment">printf("b ");</span></span><br><span class="line"><span class="comment">printf("c "); /* 注释二 */</span></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"d "</span>);</span><br></pre></td></tr></table></figure><p>上面示例的原意是,第一行和第三行代码的尾部,有两个注释。但是,第一行注释忘记写结束符号,导致注释一延续到第三行结束。</p><blockquote><p>/**/的这个注释也不支持嵌套注释,/<em>开始注释后,遇到第一个</em>/就认为注释结束了。</p></blockquote><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">printf("a ");</span></span><br><span class="line"><span class="comment">printf("b ");</span></span><br><span class="line"><span class="comment">printf("c "); /* 注释二 */</span></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"d "</span>);</span><br><span class="line">*/</span><br></pre></td></tr></table></figure><h4 id="的形式-1"><a href="#的形式-1" class="headerlink" title="//的形式"></a>//的形式</h4><p>第二种写法是将注释放在双斜杠//后面,从双斜杠到行尾都属于注释。这种注释只能是单行,可以放在行首,也可以放在一行语句的结尾。这是<strong>C99</strong>标准新增的语法。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 这是一行注释</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> x = <span class="number">1</span> ; <span class="comment">// 这也是注释</span></span><br></pre></td></tr></table></figure><p>不管是哪一种注释,都不能放在双引号里面。<br>双引号里面的注释符号,会成为字符串的一部分,解释为普通符号,失去注释作用。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span> <span class="built_in">printf</span>(<span class="string">"// hello /* world */ "</span>);</span><br></pre></td></tr></table></figure><p>上面示例中,双引号里面的注释符号,都会被视为普通字符,没有注释作用。</p><h5 id="注释会被替换"><a href="#注释会被替换" class="headerlink" title="注释会被替换"></a>注释会被替换</h5><p>编译时,注释会被替换成一个空格,所以<em><em>min/</em> 这里是注释</em>/Value<strong>会变成</strong>min Value<strong>,而不是</strong>minValue**。</p><blockquote><p>课堂板书:</p></blockquote><p><a href="https://gitee.com/bitpg/class114training-camp-phase-5/blob/master/2024-03-28-%E6%9D%BF%E4%B9%A6.png">https://gitee.com/bitpg/class114training-camp-phase-5/blob/master/2024-03-28-%E6%9D%BF%E4%B9%A6.png</a></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/26817/image-22.png" alt="alt text"></p><p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="/img/404.jpg"" data-lazy-src="/posts/26817/image-23.png" alt="alt text"></p><blockquote><p>课堂代码:</p></blockquote><p><a href="https://gitee.com/bitpg/class114training-camp-phase-5/blob/master/test_3_28/test_3_28/test.c">https://gitee.com/bitpg/class114training-camp-phase-5/blob/master/test_3_28/test_3_28/test.c</a></p><p><a href="https://gitee.com/bitpg/class114training-camp-phase-5/blob/master/test_3_28/test_3_28/add.c">https://gitee.com/bitpg/class114training-camp-phase-5/blob/master/test_3_28/test_3_28/add.c</a></p><blockquote><p><strong>完</strong></p></blockquote>]]></content>
<categories>
<category> C语言程序设计 </category>
</categories>
<tags>
<tag> C语言程序设计知识点总结 </tag>
</tags>
</entry>
</search>