C语言数组的概念

来源:互联网 发布:上海知柚网络科技公司 编辑:程序博客网 时间:2024/05/16 04:34

1.C语言数组的概念

在《printf函数的高级用法》一节中我们举了一个例子,是输出一个 4×4 的整数矩阵,代码如下:

<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdio.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdlib.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> <span class="sh_function" style="color: rgb(209, 28, 237);">main</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">()</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> a1<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">20</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> a2<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">345</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> a3<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">700</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> a4<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">22</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> b1<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">56720</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> b2<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">9999</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> b3<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">20098</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> b4<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">2</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> c1<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">233</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> c2<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">205</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> c3<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">1</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> c4<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">6666</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> d1<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">34</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> d2<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> d3<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">23</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> d4<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">23006783</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%-9d %-9d %-9d %-9d</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> a1<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> a2<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> a3<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> a4<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%-9d %-9d %-9d %-9d</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> b1<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> b2<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> b3<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> b4<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%-9d %-9d %-9d %-9d</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> c1<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> c2<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> c3<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> c4<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%-9d %-9d %-9d %-9d</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> d1<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> d2<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> d3<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> d4<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">system</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"pause"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li></ol>
运行结果:
20        345       700       2256720     9999      20098     2233       205       1         666634        0         23        23006783
矩阵共有 16 个整数,我们为每个整数定义了一个变量,也就是 16 个变量。那么,为了减少变量的数量,让开发更有效率,能不能为多个数据定义一个变量呢?比如,把每一行的整数放在一个变量里面,或者把 16 个整数全部都放在一个变量里面。

我们知道,要想把数据放入内存,必须先要分配内存空间。放入4个整数,就得分配4个int类型的内存空间:
int a[4];
这样,就在内存中分配了4个int类型的内存空间,共 4×4=16 个字节,并为它们起了一个名字,叫a

我们把这样的一组数据的集合称为数组(Array),它所包含的每一个数据叫做数组元素(Element),所包含的数据的个数称为数组长度(Length),例如int a[4];就定义了一个长度为4的整型数组,名字是a

数组中的每个元素都有一个序号,这个序号从0开始,而不是从我们熟悉的1开始,称为下标(Index)。使用数组元素时,指明下标即可,形式为:
arrayName[index]
arrayName 为数组名称,index 为下标。例如,a[0] 表示第0个元素,a[3] 表示第3个元素。

接下来我们就把第一行的4个整数放入数组:
a[0]=20;a[1]=345;a[2]=700;a[3]=22;
这里的0、1、2、3就是数组下标,a[0]、a[1]、a[2]、a[3] 就是数组元素。

我们来总结一下数组的定义方式:
dataType  arrayName[length];
dataType 为数据类型,arrayName 为数组名称,length 为数组长度。例如:
float m[12];char ch[9];

注意:
1) 数组中每个元素的数据类型必须相同,对于int a[4];,每个元素都必须为 int。

2) 数组下标必须是整数,取值范围为 0 ≤ index < length。

3) 数组是一个整体,它的内存是连续的,下面是int a[4];的内存示意图:

数组的初始化

上面的代码是先定义数组再给数组赋值,我们也可以在定义数组的同时赋值:
int a[4] = {20, 345, 700, 22};
{ }中的值即为各元素的初值,各值之间用,间隔。

对数组赋初值需要注意以下几点:
1) 可以只给部分元素赋初值。当{ }中值的个数少于元素个数时,只给前面部分元素赋值。例如:
int a[10]={12, 19, 22 , 993, 344};
表示只给 a[0]~a[4] 5个元素赋值,而后面5个元素自动赋0值。

当赋值的元素少于数组总体元素的时候,剩余的元素自动初始化为 0:对于short、int、long,就是整数0;对于char,就是字符 '\0';对于float、double,就是小数0.0。

我们可以通过下面的形式将数组的所有元素初始化为 0:
int a[10] = {0};char c[10] = {0};float f[10] = {0};
由于剩余的元素会自动初始化为0,所以只需要给第0个元素赋0值即可。

示例:输出数组元素。
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdio.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> <span class="sh_function" style="color: rgb(209, 28, 237);">main</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">()</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">6</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span><span class="sh_number" style="color: rgb(50, 186, 6);">299</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">34</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">92</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">100</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> b<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">6</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">],</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_comment" style="color: rgb(56, 173, 36);">//从控制台输入数据为每个元素赋值</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">for</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);"><</span><span class="sh_number" style="color: rgb(50, 186, 6);">6</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);">++)</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(209, 28, 237);">scanf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%d"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">&</span>b<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">]);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_comment" style="color: rgb(56, 173, 36);">//输出数组元素</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">for</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);"><</span><span class="sh_number" style="color: rgb(50, 186, 6);">6</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);">++)</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%d  "</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">]);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">putchar</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">'</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">'</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">for</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);"><</span><span class="sh_number" style="color: rgb(50, 186, 6);">6</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);">++)</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%d  "</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> b<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">]);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">putchar</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">'</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">'</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">   </li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li></ol>
运行结果:
90 100 33 22 568 10299  34  92  100  0  090  100  33  22  568  10

2) 只能给元素逐个赋值,不能给数组整体赋值。例如给十个元素全部赋1值,只能写为:
int a[10]={1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
而不能写为:
int a[10]=1;

3) 如给全部元素赋值,那么在数组定义时可以不给出数组的长度。例如:
int a[]={1,2,3,4,5};
等价于
int a[5]={1,2,3,4,5};

最后,我们借助数组来输出一个 4×4 的矩阵:
复制纯文本新窗口
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdio.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdlib.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> <span class="sh_function" style="color: rgb(209, 28, 237);">main</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">()</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">4</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span><span class="sh_number" style="color: rgb(50, 186, 6);">20</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">345</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">700</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">22</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> b<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">4</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span><span class="sh_number" style="color: rgb(50, 186, 6);">56720</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">9999</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">20098</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">2</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> c<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">4</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span><span class="sh_number" style="color: rgb(50, 186, 6);">233</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">205</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">1</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">6666</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> d<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">4</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span><span class="sh_number" style="color: rgb(50, 186, 6);">34</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">23</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">23006783</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%-9d %-9d %-9d %-9d</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">],</span> a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">1</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">],</span> a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">2</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">],</span> a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">3</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%-9d %-9d %-9d %-9d</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> b<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">],</span> b<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">1</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">],</span> b<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">2</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">],</span> b<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">3</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%-9d %-9d %-9d %-9d</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> c<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">],</span> c<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">1</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">],</span> c<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">2</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">],</span> c<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">3</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%-9d %-9d %-9d %-9d</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> d<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">],</span> d<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">1</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">],</span> d<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">2</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">],</span> d<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">3</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">system</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"pause"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li></ol>
2.C语言二维数组

上节讲解的数组可以看作是一行连续的数据,只有一个下标,称为一维数组。在实际问题中有很多量是二维的或多维的,因此C语言允许构造多维数组。多维数组元素有多个下标,以确定它在数组中的位置。本节只介绍二维数组,多维数组可由二维数组类推而得到。

