C語言函數與遞歸四

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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章