1 函数的定义
1.1 问题
自定义两个简单的函数,使用return返回数据。
1.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:函数的定义
代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">getInt</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">double <span class="sh_function" style="color: rgb(0, 68, 102);">getDouble</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">5.5</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> n <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">getInt</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">();</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> double d<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> d <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">getDouble</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">();</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%lf</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> d<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">getInt</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
定义了一个函数getInt,该函数返回一个整型数据,没有参数。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">double <span class="sh_function" style="color: rgb(0, 68, 102);">getDouble</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">5.5</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
定义了一个函数getDouble,该函数返回一个双精度浮点型数据,没有参数。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> n <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">getInt</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">();</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
首先,定义一个整型变量n,用于存储getInt函数的返回值。
然后,调用函数getInt得到返回值,并存储在变量n中。
最后,使用函数printf输出变量n的内容。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> double d<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> d <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">getDouble</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">();</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%lf</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> d<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
首先,定义一个双精度浮点型变量d,用于存储getDouble函数的返回值。
然后,调用函数getDouble得到返回值,并存储在变量d中。
最后,使用函数printf输出变量d的内容。
1.3 完整代码
本案例的完整代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">getInt</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">double <span class="sh_function" style="color: rgb(0, 68, 102);">getDouble</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">5.5</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> n <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">getInt</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">();</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> double d<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> d <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">getDouble</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">();</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%lf</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> d<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
2 函数的声明
2.1 问题
定义两个函数,分别测试隐式声明和显式声明,同时体现出隐式声明中类型的自动提示效果。
2.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:函数的声明
代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">f1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">();</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> double <span class="sh_function" style="color: rgb(0, 68, 102);">f2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_comment" style="color: rgb(255, 128, 0);">/*void*//*int i*/</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">f2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">f1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"f1()</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">double <span class="sh_function" style="color: rgb(0, 68, 102);">f2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>int b<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"f2()</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3.0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">f1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">();</span></li></ol>
由于在调用f1前未对其进行声明,所以是隐式声明。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> double <span class="sh_function" style="color: rgb(0, 68, 102);">f2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_comment" style="color: rgb(255, 128, 0);">/*void*//*int i*/</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
对函数f2进行显示声明。其中,参数可以省略。如果没有任何参数,表示可以接受任意的参数,如果写void表示不接受任何参数。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">f2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
调用函数f2。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">f1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"f1()</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
定义了一个函数f1。该函数没有参数,返回一个整型数据。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">double <span class="sh_function" style="color: rgb(0, 68, 102);">f2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>int b<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"f2()</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3.0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
定义了一个函数f2。该函数有两个参数,分别是整型变量a和b。在函数体中并没有使用这两个参数,这是允许的。
2.3 完整代码
本案例的完整代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">f1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">();</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> double <span class="sh_function" style="color: rgb(0, 68, 102);">f2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_comment" style="color: rgb(255, 128, 0);">/*void*//*int i*/</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">f2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">f1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"f1()</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">double <span class="sh_function" style="color: rgb(0, 68, 102);">f2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>int b<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"f2()</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">3.0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
3 函数参数的应用
3.1 问题
定义两个函数,分别体现参数传递时的值传递和数组做参数时的处理方法。
3.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:函数参数的应用
代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">void <span class="sh_function" style="color: rgb(0, 68, 102);">fun1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"形参a从主函数传入fun1时的值为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> a <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> a <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"形参a在fun1中改变后的值为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">void <span class="sh_function" style="color: rgb(0, 68, 102);">fun2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[])</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"形参array[0]从主函数传入fun2时的值为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"形参array[0]在fun2中改变后的值为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int a <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">5</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"变量a在调用函数fun1之前的值为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fun1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"变量a在调用函数fun1之后的值为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"数组元素a[0]在调用函数fun2之前的值为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fun2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"数组元素a[0]在调用函数fun2之后的值为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">void <span class="sh_function" style="color: rgb(0, 68, 102);">fun1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"形参a从主函数传入fun1时的值为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> a <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> a <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"形参a在fun1中改变后的值为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
首先,定义了一个函数fun1,该函数有一个参数,为整型变量a。
然后,使用函数printf输出形参a从调用函数传入的值。
下一步,将形参a加上10。
最后,再使用函数printf输出形参a加上10后的值。两次调用可以看出形参a的变化情况。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">void <span class="sh_function" style="color: rgb(0, 68, 102);">fun2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[])</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"形参array[0]从主函数传入fun2时的值为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"形参array[0]在fun2中改变后的值为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
首先,定义了一个函数fun1,该函数有一个参数,为整型数组array。
然后,使用函数printf输出形参数组的第一个元素array[0]从调用函数传入的值。
下一步,将形参数组的第一个元素array[0]加上10。
最后,再使用函数printf输出形参数组的第一个元素array[0]加上10后的值。两次调用可以看出形参数组的第一个元素array[0]的变化情况。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int a <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">5</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
在主函数中定义一个整型变量a和一个整型数组array,并将变量a初始化为5,将数组array的第一个元素初始化成1。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"变量a在调用函数fun1之前的值为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fun1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"变量a在调用函数fun1之后的值为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
首先,使用函数printf输出变量a在调用函数fun1之前的值。
然后,调用函数fun1。
最后,再使用函数printf输出变量a在调用函数fun1之后的值。两次调用可以看出变量a的变化情况。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"数组元素a[0]在调用函数fun2之前的值为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fun2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"数组元素a[0]在调用函数fun2之后的值为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li></ol>
首先,使用函数printf输出整型数组array的第一个元素array[0]在调用函数fun2之前的值。
然后,调用函数fun2。
最后,再使用函数printf输出整型数组array的第一个元素array[0]在调用函数fun2之后的值。两次调用可以看出整型数组array的第一个元素array[0]的变化情况。
3.3 完整代码
本案例的完整代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">void <span class="sh_function" style="color: rgb(0, 68, 102);">fun1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"形参a从主函数传入fun1时的值为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> a <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> a <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"形参a在fun1中改变后的值为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">void <span class="sh_function" style="color: rgb(0, 68, 102);">fun2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[])</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"形参array[0]从主函数传入fun2时的值为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"形参array[0]在fun2中改变后的值为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int a <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">5</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"变量a在调用函数fun1之前的值为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fun1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"变量a在调用函数fun1之后的值为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"数组元素a[0]在调用函数fun2之前的值为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fun2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"数组元素a[0]在调用函数fun2之后的值为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
4 函数和程序的终止
4.1 问题
定义一个函数,分别调用return语句和exit()函数,并在主函数中调用测试效果。
4.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:函数和程序的终止
代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdlib<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">void <span class="sh_function" style="color: rgb(0, 68, 102);">fun</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int e<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> e<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">exit</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>EXIT_SUCCESS<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">else</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"输入1在函数fun中调用exit函数</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">输入2函数fun中执行return语句</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">请选择输入:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fun</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"选择2才会输出此句"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">void <span class="sh_function" style="color: rgb(0, 68, 102);">fun</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int e<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> e<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">exit</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>EXIT_SUCCESS<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">else</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
定义了一个函数,该函数有一个参数为整型变量e。当参数e为1时,函数fun将调用函数exit退出程序;当参数e为其它值时,函数fun执行return语句返回。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"输入1在函数fun中调用exit函数</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">输入2函数fun中执行return语句</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">请选择输入:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
首先,定义一个整型变量n,用于存储用户输入的选择。
然后,使用函数printf提示输入选择。
最后,使用函数scanf输入一个选择到变量n中。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fun</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
将用户的选择,变量n,作为实参调用函数fun。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"选择2才会输出此句"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
当用户选择1时,程序会在函数fun中退出,则此语句将不被执行。只有用户选择了2或其他值时,函数fun将执行return语句返回,此语句才被执行。
4.3 完整代码
本案例的完整代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdlib<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">void <span class="sh_function" style="color: rgb(0, 68, 102);">fun</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int e<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> e<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">exit</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>EXIT_SUCCESS<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">else</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"输入1在函数fun中调用exit函数</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">输入2函数fun中执行return语句</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">请选择输入:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fun</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"选择2才会输出此句</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
5 递归和递推的应用
5.1 问题
分别使用递归函数和循环递推的方式实现n的阶乘。
5.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:递归和递推的应用。
代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">factorial</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int f<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> f <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> n <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span> <span class="sh_function" style="color: rgb(0, 68, 102);">factorial</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> f<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"请输入求阶乘的数:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int f <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><=</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> f <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*=</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"递推方法求得%d的阶乘为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> f<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> f <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">factorial</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"递归方法求得%d的阶乘为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> f<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">factorial</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int f<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> f <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> n <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span> <span class="sh_function" style="color: rgb(0, 68, 102);">factorial</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> f<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
定义递归函数factorial,用于计算整数n的阶乘。在该函数中,以下语句:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
为递归函数的出口,因为1的阶乘还是1。在该函数中,以下语句:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int f<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> f <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> n <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span> <span class="sh_function" style="color: rgb(0, 68, 102);">factorial</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
如果求整数n的阶乘,则转化为求n乘以n–1的阶乘,而n–1的阶乘的计算方法还是调用factorial函数,只是参数变成n-1。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"请输入求阶乘的数:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
首先,定义一个整型变量n,用于存储求阶乘的数。
然后,使用函数printf提示输入求阶乘的数。
最后,使用函数scanf输入求阶乘的数到变量n中。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int f <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><=</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> f <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*=</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"递推方法求得%d的阶乘为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> f<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
是使用递推的方法求整数n的阶乘,n的阶乘是计算方法为1x2x3x· · ·xN,所以使用循环逐次相乘,得到整数n的阶乘。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> f <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">factorial</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"递归方法求得%d的阶乘为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> f<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
是使用递归的方法求整数n的阶乘,调用函数factorial,得到整数n的阶乘。
5.3 完整代码
本案例的完整代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">factorial</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int f<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> f <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> n <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span> <span class="sh_function" style="color: rgb(0, 68, 102);">factorial</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> f<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"请输入求阶乘的数:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int f <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><=</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> f <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*=</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"递推方法求得%d的阶乘为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> f<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> f <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">factorial</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"递归方法求得%d的阶乘为%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> f<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
6 递归和递推的比较
6.1 问题
定义两个函数,分别用递归和递推方式计算费氏数列的第n项,并利用time命令查看时间差。
6.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:递归和递推的应用。
代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>time<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">long <span class="sh_function" style="color: rgb(0, 68, 102);">fib</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> long f1 <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> long f2 <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> long fn <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fn <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> f1 <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> f2<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> f1 <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> f2<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> f2 <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> fn<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> fn<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">long <span class="sh_function" style="color: rgb(0, 68, 102);">fib_f</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> n <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">||</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">2</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_function" style="color: rgb(0, 68, 102);">fib_f</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_function" style="color: rgb(0, 68, 102);">fib_f</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"请输入月份:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> time_t begin <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">time</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> long sum <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">fib</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> time_t stop <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">time</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"费氏数列第%d月兔子数为%ld,递推方法用时%lf秒</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> sum<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">difftime</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>stop<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> begin<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">));</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> begin <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">time</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> sum <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">fib_f</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> stop <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">time</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"费氏数列第%d月兔子数为%ld,递推方法用时%lf秒</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> sum<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">difftime</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>stop<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> begin<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">));</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">long <span class="sh_function" style="color: rgb(0, 68, 102);">fib</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> long f1 <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> long f2 <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> long fn <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fn <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> f1 <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> f2<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> f1 <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> f2<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> f2 <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> fn<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> fn<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
定义了一个使用递推的方法求费氏数列的函数fib,该函数有一个参数是要求第几个月的兔子数。费氏数列的计算公式为:
f1 = 1
f2 = 1
fn = fn-1 + fn-2
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fn <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> f1 <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> f2<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
按计算公式fn = fn-1 + fn-2计算。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> f1 <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> f2<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> f2 <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> fn<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
为下一次循环中公式fn = fn-1 + fn-2中的f1和f2做准备。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">long <span class="sh_function" style="color: rgb(0, 68, 102);">fib_f</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> n <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">||</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">2</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_function" style="color: rgb(0, 68, 102);">fib_f</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_function" style="color: rgb(0, 68, 102);">fib_f</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
定义了一个使用递归的方法求费氏数列的函数fib_f,该函数有一个参数是要求第几个月的兔子数。该函数中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> n <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">||</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">2</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
为递归出口,根据公式,第1个月和第2个月的兔子数均为1。该函数中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_function" style="color: rgb(0, 68, 102);">fib_f</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_function" style="color: rgb(0, 68, 102);">fib_f</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
根据公式fn = fn-1 + fn-2求第n个月的兔子数,因为求第n-1个月和第n-2个月与求第n个月的兔子数方法相同,都是调用fib_f函数,只是参数不同罢了,所以使用递归方法。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"请输入月份:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
首先,定义一个整型变量n,用于存储求兔子数的月份数。
然后,使用函数printf提示输入求兔子数的月份数。
最后,使用函数scanf输入求兔子数的月份数到变量n中。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> time_t begin <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">time</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> long sum <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">fib</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> time_t stop <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">time</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"费氏数列第%d月兔子数为%ld,递推方法用时%lf秒</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> sum<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">difftime</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>stop<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> begin<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">));</span></li></ol>
首先,使用time函数记录调用递推方法函数fib开始的时间。
然后,调用函数fib计算输入月份的兔子数。
下一步,使用time函数记录调用递推方法函数fib结束的时间。
最后,打印输入月份的兔子数和计算所用的时间数。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> begin <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">time</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> sum <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">fib_f</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> stop <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">time</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"费氏数列第%d月兔子数为%ld,递推方法用时%lf秒</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> sum<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">difftime</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>stop<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> begin<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">));</span></li></ol>
首先,使用time函数记录调用递归方法函数fib_f开始的时间。
然后,调用函数fib_f计算输入月份的兔子数。
下一步,使用time函数记录调用递归方法函数fib_f结束的时间。
最后,打印输入月份的兔子数和计算所用的时间数。
6.3 完整代码
本案例的完整代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>time<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">long <span class="sh_function" style="color: rgb(0, 68, 102);">fib</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> long f1 <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> long f2 <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> long fn <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fn <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> f1 <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> f2<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> f1 <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> f2<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> f2 <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> fn<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> fn<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">long <span class="sh_function" style="color: rgb(0, 68, 102);">fib_f</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> n <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">||</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">2</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_function" style="color: rgb(0, 68, 102);">fib_f</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_function" style="color: rgb(0, 68, 102);">fib_f</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">2</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"请输入月份:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> time_t begin <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">time</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> long sum <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">fib</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> time_t stop <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">time</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"费氏数列第%d月兔子数为%ld,递推方法用时%lf秒</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> sum<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">difftime</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>stop<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> begin<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">));</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> begin <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">time</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> sum <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">fib_f</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> stop <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">time</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"费氏数列第%d月兔子数为%ld,递推方法用时%lf秒</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> sum<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">difftime</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>stop<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> begin<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">));</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
7 递归的典型应用
7.1 问题
使用递归实现汉诺塔。
汉诺塔是指法国数学家爱德华·卢卡斯曾编写过一个印度的古老传说:在世界中心贝拿勒斯(在印度北部)的圣庙里,一块黄铜板上插着三根宝石针。印度教的主神梵天在创造世界的时候,在其中一根针上从下到上地穿好了由大到小的64片金片,这就是所谓的汉诺塔。不论白天黑夜,总有一个僧侣在按照下面的法则移动这些金片:一次只移动一片,不管在哪根针上,小片必须在大片上面。僧侣们预言,当所有的金片都从梵天穿好的那根针上移到另外一根针上时,世界就将在一声霹雳中消灭,而梵塔、庙宇和众生也都将同归于尽。
7.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:递归的典型应用。
代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">void <span class="sh_function" style="color: rgb(0, 68, 102);">move</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>char a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>char b<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>char c<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\t</span><span class="sh_string" style="color: rgb(166, 133, 0);">%c->%c</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>c<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">else</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">move</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n<span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">-1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>c<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>b<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\t</span><span class="sh_string" style="color: rgb(166, 133, 0);">%c->%c</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>c<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">move</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n<span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">-1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>b<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>c<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"请输入要移动的块数:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,&</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">move</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">'a'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">'b'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">'c'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">void <span class="sh_function" style="color: rgb(0, 68, 102);">move</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>char a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>char b<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>char c<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\t</span><span class="sh_string" style="color: rgb(166, 133, 0);">%c->%c</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>c<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">else</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">move</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n<span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">-1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>c<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>b<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\t</span><span class="sh_string" style="color: rgb(166, 133, 0);">%c->%c</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>c<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">move</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n<span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">-1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>b<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>c<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
定义了一个递归函数move,用来模拟移动盘子。该函数有四个参数,说明如下:
第一个参数为盘子的数量。
第二个参数为从这根针移走。
第三个参数为经过这根针。
第四个参数为移到这根针。
在该函数中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\t</span><span class="sh_string" style="color: rgb(166, 133, 0);">%c->%c</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>c<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span> </li></ol>
表示当n只有1个盘子的时候直接从a移动到c。
在该函数中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">move</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n<span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">-1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>c<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>b<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span> </li></ol>
表示第n-1个盘子要从a通过c移动到b。
在该函数中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\t</span><span class="sh_string" style="color: rgb(166, 133, 0);">%c->%c</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>c<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
输出从a移动到c。
在该函数中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">move</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n<span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">-1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>b<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>c<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span> </li></ol>
表示n-1个盘子移动到b后,b变开始盘,b通过a移动到c。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"请输入要移动的块数:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,&</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
首先,定义一个整型变量n,用于存储要移动的盘子数。
然后,使用函数printf提示输入要移动的盘子数。
最后,使用函数scanf输入要移动的盘子数到变量n中。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">move</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">'a'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">'b'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">'c'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
启动移动函数,输出移动过程。
7.3 完整代码
本案例的完整代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">void <span class="sh_function" style="color: rgb(0, 68, 102);">move</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>char a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>char b<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>char c<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\t</span><span class="sh_string" style="color: rgb(166, 133, 0);">%c->%c</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>c<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span> <span class="sh_comment" style="color: rgb(255, 128, 0);">//当n只有1个的时候直接从a移动到c</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">else</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">move</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n<span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">-1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>c<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>b<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span> <span class="sh_comment" style="color: rgb(255, 128, 0);">//第n-1个要从a通过c移动到b</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\t</span><span class="sh_string" style="color: rgb(166, 133, 0);">%c->%c</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>c<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">move</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n<span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">-1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>b<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span>c<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span> <span class="sh_comment" style="color: rgb(255, 128, 0);">//n-1个移动过来之后b变开始盘,b通过a移动到c,这边很难理解</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"请输入要移动的块数:"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,&</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">move</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>n<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">'a'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">'b'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">'c'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
标准函数库
1 格式化输入输出
1.1 问题
测试格式化输入输出函数的中格式字符串的各种效果。
1.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:格式化输入输出
代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int a <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> b <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%*d%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>b<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"a = %d, b = %d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> b<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%10d,%-10d,%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">100</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">200</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">300</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%08d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">100</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%0*d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">8</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">100</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"0%o,0x%x</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">8</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">16</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%.2f</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">12.34567</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%10.2f</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">12.34567</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%*d%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>b<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
%*d中的*表示禁止字符,即输入的第一个数据被跳过,不进行赋值。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%10d,%-10d,%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">100</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">200</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">300</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
首先,%10d中的10表示该数据如果不足10位则左边补空格。
然后,%-10d中的-10表示该数据如果不足10位则右边补空格。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%08d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">100</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
%08d中的08表示数据如果不足8位则左边补0。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%0*d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">8</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">100</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
%0*d中的0*表示数据不足后面第一个参数,即8,所含的位数,则左边补0。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"0%o,0x%x</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">8</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">16</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
0%o表示将数据按8进制输出;0x%x表示将数据按16进制输出。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%.2f</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">12.34567</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
%.2f表示将数据保留2位小数。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%10.2f</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">12.34567</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
%10.2表示将数据保留2位小数,且包括小数点在内的位数不足10位时,左边补0。
1.3 完整代码
本案例的完整代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int a <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> b <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">scanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%*d%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>b<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"a = %d, b = %d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> b<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%10d,%-10d,%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">100</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">200</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">300</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%08d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">100</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%0*d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">8</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">100</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"0%o,0x%x</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">8</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">16</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%.2f</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">12.34567</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%10.2f</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">12.34567</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
2 动态分配内存
2.1 问题
动态分配基本类型和字符串的内存,并进行数据存储和取出打印。(最后记得用free()释放)
2.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:动态分配内存
代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>string<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdlib<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pi <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*)</span><span class="sh_function" style="color: rgb(0, 68, 102);">malloc</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">));</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pi <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"*pi = %d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pi<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">free</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>pi<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> float <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pf <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>float<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*)</span><span class="sh_function" style="color: rgb(0, 68, 102);">malloc</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>float<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">));</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pf <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10.23</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"*pf = %f</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">free</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>pf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> double <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pd <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>double<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*)</span><span class="sh_function" style="color: rgb(0, 68, 102);">malloc</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>double<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">));</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pd <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">123.456</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"*pd = %lf</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pd<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">free</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>pd<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pstr <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>char<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*)</span><span class="sh_function" style="color: rgb(0, 68, 102);">malloc</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1024</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">strcpy</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>pstr<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"将字符串放在堆上"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%s</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> pstr<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">free</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>pstr<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pi <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*)</span><span class="sh_function" style="color: rgb(0, 68, 102);">malloc</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">));</span></li></ol>
在堆上动态分配一个整型变量。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pi <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
将动态分配的整型变量赋值。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"*pi = %d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pi<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
使用printf函数输出动态变量的值。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">free</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>pi<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
释放动态变量所占的存储空间。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> float <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pf <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>float<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*)</span><span class="sh_function" style="color: rgb(0, 68, 102);">malloc</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>float<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">));</span></li></ol>
在堆上动态分配一个单精度浮点型变量。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pf <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10.23</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
将动态分配的单精度浮点型变量赋值。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"*pf = %f</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
使用printf函数输出动态变量的值。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">free</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>pf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
释放动态变量所占的存储空间。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> double <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pd <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>double<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*)</span><span class="sh_function" style="color: rgb(0, 68, 102);">malloc</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>double<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">));</span></li></ol>
在堆上动态分配一个双精度浮点型变量。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pd <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">123.456</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
将动态分配的双精度浮点型变量赋值。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"*pd = %lf</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pd<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
使用printf函数输出动态变量的值。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">free</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>pd<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
释放动态变量所占的存储空间。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pstr <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>char<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*)</span><span class="sh_function" style="color: rgb(0, 68, 102);">malloc</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1024</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
在堆上动态分配一个字符型数组。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">strcpy</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>pstr<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"将字符串放在堆上"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
将字符型数组赋值为字符串。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%s</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> pstr<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
使用printf函数输出动态字符型数组。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">free</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>pstr<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
释放动态字符型数组所占的存储空间。
2.3 完整代码
本案例的完整代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>string<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdlib<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pi <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*)</span><span class="sh_function" style="color: rgb(0, 68, 102);">malloc</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">));</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pi <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"*pi = %d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pi<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">free</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>pi<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> float <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pf <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>float<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*)</span><span class="sh_function" style="color: rgb(0, 68, 102);">malloc</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>float<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">));</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pf <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10.23</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"*pf = %f</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">free</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>pf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> double <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pd <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>double<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*)</span><span class="sh_function" style="color: rgb(0, 68, 102);">malloc</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>double<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">));</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pd <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">123.456</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"*pd = %lf</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pd<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">free</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>pd<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pstr <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>char<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*)</span><span class="sh_function" style="color: rgb(0, 68, 102);">malloc</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1024</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">strcpy</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>pstr<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"将字符串放在堆上"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%s</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> pstr<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">free</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>pstr<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
3 动态分配内存(续1)
3.1 问题
动态分配整型数组和结构的内存,并进行数据存储和取出打印。(最后记得用free()释放)
3.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:动态分配内存(续1)
代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdlib<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>array <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*)</span><span class="sh_function" style="color: rgb(0, 68, 102);">malloc</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d "</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">free</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> struct Data</li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> double b<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char c<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> struct Data <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>p <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>struct Data<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*)</span><span class="sh_function" style="color: rgb(0, 68, 102);">malloc</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>struct Data<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">));</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> p<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>a <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> p<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>b <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">12.34</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> p<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>c <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'a'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d %lf %c</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> p<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> p<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>b<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> p<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>c<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">free</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>p<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>array <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*)</span><span class="sh_function" style="color: rgb(0, 68, 102);">malloc</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
在堆上定义了一个整型数组。该数组共有10个元素。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
使用循环对堆上的数组元素进行逐个赋值。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d "</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
使用循环遍历输出堆上的数组中的所有元素。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">free</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
释放堆上的整型数组。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> struct Data</li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> double b<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char c<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
定义一个结构体Data,有三个成员,整型变量a,双精度浮点型变量b,字符型变量c。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> struct Data <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>p <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>struct Data<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*)</span><span class="sh_function" style="color: rgb(0, 68, 102);">malloc</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>struct Data<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">));</span></li></ol>
在堆上动态分配一个结构体Data的变量。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> p<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>a <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> p<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>b <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">12.34</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> p<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>c <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'a'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
対堆上的结构体变量的每个成员进行赋值。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d %lf %c</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> p<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> p<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>b<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> p<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>c<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
使用printf函数输出堆上的结构体Data的变量。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">free</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>p<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
释放堆上的结构体Data的变量。
3.3 完整代码
本案例的完整代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdlib<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>array <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*)</span><span class="sh_function" style="color: rgb(0, 68, 102);">malloc</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d "</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">free</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>array<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> struct Data</li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> double b<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char c<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> struct Data <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>p <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>struct Data<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*)</span><span class="sh_function" style="color: rgb(0, 68, 102);">malloc</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>struct Data<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">));</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> p<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>a <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> p<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>b <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">12.34</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> p<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>c <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'a'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d %lf %c</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> p<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> p<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>b<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> p<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>c<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">free</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>p<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
4 时间函数的使用
4.1 问题
打印年月日小时分秒格式的系统当前时间。
4.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:时间函数的使用
代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>time<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> time_t t <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">time</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> struct tm <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span> local <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">localtime</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(&</span>t<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d年%d月%d日 %d:%d:%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1900</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> local<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>tm_year<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> local<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>tm_mon <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> local<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>tm_mday<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> local<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>tm_hour<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> local<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>tm_min<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> local<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>tm_sec<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> time_t t <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">time</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
使用函数time获取当前系统时间。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> struct tm <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span> local <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">localtime</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(&</span>t<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
将系统时间转换成本地时间。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d年%d月%d日 %d:%d:%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1900</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> local<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>tm_year<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> local<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>tm_mon <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> local<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>tm_mday<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> local<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>tm_hour<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> local<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>tm_min<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> local<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>tm_sec<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
使用printf函数输出本地时间。
4.3 完整代码
本案例的完整代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>time<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">()</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> time_t t <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">time</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> struct tm <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span> local <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">localtime</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(&</span>t<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d年%d月%d日 %d:%d:%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1900</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> local<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>tm_year<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> local<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>tm_mon <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> local<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>tm_mday<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> local<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>tm_hour<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> local<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>tm_min<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> local<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-></span>tm_sec<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
输入输出函数(IO)
1 打开文件
1.1 问题
用各种模式打开文件。
1.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:格式化输入输出
代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>void<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> FILE<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"w"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"a"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"r"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> FILE<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
定义一个FILE类型的指针fp。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"w"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
首先,使用函数fopen打开一个文件,该函数有两个参数,说明如下:
第一个参数为字符串包含欲打开的文件路径及文件名。
第二个参数为字符串代表文件打开方式。
在上述语句中,打开的文件名为当前目录下的file.c。使用w方式打开,该方式打开只写文件,若文件存在则清空原文件,若不存在新建。
然后,使用函数fclose关闭打开的文件。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"a"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
首先,使用函数fopen打开一个文件。
在上述语句中,打开的文件名为当前目录下的file.c。使用a方式打开,该方式以追加的方式打开只写文件,若文件存在则在最后追加写入,若不存在新建。
然后,使用函数fclose关闭打开的文件。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"r"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
首先,使用函数fopen打开一个文件。
在上述语句中,打开的文件名为当前目录下的file.c。使用r方式打开,该方式打开只读文件,该文件必须存在。
然后,使用函数fclose关闭打开的文件。
1.3 完整代码
本案例的完整代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>void<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> FILE<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"w"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"a"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"r"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
2 使用fscanf()和fprintf()读写文件
2.1 问题
fscanf函数的功能是从一个流中执行格式化输入,fscanf遇到空格和换行时结束。
fprintf函数的功能是传送格式化输出到一个文件中。
2.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:使用fscanf()和fprintf()读写文件
代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>void<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> FILE<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"w"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int a <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> float f <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">12.34</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> double d <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">12345.6789</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char c <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'a'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fprintf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"%c %d %f %lf"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> c<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> f<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> d<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"r"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char c1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int a1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> float f1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> double d1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fscanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"%c%d%f%lf"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>c1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>a1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>f1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>d1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%c</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">%f</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">%lf</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> c1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> a1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> f1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> d1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> FILE<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
定义一个FILE类型的指针fp。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"w"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
使用函数fopen用w的方式打开一个文件file.c。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int a <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> float f <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">12.34</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> double d <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">12345.6789</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char c <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'a'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
定义四个变量,并对它们进行初始化。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fprintf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"%c %d %f %lf"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> c<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> f<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> d<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
使用函数fprintf用格式化方法将上述四个变量写入文件中。该函数与printf使用方法非常类似。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> float <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>pf <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>float<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*)</span><span class="sh_function" style="color: rgb(0, 68, 102);">malloc</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>float<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">));</span></li></ol>
在堆上动态分配一个单精度浮点型变量。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
使用函数fclose关闭打开的文件。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"r"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
首先,使用函数fopen用r的方式重新打开文件file.c。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char c1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int a1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> float f1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> double d1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
重新定义四个新的变量,但并不对它们进行初始化。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fscanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"%c%d%f%lf"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>c1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>a1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>f1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>d1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
使用函数fscanf用格式化方法从文件file.c中读入数据到这四个新的变量中。该函数与scanf使用方法非常类似。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%c</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">%f</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">%lf</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> c1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> a1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> f1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> d1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
使用函数printf输出这四个新的变量值,已验证读入的效果。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
使用函数fclose关闭打开的文件。
2.3 完整代码
本案例的完整代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>void<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> FILE<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"w"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int a <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> float f <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">12.34</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> double d <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">12345.6789</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char c <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_string" style="color: rgb(166, 133, 0);">'a'</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fprintf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"%c %d %f %lf"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> c<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> f<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> d<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"r"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char c1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int a1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> float f1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> double d1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fscanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"%c%d%f%lf"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>c1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>a1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>f1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>d1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%c</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">%f</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">%lf</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> c1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> a1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> f1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> d1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
3 使用fread()和fwrite()读写文件
3.1 问题
使用fwrite()和fread()读写int、double、字符串类型的数据,先写入后读取。
3.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:使用fread()和fwrite()读写文件
代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>string<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>void<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> FILE<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"w"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int a <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> double d <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">12345.6789</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char str<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1024</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"This is a string."</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fwrite</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(&</span>a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fwrite</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(&</span>d<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>double<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int len <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">strlen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>str<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fwrite</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(&</span>len<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fwrite</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>str<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>char<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> len<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"r"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int a1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> double d1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char str1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1024</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fread</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(&</span>a1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fread</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(&</span>d1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>double<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int len1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fread</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(&</span>len1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fread</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>str1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>char<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> len<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">%lf</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">%s</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> a1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> d1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> str1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> FILE<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
定义一个FILE类型的指针fp。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"w"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
使用函数fopen用w的方式打开一个文件file.c。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int a <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> double d <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">12345.6789</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char str<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1024</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"This is a string."</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
定义两个变量和一个数组,并对它们进行初始化。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fwrite</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(&</span>a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
使用fwrite函数将变量a写入文件。该函数有四个参数,说明如下:
第一个参数为是一个指针,是要获取写入文件的数据的地址。
第二个参数为要写入内容的单字节数。
第三个参数为要进行写入第二个参数中字节的数据项的个数。
第四个参数为目标文件指针。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fwrite</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(&</span>d<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>double<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
使用fwrite函数将变量d写入文件。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int len <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">strlen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>str<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fwrite</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(&</span>len<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fwrite</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>str<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>char<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> len<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
使用fwrite函数将字符数组str写入文件。写入字符数组时,应先将该数组中字符串的长度存入文件,再存入字符串本身,这样有理由读文件时,定义读取字符串的长度。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
使用函数fclose关闭打开的文件。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"r"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
使用函数fopen用r的方式重新打开文件file.c。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int a1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> double d1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char str1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1024</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li></ol>
重新定义两个变量和一个数组,但并不对它们进行初始化。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fread</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(&</span>a1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
使用fread函数从文件中读入数据放在变量a1中。该函数有四个参数,说明如下:
第一个参数为是一个指针,是要将获取的数据写入到的地址。
第二个参数为要读取内容的单字节数。
第三个参数为要进行读取第二个参数中字节的数据项的个数。
第四个参数为目标文件指针。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fread</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(&</span>d1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>double<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
使用fread函数从文件中读入数据放在变量d1中。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int len1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fread</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(&</span>len1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fread</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>str1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>char<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> len<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
使用fread函数从文件中读入数据放在字符数组str中。在读取字符串前,先读取该字符串的长度,然后根据长度读取字符串的内容。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">%lf</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">%s</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> a1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> d1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> str1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
使用printf函数验证读取的内容是否正确。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
使用函数fclose关闭打开的文件。
3.3 完整代码
本案例的完整代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>string<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>void<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> FILE<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"w"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int a <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> double d <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">12345.6789</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char str<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1024</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"This is a string."</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fwrite</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(&</span>a<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fwrite</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(&</span>d<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>double<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int len <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">strlen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>str<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fwrite</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(&</span>len<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fwrite</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>str<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>char<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> len<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"r"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int a1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> double d1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char str1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1024</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fread</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(&</span>a1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fread</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(&</span>d1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>double<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int len1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fread</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(&</span>len1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fread</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>str1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>char<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> len<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">%lf</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">%s</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> a1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> d1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> str1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
4 使用fread()和fwrite()读写文件(续1)
4.1 问题
输入学生信息(学号、姓名、出生日期),然后用结构存储数据,并读写入文件中。
4.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:使用fread()和fwrite()读写文件(续1)
代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>string<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">struct student</li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int id<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char name<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">20</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char address<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">200</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>void<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> FILE<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"w"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> struct student stu <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10000</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"zhangsan"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"江苏南京"</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fwrite</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(&</span>stu<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>struct student<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"r"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> struct student stu1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fread</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(&</span>stu1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>struct student<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d %s %s</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> stu1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>id<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> stu1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>name<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> stu1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>address<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> FILE<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
定义一个FILE类型的指针fp。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"w"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
使用函数fopen用w的方式打开一个文件file.c。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> struct student stu <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10000</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"zhangsan"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"江苏南京"</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
定义一个结构体student的变量stu,并对它进行初始化。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fwrite</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(&</span>stu<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>struct student<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
使用fwrite函数将结构体student的变量stu写入文件。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
使用函数fclose关闭打开的文件。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"r"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
使用函数fopen用r的方式重新打开文件file.c。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> struct student stu1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
重新定义一个结构体student的变量stu1,但并不对它进行初始化。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fread</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(&</span>stu1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>struct student<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
使用fread函数从文件中读入数据放在结构体student的变量stu1中。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d %s %s</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> stu1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>id<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> stu1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>name<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> stu1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>address<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
使用printf函数验证读取的内容是否正确。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
使用函数fclose关闭打开的文件。
4.3 完整代码
本案例的完整代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>string<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">struct student</li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int id<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char name<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">20</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char address<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">200</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">];</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>void<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> FILE<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"w"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> struct student stu <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10000</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"zhangsan"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"江苏南京"</span><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fwrite</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(&</span>stu<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>struct student<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"r"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> struct student stu1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fread</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(&</span>stu1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>struct student<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%d %s %s</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> stu1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>id<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> stu1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>name<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> stu1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>address<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
5 fseek()的使用
5.1 问题
测试fseek()的效果,包括三个不同的起始位置。
5.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:fseek()的使用
代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>string<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>void<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> FILE<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"w+"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"abcdefghijklmnopqrstuvwxyz"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fwrite</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>char<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">26</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fseek</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> SEEK_SET<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">memset</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">26</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fread</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>char<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%s</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fseek</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">5</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> SEEK_CUR<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">memset</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">26</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fread</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>char<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%s</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fseek</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> SEEK_END<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">memset</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">26</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fread</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>char<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%s</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> FILE<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
定义一个FILE类型的指针fp。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"w+"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
使用函数fopen用w+的方式打开一个文件file.c。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"abcdefghijklmnopqrstuvwxyz"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
定义一个字符数组buf,并初始化为一个字符串。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fwrite</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>char<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">26</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
使用fwrite函数将字符串buf写入文件。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fseek</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> SEEK_SET<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">memset</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">26</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fread</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>char<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%s</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
首先,使用函数fseek设置文件指针fp的位置为文件头开始的第一个字节。该函数有三个参数,说明如下:
第一个参数为目标文件指针。
第二个参数为从第三个参数开始的偏移量,为正数时向后偏移,为负数时向前偏移。
第三个参数为偏移的起始位置。
然后,使用memset函数将字符串buf清空。
下一步,使用fread函数从文件当前设定位置读入10个字符。
最后,使用printf函数验证读入的字符是从文件开头第一个字节开始的10个字符。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fseek</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">5</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> SEEK_CUR<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">memset</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">26</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fread</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>char<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%s</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
首先,使用函数fseek设置文件指针fp的位置为文件当前位置向后偏移5个字节的位置。
然后,使用memset函数将字符串buf清空。
下一步,使用fread函数从文件当前设定位置读入10个字符。
最后,使用printf函数验证读入的字符是从文件当前位置向后偏移5个字节开始的10个字符。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fseek</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> SEEK_END<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">memset</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">26</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fread</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>char<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%s</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
首先,使用函数fseek设置文件指针fp的位置为文件尾开始向前偏移10个字节的位置。
然后,使用memset函数将字符串buf清空。
下一步,使用fread函数从文件当前设定位置读入10个字符。
最后,使用printf函数验证读入的字符是从文件尾开始向前偏移10个字节开始的10个字符。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
使用函数fclose关闭打开的文件。
5.3 完整代码
本案例的完整代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>string<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>void<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> FILE<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"w+"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"abcdefghijklmnopqrstuvwxyz"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fwrite</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>char<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">26</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fseek</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> SEEK_SET<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">memset</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">26</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fread</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>char<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%s</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fseek</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">5</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> SEEK_CUR<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">memset</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">26</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fread</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>char<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%s</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fseek</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> SEEK_END<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">memset</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">26</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fread</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">sizeof</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>char<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%s</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
6 ftell()的使用
6.1 问题
使用ftell()获取文件的大小。
6.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:ftell()的使用
代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>void<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> FILE<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"r"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fseek</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> SEEK_END<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> long len <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">ftell</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"文件长度为:%ld个字节</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> len<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> FILE<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
定义一个FILE类型的指针fp。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"r"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
使用函数fopen用r的方式打开一个文件file.c。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fseek</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> SEEK_END<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
使用函数fseek设置文件指针fp的位置为文件尾开始的第一个字节。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> long len <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">ftell</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
使用函数ftell获得指定文件的文件指针当前位置距离文件头的第一个字节的字节数。如果文件当前位置在文件尾开始的第一个字节,即文件最后一个字节的下一个位置,则将得到文件长度。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"文件长度为:%ld个字节</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> len<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
打印文件长度。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
使用函数fclose关闭打开的文件。
6.3 完整代码
本案例的完整代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>void<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> FILE<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"r"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fseek</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> SEEK_END<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> long len <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">ftell</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"文件长度为:%ld个字节</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> len<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
7 字符的输入输出
7.1 问题
使用字符的输入输出函数实现文件的复制。
7.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:字符的输入输出
代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>void<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> FILE<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>fp <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"r"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> FILE<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>fp1 <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp1 <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file副本.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"w"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> fp1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char ch<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">while</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">((</span>ch <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">getc</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">))</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">!=</span> EOF<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">putc</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>ch<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> FILE<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
定义一个FILE类型的指针fp。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"r"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
使用函数fopen用r的方式打开一个文件file.c。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> FILE<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
定义一个FILE类型的指针fp。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"w"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
使用函数fopen用w的方式打开一个文件file.c。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char ch<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">while</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">((</span>ch <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">getc</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">))</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">!=</span> EOF<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">putc</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>ch<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
首先,在循环的结束条件处,使用函数getc从文件fp中读入一个字符。
然后,在循环体内,将读入的字符写入到另一个文件fp1中。
最后,设置一个循环,逐个字节地读入文件fp的内容,再逐个字节的写入文件fp1,直到文件fp结束,即读入的字节是EOF。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
使用函数fclose关闭打开的文件。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
使用函数fclose关闭打开的文件。
7.3 完整代码
本案例的完整代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>void<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> FILE<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>fp <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"r"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> FILE<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>fp1 <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp1 <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file副本.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"w"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span> fp1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char ch<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">while</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">((</span>ch <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">getc</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">))</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">!=</span> EOF<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">putc</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>ch<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> fp1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
8 字符串函数的使用
8.1 问题
使用sscanf()和sprintf()处理字符串。
8.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:字符串函数的使用
代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>string<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdlib<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>void<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> FILE<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"w"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"abcdefghijklmnopqrstuvwxyz"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fprintf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"%lu%s"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">strlen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"r"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>buf1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int len<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fscanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>len<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> buf1 <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">malloc</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>len<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fscanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"%s"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> buf1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> buf1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>len<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%s</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> buf1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> FILE<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
定义一个FILE类型的指针fp。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"w"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
使用函数fopen用r的方式打开一个文件file.c。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"abcdefghijklmnopqrstuvwxyz"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
定义一个字符数组buf,并对其进行初始化。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fprintf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"%lu%s"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">strlen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
使用函数fprintf将字符数组buf保存到文件中。
注意:在保存之前应先保存字符数组buf中的字符串长度。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
使用函数fclose关闭打开的文件。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"r"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
使用函数fopen用r的方式重新打开文件file.c。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>buf1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int len<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
首先,定义一个字符指针buf1。
然后,定义一个整型变量len。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fscanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>len<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> buf1 <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">malloc</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>len<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
首先,使用fscanf函数从文件中读入字符串的长度到变量len中。
然后,根据len中的值使用malloc函数为字符指针buf1申请存储空间。加1是为了给字符串结束符\0留出位置。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fscanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"%s"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> buf1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> buf1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>len<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%s</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> buf1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
首先,使用fscanf函数从文件中从文件中读入字符串。
然后,将字符数组buf1中的最后一个字节置为\0。
最后,使用printf函数验证读入的内容。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
使用函数fclose关闭打开的文件。
8.3 完整代码
本案例的完整代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>string<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdlib<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>void<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> FILE<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"w"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"abcdefghijklmnopqrstuvwxyz"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fprintf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"%lu%s"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_function" style="color: rgb(0, 68, 102);">strlen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">),</span> buf<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span><span class="sh_function" style="color: rgb(0, 68, 102);">fopen</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"file.c"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span><span class="sh_string" style="color: rgb(166, 133, 0);">"r"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">==</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">-</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> char <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">*</span>buf1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int len<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fscanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"%d"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">&</span>len<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> buf1 <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">malloc</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>len<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">+</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fscanf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_string" style="color: rgb(166, 133, 0);">"%s"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> buf1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> buf1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">[</span>len<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">]</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"%s</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> buf1<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">fclose</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> fp<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span>NULL<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
9 可变长参数的使用
9.1 问题
可变长函数参数是指定义一个函数,该函数的参数个数为不确定的值,如printf函数。
9.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:可变长参数的使用
代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdarg<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">max</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int cnt<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">...)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> va_list v<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">va_start</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>v<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> cnt<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int maxvalue <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">va_arg</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>v<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> cnt<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int data <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">va_arg</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>v<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>data <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span> maxvalue<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> maxvalue <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> data<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">va_end</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>v<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> maxvalue<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>void<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int maxNum <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">max</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">55</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">34</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">28</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">16</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">47</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">62</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">77</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">86</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">91</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">9</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"最大数为:%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> maxNum<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">max</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int cnt<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">...)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> va_list v<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">va_start</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>v<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> cnt<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int maxvalue <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">va_arg</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>v<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> cnt<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int data <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">va_arg</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>v<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>data <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span> maxvalue<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> maxvalue <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> data<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">va_end</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>v<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> maxvalue<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
定义了一个函数max。用于求参数中的最大值。在该函数中,以下语句:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> va_list v<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
定义可变长参数表变量v。在该函数中,以下语句:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">va_start</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>v<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> cnt<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
使用函数va_start将参数cnt之后的那些参数保存到v中。在该函数中,以下语句:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int maxvalue <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">va_arg</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>v<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
使用函数va_arg从参数表中取一个int类型参数放入变量maxvalue中,并假设maxvalue保存的是所有参数中的最大值。在该函数中,以下语句:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int data <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">va_arg</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>v<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>data <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span> maxvalue<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> maxvalue <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> data<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li></ol>
首先,使用函数va_arg从参数表中取一个int类型参数放入变量data中。
然后,将变量data与变量maxvalue对比,如果data大于maxvalue,则将data赋值给maxvalue,以保证maxvalue中保存的永远是最大值。在该函数中,以下语句:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> cnt<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int data <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">va_arg</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>v<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>data <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span> maxvalue<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> maxvalue <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> data<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>
设置循环,将cnt后的参数,逐个遍历,找出最大值。在该函数中,以下语句:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">va_end</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>v<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
使用函数va_end释放可变长参数表v。
上述代码中,以下代码:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>void<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int maxNum <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">max</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">55</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">34</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">28</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">16</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">47</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">62</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">77</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">86</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">91</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">9</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"最大数为:%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> maxNum<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li></ol>
在主函数中,使用函数max,找出参数中的最大值,并输出。
9.3 完整代码
本案例的完整代码如下所示:
<ol class="snippet-num" style="margin: 1em 0px 1em 1em; padding-left: 3em;"><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdio<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">#include <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span>stdarg<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">.</span>h<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">max</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int cnt<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">...)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> va_list v<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">va_start</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>v<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> cnt<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int maxvalue <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">va_arg</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>v<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">for</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>int i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">1</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;"><</span> cnt<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span> i<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">++)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int data <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">va_arg</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>v<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> int<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>data <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">></span> maxvalue<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> maxvalue <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> data<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">va_end</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>v<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> maxvalue<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;">int <span class="sh_function" style="color: rgb(0, 68, 102);">main</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span>void<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">)</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">{</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> int maxNum <span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">=</span> <span class="sh_function" style="color: rgb(0, 68, 102);">max</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">10</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">55</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">34</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">28</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">16</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">47</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">62</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">77</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">86</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">91</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">9</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_function" style="color: rgb(0, 68, 102);">printf</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">(</span><span class="sh_string" style="color: rgb(166, 133, 0);">"最大数为:%d</span><span class="sh_specialchar" style="color: rgb(255, 0, 255); font-weight: bold;">\n</span><span class="sh_string" style="color: rgb(166, 133, 0);">"</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">,</span> maxNum<span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">);</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> </li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"> <span class="sh_keyword" style="color: rgb(187, 121, 119); font-weight: bold;">return</span> <span class="sh_number" style="color: rgb(128, 0, 128); font-weight: bold;">0</span><span class="sh_symbol" style="color: rgb(255, 0, 128); font-weight: bold;">;</span></li><li style="padding-left: 1.5em; list-style: decimal-leading-zero outside none;"><span class="sh_cbracket" style="color: rgb(255, 0, 128); font-weight: bold;">}</span></li></ol>