二维数组的定义

二维数组定义的一般形式是:
dataType arrayName[length1][length2];
其中,dataType 为数据类型,arrayName 为数组名,length1 为第一维下标的长度,length2 为第二维下标的长度。例如:
int a[3][4];
定义了一个3行4列的数组,共有3×4=12个元素,数组名为a,即:
a[0][0], a[0][1], a[0][2], a[0][3]
a[1][0], a[1][1], a[1][2], a[1][3]
a[2][0], a[2][1], a[2][2], a[2][3]

在二维数组中,要定位一个元素,必须给出一维下标和二维下标,就像在一个平面中确定一个点,要知道x坐标和y坐标。例如,a[3][4] 表示a数组第3行第4列的元素。

二维数组在概念上是二维的,但在内存中地址是连续的,也就是说存储器单元是按一维线性排列的。那么,如何在一维存储器中存放二维数组呢?有两种方式:一种是按行排列, 即放完一行之后顺次放入第二行。另一种是按列排列, 即放完一列之后再顺次放入第二列。

在C语言中,二维数组是按行排列的。也就是先存放a[0]行,再存放a[1]行,最后存放a[2]行;每行中的四个元素也是依次存放。数组a为int类型,每个元素占用4个字节,整个数组共占用4×(3×4)=48个字节。

【示例】一个学习小组有5个人,每个人有三门课的考试成绩。求全组分科的平均成绩和各科总平均成绩。
--张王李赵周Math8061598576C7565638777English9271709085
可设一个二维数组a[5][3]存放五个人三门课的成绩。再设一个一维数组v[3]存放所求得各分科平均成绩,设变量average 为全组各科总平均成绩。编程如下:
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdio.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> <span class="sh_function" style="color: rgb(209, 28, 237);">main</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">()</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> j<span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span>  <span class="sh_comment" style="color: rgb(56, 173, 36);">//二维数组下标</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> sum<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span>  <span class="sh_comment" style="color: rgb(56, 173, 36);">//当前科目的总成绩</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> average<span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span>  <span class="sh_comment" style="color: rgb(56, 173, 36);">//总平均分</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> v<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">3</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">];</span>  <span class="sh_comment" style="color: rgb(56, 173, 36);">//各科平均分</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">5</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">][</span><span class="sh_number" style="color: rgb(50, 186, 6);">3</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">];</span>  <span class="sh_comment" style="color: rgb(56, 173, 36);">//用来保存每个同学各科成绩的二维数组</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"Input score:</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">for</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);"><</span><span class="sh_number" style="color: rgb(50, 186, 6);">3</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);">++)</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">for</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>j<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span> j<span class="sh_symbol" style="color: rgb(48, 48, 238);"><</span><span class="sh_number" style="color: rgb(50, 186, 6);">5</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span> j<span class="sh_symbol" style="color: rgb(48, 48, 238);">++)</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">            <span class="sh_function" style="color: rgb(209, 28, 237);">scanf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%d"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">&</span>a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span>j<span class="sh_symbol" style="color: rgb(48, 48, 238);">][</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">]);</span>  <span class="sh_comment" style="color: rgb(56, 173, 36);">//输入每个同学的各科成绩</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">            sum<span class="sh_symbol" style="color: rgb(48, 48, 238);">+=</span>a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span>j<span class="sh_symbol" style="color: rgb(48, 48, 238);">][</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">];</span>  <span class="sh_comment" style="color: rgb(56, 173, 36);">//计算当前科目的总成绩</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        v<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">]=</span>sum<span class="sh_symbol" style="color: rgb(48, 48, 238);">/</span><span class="sh_number" style="color: rgb(50, 186, 6);">5</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span>  <span class="sh_comment" style="color: rgb(56, 173, 36);">// 当前科目的平均分</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        sum<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    average <span class="sh_symbol" style="color: rgb(48, 48, 238);">=(</span>v<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]+</span>v<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">1</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]+</span>v<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">2</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">])/</span><span class="sh_number" style="color: rgb(50, 186, 6);">3</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"Math: %d</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">C Languag: %d</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">English: %d</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> v<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">],</span> v<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">1</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">],</span> v<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">2</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"Total:%d</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> average<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li></ol>
运行结果:
Input score:
80 61 59 85 76 75 65 63 87 77 92 71 70 90 85↙
Math: 72
C Languag: 73
English: 81
Total:75

程序中首先用了一个双重循环。在内循环中依次读入某一门课程的各个学生的成绩,并把这些成绩累加起来,退出内循环后再把该累加成绩除以5送入v[i]之中,这就是该门课程的平均成绩。外循环共循环三次,分别求出三门课各自的平均成绩并存放在v数组之中。退出外循环之后,把v[0]、v[1]、v[2]相加除以3即得到各科总平均成绩。最后按题意输出各个成绩。

二维数组的初始化

二维数组的初始化可以按行分段赋值,也可按行连续赋值。

例如对数组a[5][3],按行分段赋值可写为:
int a[5][3]={ {80,75,92}, {61,65,71}, {59,63,70}, {85,87,90}, {76,77,85} };
按行连续赋值可写为:
int a[5][3]={80, 75, 92, 61, 65, 71, 59, 63, 70, 85, 87, 90, 76, 77, 85};
这两种赋初值的结果是完全相同的。

【示例】求各科平均分和总成绩平均分。
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdio.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> <span class="sh_function" style="color: rgb(209, 28, 237);">main</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">()</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> j<span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span>  <span class="sh_comment" style="color: rgb(56, 173, 36);">//二维数组下标</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> sum<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span>  <span class="sh_comment" style="color: rgb(56, 173, 36);">//当前科目的总成绩</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> average<span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span>  <span class="sh_comment" style="color: rgb(56, 173, 36);">//总平均分</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> v<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">3</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">];</span>  <span class="sh_comment" style="color: rgb(56, 173, 36);">//各科平均分</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">5</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">][</span><span class="sh_number" style="color: rgb(50, 186, 6);">3</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]=</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span> <span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span><span class="sh_number" style="color: rgb(50, 186, 6);">80</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span><span class="sh_number" style="color: rgb(50, 186, 6);">75</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span><span class="sh_number" style="color: rgb(50, 186, 6);">92</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span><span class="sh_number" style="color: rgb(50, 186, 6);">61</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span><span class="sh_number" style="color: rgb(50, 186, 6);">65</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span><span class="sh_number" style="color: rgb(50, 186, 6);">71</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span><span class="sh_number" style="color: rgb(50, 186, 6);">59</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span><span class="sh_number" style="color: rgb(50, 186, 6);">63</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span><span class="sh_number" style="color: rgb(50, 186, 6);">70</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span><span class="sh_number" style="color: rgb(50, 186, 6);">85</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span><span class="sh_number" style="color: rgb(50, 186, 6);">87</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span><span class="sh_number" style="color: rgb(50, 186, 6);">90</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span><span class="sh_number" style="color: rgb(50, 186, 6);">76</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span><span class="sh_number" style="color: rgb(50, 186, 6);">77</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span><span class="sh_number" style="color: rgb(50, 186, 6);">85</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span> <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">for</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);"><</span><span class="sh_number" style="color: rgb(50, 186, 6);">3</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);">++)</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">for</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>j<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span> j<span class="sh_symbol" style="color: rgb(48, 48, 238);"><</span><span class="sh_number" style="color: rgb(50, 186, 6);">5</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span> j<span class="sh_symbol" style="color: rgb(48, 48, 238);">++)</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">            sum<span class="sh_symbol" style="color: rgb(48, 48, 238);">+=</span>a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span>j<span class="sh_symbol" style="color: rgb(48, 48, 238);">][</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">];</span>  <span class="sh_comment" style="color: rgb(56, 173, 36);">//计算当前科目的总成绩</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        v<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">]=</span>sum<span class="sh_symbol" style="color: rgb(48, 48, 238);">/</span><span class="sh_number" style="color: rgb(50, 186, 6);">5</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span>  <span class="sh_comment" style="color: rgb(56, 173, 36);">// 当前科目的平均分</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        sum<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    average <span class="sh_symbol" style="color: rgb(48, 48, 238);">=(</span>v<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]+</span>v<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">1</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]+</span>v<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">2</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">])/</span><span class="sh_number" style="color: rgb(50, 186, 6);">3</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"Math: %d</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">C Languag: %d</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">English: %d</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> v<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">],</span> v<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">1</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">],</span> v<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">2</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"Total:%d</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> average<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li></ol>
运行结果:
Math: 72
C Languag: 73
English: 81
Total:75

对于二维数组初始化赋值还有以下说明

1) 可以只对部分元素赋初值,未赋初值的元素自动取0值。例如:
int a[3][3]={{1},{2},{3}};
是对每一行的第一列元素赋值,未赋值的元素取0值。 赋值后各元素的值为:
1  0  0
2  0  0
3  0  0

int a [3][3]={{0,1},{0,0,2},{3}};
赋值后的元素值为:
0  1  0
0  0  2
3  0  0

2) 如对全部元素赋初值,则第一维的长度可以不给出。例如:
int a[3][3]={1,2,3,4,5,6,7,8,9};
可以写为:
int a[][3]={1,2,3,4,5,6,7,8,9};

3) 数组是一种构造类型的数据。二维数组可以看作是由一维数组的嵌套而构成的。设一维数组的每个元素都又是一个数组,就组成了二维数组。当然,前提是各元素类型必须相同。根据这样的分析,一个二维数组也可以分解为多个一维数组。C语言允许这种分解。

如二维数组a[3][4],可分解为三个一维数组,其数组名分别为:a[0]、a[1]、a[2]。

对这三个一维数组不需另作说明即可使用。这三个一维数组都有4个元素,例如:一维数组a[0]的元素为a[0][0], a[0][1], a[0][2], a[0][3]。必须强调的是,a[0], a[1], a[2]不能当作下标变量使用,它们是数组名,不是一个单纯的下标变量。


3.C语言数组元素的查询

在实际开发中,经常需要查询数组中的元素。例如,学校为每位同学分配了一个唯一的编号,现在有一个数组,保存了实验班所有同学的编号信息,如果有家长想知道他的孩子是否进入了实验班,只要提供孩子的编号就可以,如果编号和数组中的某个元素相等,就进入了实验班,否则就没进入。

不幸的是,C语言标准库没有提供与数组查询相关的函数,所以我们只能自己编写代码。

对无序数组的查询

所谓无序数组,就是数组元素的排列没有规律。无序数组元素查询的思路也很简单,就是用循环遍历数组中的每个元素,把要查询的值挨个比较一遍。请看下面的代码:
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdio.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdlib.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> <span class="sh_function" style="color: rgb(209, 28, 237);">main</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">()</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> nums<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">10</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span><span class="sh_number" style="color: rgb(50, 186, 6);">1</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">10</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">6</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">296</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">177</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">23</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">100</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">34</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">999</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> num<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> subscript <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">-</span><span class="sh_number" style="color: rgb(50, 186, 6);">1</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"Please input an integer: "</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">scanf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%d"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">&</span>num<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">for</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);"><</span><span class="sh_number" style="color: rgb(50, 186, 6);">10</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);">++)</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>nums<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">==</span> num<span class="sh_symbol" style="color: rgb(48, 48, 238);">)</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">            subscript <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">            <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">break</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>subscript<span class="sh_symbol" style="color: rgb(48, 48, 238);"><</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">)</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%d isn't  in the array.</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> num<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span><span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">else</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%d is  in the array, and it's subscript is %d.</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> num<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> subscript<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">system</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"pause"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li></ol>
运行结果:

Please input an integer: 100
100 is  in the array, and it's subscript is 7.

或者

Please input an integer: 28
28 isn't  in the array.

这段代码的作用是让用户输入一个数字,判断该数字是否在数组中,如果在,就打印出下标。

第10~15行代码是关键,它会遍历数组中的每个元素,和用户输入的数字进行比较,如果相等就获取它的下标并跳出循环。

注意:数组下标的取值范围是非负数,当 subscript >= 0 时,该数字在数组中,当 subscript < 0 时,该数字不在数组中,所以在定义 subscript 变量时,必须将其初始化为一个负数。

对有序数组的查询

查询无序数组需要遍历数组中的所有元素,而查询有序数组只需要遍历其中一部分元素。例如有一个长度为10的整型数组,它所包含的元素按照从小到大的顺序(升序)排列,假设比较到第4个元素时发现它的值大于输入的数字,那么剩下的5个元素就没必要再比较了,肯定也大于输入的数字,这样就减少了循环的次数,提高了执行效率。

请看下面的代码:
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdio.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdlib.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> <span class="sh_function" style="color: rgb(209, 28, 237);">main</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">()</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> nums<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">10</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">1</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">6</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">10</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">23</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">34</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">100</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">177</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">296</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">999</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> num<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> subscript <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">-</span><span class="sh_number" style="color: rgb(50, 186, 6);">1</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"Please input an integer: "</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">scanf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%d"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">&</span>num<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">for</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);"><</span><span class="sh_number" style="color: rgb(50, 186, 6);">10</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);">++)</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>nums<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">>=</span> num<span class="sh_symbol" style="color: rgb(48, 48, 238);">)</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">            <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>nums<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">==</span> num<span class="sh_symbol" style="color: rgb(48, 48, 238);">)</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">                subscript <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">            <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">            <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">break</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>subscript<span class="sh_symbol" style="color: rgb(48, 48, 238);"><</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">)</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%d isn't  in the array.</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> num<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span><span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">else</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%d is  in the array, and it's subscript is %d.</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> num<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> subscript<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">system</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"pause"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li></ol>
注意第11行代码,只有当 nums[i] >= num 成立时才进行处理,否则继续循环。nums[i] >= num 有两重含义:
  • 如果 nums[i] == num,则 num 在数组中,那么就需要给 subscript 赋值,记录当前元素的下标;
  • 如果 nums[i] > num,则 nums 不在数组中。

无论哪种情况,都没有必要再继续循环下去了,所以一旦满足 nums[i] >= num,就应该使用 break 跳出循环。


4.C语言字符数组和字符串

用来存放字符的数组称为字符数组,例如:

<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">char</span> a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">10</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">];</span>  <span class="sh_comment" style="color: rgb(56, 173, 36);">//一维字符数组</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">char</span> b<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">5</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">][</span><span class="sh_number" style="color: rgb(50, 186, 6);">10</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">];</span>  <span class="sh_comment" style="color: rgb(56, 173, 36);">//二维字符数组</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">char</span> c<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">20</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]=</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span><span class="sh_string" style="color: rgb(24, 97, 167);">'c'</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_string" style="color: rgb(24, 97, 167);">'  '</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_string" style="color: rgb(24, 97, 167);">'p'</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_string" style="color: rgb(24, 97, 167);">'r'</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_string" style="color: rgb(24, 97, 167);">'o'</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_string" style="color: rgb(24, 97, 167);">'g'</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_string" style="color: rgb(24, 97, 167);">'r'</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_string" style="color: rgb(24, 97, 167);">'a'</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span><span class="sh_string" style="color: rgb(24, 97, 167);">'m'</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span>  <span class="sh_comment" style="color: rgb(56, 173, 36);">// 给部分数组元素赋值</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">char</span> d<span class="sh_symbol" style="color: rgb(48, 48, 238);">[]=</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span><span class="sh_string" style="color: rgb(24, 97, 167);">'c'</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_string" style="color: rgb(24, 97, 167);">' '</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_string" style="color: rgb(24, 97, 167);">'p'</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_string" style="color: rgb(24, 97, 167);">'r'</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_string" style="color: rgb(24, 97, 167);">'o'</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_string" style="color: rgb(24, 97, 167);">'g'</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_string" style="color: rgb(24, 97, 167);">'r'</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_string" style="color: rgb(24, 97, 167);">'a'</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_string" style="color: rgb(24, 97, 167);">'m'</span> <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span>  <span class="sh_comment" style="color: rgb(56, 173, 36);">//对全体元素赋值时可以省去长度</span></li></ol>
字符数组实际上是一系列字符的集合,也就是字符串(String)。在C语言中,没有专门的字符串变量,没有string类型,通常就用一个字符数组来存放一个字符串。

C语言规定,可以将字符串直接赋值给字符数组,例如:
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">char</span> str<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">30</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span><span class="sh_string" style="color: rgb(24, 97, 167);">"c.biancheng.net"</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">char</span> str<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">30</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_string" style="color: rgb(24, 97, 167);">"c.biancheng.net"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span>  <span class="sh_comment" style="color: rgb(56, 173, 36);">//这种形式更加简洁,实际开发中常用</span></li></ol>
数组第0个元素为 'c',第1个元素为 '.',第2个元素为 'b',后面的元素以此类推。也可以不指定数组长度,例如:
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">char</span> str<span class="sh_symbol" style="color: rgb(48, 48, 238);">[]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span><span class="sh_string" style="color: rgb(24, 97, 167);">"c.biancheng.net"</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">char</span> str<span class="sh_symbol" style="color: rgb(48, 48, 238);">[]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_string" style="color: rgb(24, 97, 167);">"c.biancheng.net"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span>  <span class="sh_comment" style="color: rgb(56, 173, 36);">//这种形式更加简洁,实际开发中常用</span></li></ol>

在C语言中,字符串总是以'\0'作为串的结束符。上面的两个字符串,编译器已经在末尾自动添加了'\0'
'\0'是ASCII码表中的第0个字符,用NUL表示,称为空字符。该字符既不能显示,也不是控制字符,输出该字符不会有任何效果,它在C语言中仅作为字符串的结束标志。
puts 和 printf 在输出字符串时会逐个扫描字符,直到遇见 '\0' 才结束输出。请看下面的例子:
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdio.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> <span class="sh_function" style="color: rgb(209, 28, 237);">main</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">()</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">char</span> str1<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">30</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_string" style="color: rgb(24, 97, 167);">"http://c.biancheng.net"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">char</span> str2<span class="sh_symbol" style="color: rgb(48, 48, 238);">[]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_string" style="color: rgb(24, 97, 167);">"C Language"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">char</span> str3<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">30</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_string" style="color: rgb(24, 97, 167);">"You are a good</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\0</span><span class="sh_string" style="color: rgb(24, 97, 167);"> boy!"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"str1: %s</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> str1<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"str2: %s</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> str2<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"str3: %s</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> str3<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li></ol>
运行结果:
str1: http://c.biancheng.net
str2: C Language
str3: You are a good

str1 和 str2 很好理解,编译器会在字符串最后自动添加 '\0',并且数组足够大,所以会输出整个字符串。对于 str3,由于字符串中间存在 '\0',printf() 扫描到这里就认为字符串结束了,所以不会输出后面的内容。

需要注意的是,用字符串给字符数组赋值时由于要添加结束符 '\0',数组的长度要比字符串的长度(字符串长度不包括 '\0')大1。例如:
char str[] = "C program";
该数组在内存中的实际存放情况为:

字符串长度为 9,数组长度为 10。


5.C语言字符串处理函数

C语言提供了丰富的字符串处理函数,例如字符串的输入、输出、合并、修改、比较、转换、复制、搜索等,使用这些现成的函数可大大减轻编程的负担。

用于输入输出的字符串函数,例如printfputsscanfgets等,使用时应包含头文件stdio.h,使用其它字符串函数则应包含头文件string.h

字符串长度函数strlen

strlen 是 string length 的缩写,用来获得字符串的长度。所谓长度,就是包含多少个字符(不包括字符串结束标志 '\0')。语法格式为:
strlen(arrayName);
strlen 将返回字符串的长度,它是一个整数。请看下面的例子:
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdio.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><string.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> <span class="sh_function" style="color: rgb(209, 28, 237);">main</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">()</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">char</span> str<span class="sh_symbol" style="color: rgb(48, 48, 238);">[]=</span><span class="sh_string" style="color: rgb(24, 97, 167);">"C language"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> len <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_function" style="color: rgb(209, 28, 237);">strlen</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>str<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"The lenth of the string is %d</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> len<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li></ol>
运行结果:
The lenth of the string is 10

需要说明的是,strlen 会从字符串的第 0 个字符开始计算,直到遇到字符串结束标志 '\0'。将上面代码中的 str 改为:
char str[]="C \0language";
那么输出结果就是:
The lenth of the string is 2

字符串连接函数 strcat

strcat 是 string catenate 的缩写,意思是把两个字符串拼接在一起,语法格式为:
strcat(arrayName1, arrayName2);
arrayName1、arrayName2 为需要拼接的字符串。

strcat 将把 arrayName2 连接到 arrayName1 后面,并删去 arrayName1 最后的结束标志 '\0'。这就意味着,arrayName1 的长度要足够,必须能够同时容纳 arrayName1 和 arrayName2,否则会越界。

strcat 返回值为 arrayName1 的首地址。请看下面的例子:
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdio.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><string.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> <span class="sh_function" style="color: rgb(209, 28, 237);">main</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">()</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">char</span> str1<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">40</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]=</span><span class="sh_string" style="color: rgb(24, 97, 167);">"My name is "</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">char</span> str2<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">20</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">];</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"Input your name:"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">gets</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>str2<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">strcat</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>str1<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span>str2<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">puts</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>str1<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li></ol>
运行结果:
Input your name:xiao p
My name is xiao p

字符串复制函数strcpy

strcpy 是 string copy 的缩写,意思是字符串复制,语法格式为:
strcpy(arrayName1, arrayName2);
strcpy 会把 arrayName2 中的字符串拷贝到 arrayName1 中,串结束标志 '\0' 也一同拷贝。请看下面的例子:
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdio.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><string.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> <span class="sh_function" style="color: rgb(209, 28, 237);">main</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">()</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">char</span> str1<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">15</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">],</span> str2<span class="sh_symbol" style="color: rgb(48, 48, 238);">[]=</span><span class="sh_string" style="color: rgb(24, 97, 167);">"C Language"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">strcpy</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>str1<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> str2<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">puts</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>str1<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li></ol>
运行结果:
C Language

strcat 要求 arrayName1 要有足够的长度,否则不能全部装入所拷贝的字符串。

字符串比较函数strcmp

strcmp 是 string compare 的缩写,意思是字符串比较,语法格式为:
strcmp(arrayName1, arrayName2);
arrayName1 和 arrayName2 是需要比较的两个字符串。

字符本身没有大小之分,strcmp() 是以各个字符在ASCII 码表上对应的数值进行比较的。strcmp() 首先将 arrayName1 中第0个字符的 ASCII 码值减去 arrayName2 中第0个字符的 ASCII 码值,若差值为 0,则说明两个字符相同,再继续比较下个字符,若差值不为 0 则将差值返回。例如字符串"Ac"和"ba"比较则会返回字符"A"(65)和'b'(98)的差值(-33)。

返回值:若 arrayName1 和 arrayName2 相同,则返回0;若 arrayName1 大于 arrayName2,则返回大于 0 的值;若 arrayName1 小于 arrayName2,则返回小于0 的值。

【示例】对4组字符串进行比较。
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><string.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_function" style="color: rgb(209, 28, 237);">main</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">()</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">char</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">*</span>a <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_string" style="color: rgb(24, 97, 167);">"aBcDeF"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">char</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">*</span>b <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_string" style="color: rgb(24, 97, 167);">"AbCdEf"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">char</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">*</span>c <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_string" style="color: rgb(24, 97, 167);">"aacdef"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">char</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">*</span>d <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_string" style="color: rgb(24, 97, 167);">"aBcDeF"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"strcmp(a, b) : %d</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_function" style="color: rgb(209, 28, 237);">strcmp</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>a<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> b<span class="sh_symbol" style="color: rgb(48, 48, 238);">));</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"strcmp(a, c) : %d</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_function" style="color: rgb(209, 28, 237);">strcmp</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>a<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> c<span class="sh_symbol" style="color: rgb(48, 48, 238);">));</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"strcmp(a, d) : %d</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_function" style="color: rgb(209, 28, 237);">strcmp</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>a<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> d<span class="sh_symbol" style="color: rgb(48, 48, 238);">));</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li></ol>
运行结果:
strcmp(a, b) : 32
strcmp(a, c) :-31
strcmp(a, d) : 0


6.C语言字符串的输入输出

字符串的输出

在C语言中,输出字符串的函数有两个:
  • puts():直接输出字符串,并且只能输出字符串。
  • printf():通过格式控制符 %s 输出字符串。除了字符串,printf() 还能输出其他类型的数据。

这两个函数前面已经讲过了,这里不妨再演示一下,请看下面的代码:
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdio.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> <span class="sh_function" style="color: rgb(209, 28, 237);">main</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">()</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">char</span> str<span class="sh_symbol" style="color: rgb(48, 48, 238);">[]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_string" style="color: rgb(24, 97, 167);">"http://c.biancheng.net"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%s</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> str<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span>  <span class="sh_comment" style="color: rgb(56, 173, 36);">//通过变量输出</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%s</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_string" style="color: rgb(24, 97, 167);">"http://c.biancheng.net"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span>  <span class="sh_comment" style="color: rgb(56, 173, 36);">//直接输出</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">puts</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>str<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span>  <span class="sh_comment" style="color: rgb(56, 173, 36);">//通过变量输出</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">puts</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"http://c.biancheng.net"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span>  <span class="sh_comment" style="color: rgb(56, 173, 36);">//直接输出</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li></ol>
运行结果:
http://c.biancheng.net
http://c.biancheng.net
http://c.biancheng.net
http://c.biancheng.net

在 printf() 函数中使用%s输出字符串时,在变量列表中给出数组名即可,不能写为printf("%s", str[]);

字符串的输入

在C语言中,输入字符串的函数有两个:
  • scanf():通过格式控制符 %s 输入字符串。除了字符串,scanf() 还能输入其他类型的数据。
  • gets():直接输入字符串,并且只能输入字符串。

1) 使用 scanf() 读取字符串

请先看下面的例子:
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdio.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> <span class="sh_function" style="color: rgb(209, 28, 237);">main</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">()</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">char</span> str1<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">30</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">],</span> str2<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">30</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">];</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"Input str1: "</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">scanf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%s"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> str1<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"Input str2: "</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">scanf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%s"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> str2<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"str1: %s</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">str2: %s</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> str1<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> str2<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li></ol>
运行结果:
Input str1: c.biancheng.net↙
Input str2: Java Python C-Sharp↙
str1: c.biancheng.net
str2: Java

由于字符数组长度为30,因此输入的字符串长度必须小于30,以留出一个字节用于存放字符串结束标志`\0`。

对程序的说明:
① 我们本来希望将 "Java Python C-Sharp" 赋值给 str2,但是 scanf() 只读取到 "Java",这是因为 scanf() 读取到空格时就认为字符串输入结束了,不会继续读取了。请看下面的例子:
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdio.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> <span class="sh_function" style="color: rgb(209, 28, 237);">main</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">()</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">char</span> str1<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">20</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">],</span> str2<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">20</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">],</span> str3<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">20</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">];</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"Input string: "</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">scanf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%s"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> str1<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">scanf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%s"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> str2<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">scanf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%s"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> str3<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"str1: %s</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">str2: %s</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">str3: %s</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> str1<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> str2<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> str3<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li></ol>
运行结果:
Input string: Java Python C-Sharp↙
str1: Java
str2: Python
str3: C-Sharp

第一个 scanf() 读取到 "Java" 后遇到空格,结束读取,将"Python C-Sharp" 留在缓冲区。第二个 scanf() 直接从缓冲区中读取,不会等待用户输入,读取到 "Python" 后遇到空格,结束读取,将 "C-Sharp" 留在缓冲区。第三个 scanf() 读取缓冲区中剩下的内容。

② 在《从键盘输入数据》中讲到,scanf 的各个变量前面要加取地址符&,用以获得变量的地址,例如:
int a, b;scanf("%d %d", &a, &b);
但是在本节的示例中,将字符串读入字符数组却没有使用&,例如:
char str1[20], str2[20], str3[20], str4[20];scanf("%s %s %s %s",str1, str2, str3, str4);
这是因为C语言规定,数组名就代表了该数组的地址。整个数组是一块连续的内存单元,如有字符数组char c[10],在内存可表示为:

C语言还规定,数组名所代表的地址为第0个元素的地址,例如char c[10];c就代表c[0]的地址。第0个元素的地址就是数组的起始地址,称为首地址也就是说,数组名表示数组的首地址。

设数组c的首地址为0X2000,也即c[0]地址为0X2000,则数组名c就代表这个地址。因为c已经表示地址,所以在c前面不能再加取地址符&,例如写作scanf("%s",&c);是错误的。

有了首地址,有了字符串结束符'\0',就可以在内存中完整定位一个字符串了。例如:
printf("%s", c);
printf 函数会根据数组名找到c的首地址,然后逐个输出数组中各个字符直到遇到 '\0' 为止。
int、float、char 类型的变量表示数据本身,数据就保存在变量中;而数组名表示的是数组的首地址,数组保存在其他内存单元,数组名保存的是这块内存的首地址。后面我们会讲解指针,大家将会有更加深刻的理解。

2) 使用 gets() 读取字符串

gets 是 get string 的缩写,意思是获取用户从键盘输入的字符串,语法格式为:
gets(arrayName);
arrayName 为字符数组。从键盘获得的字符串,将保存在 arrayName 中。请看下面的例子:
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdio.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> <span class="sh_function" style="color: rgb(209, 28, 237);">main</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">()</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">char</span> str1<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">30</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">],</span> str2<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">30</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">];</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"Input str1: "</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">gets</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>str1<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"Input str2: "</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">gets</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>str2<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"str1: %s</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">str2: %s</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> str1<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> str2<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li></ol>
运行结果:
Input str1: Java Python C-Sharp↙
Input str2: http://c.biancheng.net↙
str1: Java Python C-Sharp
str2: http://c.biancheng.net

可以发现,当输入的字符串中含有空格时,输出仍为全部字符串,这说明 gets() 函数不会把空格作为输入结束的标志,而只把回车换行作为输入结束的标志,这与 scanf() 函数是不同的。

总结:如果希望读取的字符串中不包含空格,那么使用 scanf() 函数;如果希望获取整行字符串,那么使用 gets() 函数,它能避免空格的截断。


7.C语言数组的静态行、越界以及溢出

在C语言中,数组一旦被定义后,占用的内存空间就是固定的,不能在任何位置插入元素,也不能在任何位置删除元素(当然可以修改元素),我们将这样的数组称为静态数组(Static Array)

数组越界

C语言数组不会自动扩容,当下标小于零或大于等于数组长度时,就发生了越界(Out Of Bounds),访问到数组以外的内存。如果下标小于零,就会发生下限越界(Off Normal Lower);如果下标大于等于数组长度,就会发生上限越界(Off Normal Upper)

C语言为了提高效率,并不会对越界行为进行检查,即使越界了,也能够正常编译,只有在运行期间才可能会发生问题。请看下面的代码:
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdio.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdlib.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> <span class="sh_function" style="color: rgb(209, 28, 237);">main</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">()</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">3</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span><span class="sh_number" style="color: rgb(50, 186, 6);">10</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">20</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">30</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">for</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">=-</span><span class="sh_number" style="color: rgb(50, 186, 6);">2</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);"><=</span><span class="sh_number" style="color: rgb(50, 186, 6);">4</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);">++)</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"a[%d]=%d</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">]);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">system</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"pause"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li></ol>
运行结果:
a[-2]=-858993460
a[-1]=-858993460
a[0]=10
a[1]=20
a[2]=30
a[3]=-858993460
a[4]=-858993460

越界访问的数组元素都是垃圾值,没有实际的含义,因为数组之外的内存我们并不知道是什么,可能是其他变量的值,可能是附加数据,可能是一个地址,这些都是不可控的。

由于C语言的”放任“,我们访问数组时必须非常小心,要确保不会发生越界。

当发生数组越界时,如果我们对该内存有访问权限,程序将正常运行,但会出现不可控的结果(如上例所示);如果没有访问权限,程序将会崩溃。请看下面的例子:
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdio.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdlib.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> <span class="sh_function" style="color: rgb(209, 28, 237);">main</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">()</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">3</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">];</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%d"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">10000</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">system</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"pause"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li></ol>
在 C-Free 5.0 下运行,会弹出程序停止工作的对话框:


在VS2010下运行,会出现运行时错误:


每个程序可访问的内存都是有限的,该程序要访问 4*10000 字节处的内存,显然太远了,超出了程序的访问范围。这个地方的内存可能是其他程序的内存,可能是系统本身占用的内存,可能是没被使用的内存,如果放任这种行为,将带来非常危险的后果,操作系统只能让程序停止运行。

数组溢出

当赋予数组的元素个数超过数组长度时,就会发生溢出(Overflow)。如下所示:
int a[3] = {1, 2, 3, 4, 5};
数组长度为3,初始化时却赋予5个元素,超出了数组容量,所以只能保存前3个元素,后面的元素被丢弃。

VC/VS 发现数组溢出会报错,禁止编译通过;而GCC不会,它只给出警告。

一般情况下数组溢出不会有什么问题,顶多是丢弃多余的元素。但对于字符数组,有时会产生不可控的情况,请看下面的代码:
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdio.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdlib.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> <span class="sh_function" style="color: rgb(209, 28, 237);">main</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">()</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">char</span> str<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">10</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_string" style="color: rgb(24, 97, 167);">"http://c.biancheng.net"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">puts</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>str<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">system</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"pause"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li></ol>
可能的运行结果:


字符串的长度大于数组长度,数组只能容纳字符串的前面一部分,也就是 "http://c.b",即使编译器在最后添加了 '\0',它也保存不到数组里面,所以 printf() 扫描数组时不会遇到结束符 '\0',只能继续向后扫描。而后面内存中的数据我们不知道是什么,字符能否识别,何时遇到 '\0',这些都是不确定的。当字符无法识别时,就会出现乱码,显示奇怪的字符。

由此可见,在用字符串给字符数组赋值时,要保证数组长度大于字符串长度,以容纳结束符 '\0'。


8.C语言变长数组

目前使用的C语言有两个版本,C89和C99。C89(也称ANSI C)是较早的版本,也是最经典的版本,国内大学几乎都是以该版本为基础进行授课。C99是后来对C89的升级,增添了一些内容,语法更加灵活,同时兼容C89。关于C语言标准的更多内容请访问:C语言的发展及其版本

各种编译器都遵循C89标准,但对C99的支持却不同:开源组织的GCC已经支持了大部分的C99标准,而微软的VC、VS对C99却不感兴趣(后来的VS2013、VS2015才慢慢支持)。

为什么要讨论这个问题呢?因为C89和C99对数组做出了不同的规定:在C89中,必须使用数值常量指明数组长度,不能使用变量(不管变量有没有被初始化);而在C99中,可以使用变量指明数组长度。

下面的代码使用数值常量指明数组长度,在任何编译器下都能编译通过:

<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">10</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">];</span>  <span class="sh_comment" style="color: rgb(56, 173, 36);">//长度为10</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> b<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">3</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">*</span><span class="sh_number" style="color: rgb(50, 186, 6);">5</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">];</span>  <span class="sh_comment" style="color: rgb(56, 173, 36);">//长度为15</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> c<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">4+8</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">];</span>  <span class="sh_comment" style="color: rgb(56, 173, 36);">//长度为12</span></li></ol>
下面的代码使用变量指明数组长度,在C-Free 5.0、GCC下编译通过,而在VC 6.0、VS2010下会报错:
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> m <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_number" style="color: rgb(50, 186, 6);">10</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> n<span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_function" style="color: rgb(209, 28, 237);">scanf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%d"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">&</span>n<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span>m<span class="sh_symbol" style="color: rgb(48, 48, 238);">],</span> b<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span>n<span class="sh_symbol" style="color: rgb(48, 48, 238);">];</span></li></ol>
变长数组仍然是静态数组,一旦确定长度后就不能改变。

在实际编程中,有时数组的长度不能提前确定,如果这个变化范围小,那么使用数值常量定义一个足够大的数组就可以,如果这个变化范围大,就可能会浪费空间,这时就可以使用变长数组。请看下面的代码:
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdio.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> <span class="sh_function" style="color: rgb(209, 28, 237);">main</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">()</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> n<span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"Input string length: "</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">scanf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%d"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">&</span>n<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">fflush</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>stdin<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">char</span> str<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span>n<span class="sh_symbol" style="color: rgb(48, 48, 238);">];</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"Input string: "</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">gets</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>str<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">puts</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>str<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li></ol>
在C-Free 5.0下的运行结果:
Input string length: 30↙
Input string: http://c.biancheng.net↙
http://c.biancheng.net


9.对C语言数组的总结以及实例讲解

数组(Array)是一系列相同类型的数据的集合,可以是一维的、二维的、多维的;最常用的是一维数组和二维数组,多维数组较少用到。

对数组的总结

1) 数组的定义格式为:
type arrayName[length]
type 为数据类型,arrayName 为数组名,length 为数组长度。 需要注意的是:
  • 在不支持C99的编译器中,length 必须是一个数值常量,不能是变量,例如 VC6.0、VS2010 等;在支持C99的编译器中,length 还可以是变量,例如 C-Free 5.0、GCC等。
  • 数组在内存中占用一段连续的空间,数组名表示的是这段内存空间的首地址。

2) 访问数组中某个元素的格式为:
arrayName[index]
index 为数组下标。注意 index 的值必须大于等于零,并且小于数组长度,否则会发生数组越界,出现意想不到的错误。

3) 可以对数组中的单个元素赋值,也可以整体赋值,例如:
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_comment" style="color: rgb(56, 173, 36);">// 对单个元素赋值</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">3</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">];</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_number" style="color: rgb(50, 186, 6);">3</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">1</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_number" style="color: rgb(50, 186, 6);">100</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">2</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_number" style="color: rgb(50, 186, 6);">34</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_comment" style="color: rgb(56, 173, 36);">// 整体赋值(不指明数组长度)</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">float</span> b<span class="sh_symbol" style="color: rgb(48, 48, 238);">[]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span> <span class="sh_number" style="color: rgb(50, 186, 6);">23.3</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">100.00</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">10</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0.34</span> <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_comment" style="color: rgb(56, 173, 36);">// 整体赋值(指明数组长度)</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> m<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">10</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span> <span class="sh_number" style="color: rgb(50, 186, 6);">100</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">30</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_number" style="color: rgb(50, 186, 6);">234</span> <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_comment" style="color: rgb(56, 173, 36);">// 字符数组赋值</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">char</span> str1<span class="sh_symbol" style="color: rgb(48, 48, 238);">[]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_string" style="color: rgb(24, 97, 167);">"http://c.biancheng.net"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_comment" style="color: rgb(56, 173, 36);">// 将数组所有元素都初始化为0</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> arr<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">10</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">char</span> str2<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">20</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li></ol>

数组应用举例

【示例1】求一个整型数组中的最大值和最小值。
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdio.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> <span class="sh_function" style="color: rgb(209, 28, 237);">main</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">()</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">10</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> max<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> min<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_comment" style="color: rgb(56, 173, 36);">//从控制台获取用户输入并赋值给数组元素</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">for</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);"><</span><span class="sh_number" style="color: rgb(50, 186, 6);">10</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);">++)</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(209, 28, 237);">scanf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%d"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">&</span>a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">]);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_comment" style="color: rgb(56, 173, 36);">//假设a[0]是最大值也是最小值</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    max <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">],</span> min <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">];</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">for</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_number" style="color: rgb(50, 186, 6);">1</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);"><</span><span class="sh_number" style="color: rgb(50, 186, 6);">10</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span> i<span class="sh_symbol" style="color: rgb(48, 48, 238);">++)</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">></span> max<span class="sh_symbol" style="color: rgb(48, 48, 238);">)</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">            max <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">];</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">]</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);"><</span> min<span class="sh_symbol" style="color: rgb(48, 48, 238);">)</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">            min <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> a<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">];</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"The max is %d, The min is %d</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> max<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> min<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li></ol>
运行结果:
2 123 45 100 575 240 799 710 10 90↙
The max is 799, The min is 2

这段代码有两点需要说明:
1) 从控制台获取数组元素时,我们一次性输入10个整数才按下回车键,而不是每输入一个整数就按一次回车键,这正是利用了标准输入缓冲区。

2) 要想求得数组中的最大值和最小值,就得循环比较数组中的所有元素,并设置两个变量 max 和 min 来接收。以最大值为例,开始循环之前,先假设第0个元素是最大值(当然你也可以假设第1、2、3 个元素),然后用 max 和数组中剩余的元素进行比较,如果某个元素的值比 max 大,就用这个元素的值替换 max 的值,等把所有元素遍历完了,max 中就是最大值了。

关于排序和查找

学完了数组,有两个重要的知识点要求大家掌握,那就是排序(Sort)查找(Search),比如:
  • 给你 10 个打乱顺序的整数,要能够按照从小到大或者从大到小的顺序输出;
  • 给定一个字符串 str1,以及一个子串 str2,要能够判断 str2 是否在 str1 中。

10.C语言非阻塞式键盘监听

监听键盘可以使用C语言的字符输入函数,例如 getchar、getch、getche 等,我们会在《结合缓冲区谈谈C语言getchar()、getche()、getch()的区别》一节中重点讲解它们的区别。

使用getche函数监听键盘的例子:

<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdio.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><conio.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> <span class="sh_function" style="color: rgb(209, 28, 237);">main</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">()</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">char</span> ch<span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> i <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_comment" style="color: rgb(56, 173, 36);">//循环监听,直到按Esc键退出</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">while</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>ch <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_function" style="color: rgb(209, 28, 237);">getch</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">())</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>ch <span class="sh_symbol" style="color: rgb(48, 48, 238);">==</span> <span class="sh_number" style="color: rgb(50, 186, 6);">27</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">)</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">            <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">break</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span><span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">else</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">            <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"Number: %d</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">++</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li></ol>
运行结果:
Number: 1  //按下任意键
Number: 2  //按下任意键
Number: 3
Number: 4
Number: 5  //按下Esc键

这段代码虽然达到了监听键盘的目的,但是每次都必须按下一个键才能执行getch后面的代码,也就是说,getch后面的代码被阻塞了。

阻塞式键盘监听非常不方便,尤其是在游戏中,往往意味着用户要不停按键游戏才能进行,所以一般采用非阻塞式键盘监听。

使用conio.h头文件中的kbhit函数可以实现非阻塞式键盘监听。

我们每按下一个键,都会将对应的字符放到键盘缓冲区,kbhit 函数会检测缓冲区中是否有字符,如果有字符返回非0值,没有返回0。但是kbhit不会读取字符,字符仍然留在缓冲区。请看下面的例子:
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdio.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><windows.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><conio.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> <span class="sh_function" style="color: rgb(209, 28, 237);">main</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">()</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">char</span> ch<span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> i <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_comment" style="color: rgb(56, 173, 36);">//循环监听,直到按Esc键退出</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">while</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_number" style="color: rgb(50, 186, 6);">1</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">)</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_function" style="color: rgb(209, 28, 237);">kbhit</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">())</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">            ch <span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span> <span class="sh_function" style="color: rgb(209, 28, 237);">getch</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">();</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">            <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>ch <span class="sh_symbol" style="color: rgb(48, 48, 238);">==</span> <span class="sh_number" style="color: rgb(50, 186, 6);">27</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">)</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">                <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">break</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">            <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"Number: %d</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">++</span>i<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">        <span class="sh_function" style="color: rgb(209, 28, 237);">Sleep</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_number" style="color: rgb(50, 186, 6);">1000</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span>  <span class="sh_comment" style="color: rgb(56, 173, 36);">//暂停1秒</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li></ol>
运行结果:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5  //按下Esc键

每次循环,kbhit 会检测用户是否按下某个键(也就是检测缓冲区中是否有字符),没有的话继续执行后面的语句,有的话就通过 getch 读取,并判断是否是 Esc,是的话就退出循环,否则继续循环。

kbhit 之所以能够实现非阻塞式监听是因为它只检测字符,而不要求输入字符。

Sleep 是“睡眠”的意思,用来让程序暂停执行一段时间,以毫秒记。


11.C语言获取随机数

可能大家在编程的时候需要电脑来获取一些随机的反应,这个时候我们可以使用随机数,比较常见的是 rand() 函数,它可以随机的产生 0 ~ rand_max 的随机数。rand_max 是一个很大的数字,具体关系到IDE和数据类型,我们一般的需要不可能超出它的范围。

下面是一个实例:

<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdio.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdlib.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> <span class="sh_function" style="color: rgb(209, 28, 237);">main</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">()</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> a<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_function" style="color: rgb(209, 28, 237);">rand</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">();</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%d</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span>a<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li></ol>

编译后再运行几次,你会发现产生的随机数是相同的。实际上,rand() 函数产生的随机数是伪随机数,是根据一个数按照某个公式推算出来的,这个数我们称之为“种子”,但是这个种子在系统启动之后就是一个定值,我们需要用 srand() 来进行播种,即在int a前加一句:
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_function" style="color: rgb(209, 28, 237);">srand</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">((</span><span class="sh_type" style="color: rgb(207, 149, 17);">unsigned</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">)</span><span class="sh_function" style="color: rgb(209, 28, 237);">time</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>NULL<span class="sh_symbol" style="color: rgb(48, 48, 238);">));</span>     <span class="sh_comment" style="color: rgb(56, 173, 36);">//这里利用时间进行播种,需要time.h</span></li></ol>

这样,我们就能得到不同的随机数,其实C语言中还有一个 random() 函数可以获取随机数,但是 random() 函数不是ANSI C标准,不能在VC等编译器通过,所以比较少用。

那如何产生一定范围的随机数呢?我们可以利用取模的方法:
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> a<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_function" style="color: rgb(209, 28, 237);">rand</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">()%</span><span class="sh_number" style="color: rgb(50, 186, 6);">10</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span>    <span class="sh_comment" style="color: rgb(56, 173, 36);">//产生0~9的随机数,注意10会被整除</span></li></ol>
如果要规定上下限:
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> a<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_function" style="color: rgb(209, 28, 237);">rand</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">()%</span><span class="sh_number" style="color: rgb(50, 186, 6);">51+13</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span>    <span class="sh_comment" style="color: rgb(56, 173, 36);">//产生13~63的随机数</span></li></ol>
分析:取模即取余,rand()%51+13我们可以看成2部分:rand()%51是产生0~50的随机数,后面+13保证a最小只能是13,最大就是50+13=63。

最后给出产生 13~63 范围内随机数的完整代码:
复制纯文本新窗口
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdio.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_preproc" style="color: rgb(83, 116, 176);">#include</span> <span class="sh_string" style="color: rgb(24, 97, 167);"><stdlib.h></span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_type" style="color: rgb(207, 149, 17);">int</span> <span class="sh_function" style="color: rgb(209, 28, 237);">main</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">()</span><span class="sh_cbracket" style="color: rgb(48, 48, 238);">{</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_type" style="color: rgb(207, 149, 17);">int</span> a<span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">srand</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">((</span><span class="sh_type" style="color: rgb(207, 149, 17);">unsigned</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">)</span><span class="sh_function" style="color: rgb(209, 28, 237);">time</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span>NULL<span class="sh_symbol" style="color: rgb(48, 48, 238);">));</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    a<span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_function" style="color: rgb(209, 28, 237);">rand</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">()%</span><span class="sh_number" style="color: rgb(50, 186, 6);">51+13</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_function" style="color: rgb(209, 28, 237);">printf</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">(</span><span class="sh_string" style="color: rgb(24, 97, 167);">"%d</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span>a<span class="sh_symbol" style="color: rgb(48, 48, 238);">);</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">    <span class="sh_keyword" style="color: rgb(255, 48, 48); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">;</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(48, 48, 238);">}</span></li><li></li></ol>
0 0
原创粉丝点击