我愛北京天安門
天安門上太陽升
偉大領袖毛主席
指引我們向前進
<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span style="FONT-SIZE: 14pt; FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">筆記範圍:《</span><span lang="EN" style="FONT-SIZE: 14pt">The C Programming Language</span><span
style="FONT-SIZE: 14pt; FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">》第一章</span><span lang="EN" style="FONT-SIZE: 14pt"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></span></p>
<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN" style="FONT-SIZE: 14pt"><o:p> </o:p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 1.5"><span style="FONT-SIZE: 14pt; FONT-FAMILY:
宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">應該說,算上這次,我應該是第四次讀這本書了,每次重讀都收穫不少。都引起我新的思考,很難想象這本一本技術小書給人留下的感覺是如此深刻,如此令人回味,本章的內容是很容易理解的概念,對於編程學習者是最起碼的知識,但是有些東西還是被初學者忽視。以至於在各個技術社區的初學者問題中層出不斷。其實這些東西,在第一章就得到了比較完整的解釋,下面只是把他們提出來強調一下,</span><span
lang="EN" style="FONT-SIZE: 14pt"><o:p></o:p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 63.75pt; TEXT-INDENT: -42.75pt; tab-stops: list 63.75pt; mso-list: l0 level1 lfo1"><span lang="EN" style="FONT-SIZE: 14pt; mso-bidi-font-family: 宋體"><span
style="mso-list: Ignore">第一.</span></span><span style="FONT-SIZE: 14pt; FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">關於迴圈終止。在很多書籍中都存在這樣的迴圈語句:</span><span lang="EN" style="FONT-SIZE: 14pt"><o:p></o:p></span></p>
<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 63.75pt"><span lang="EN" style="FONT-SIZE: 14pt">while ( getchar() != EOF )<span style="mso-spacerun: yes"> </span>{….}<o:p></o:p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 63.75pt"><span
style="FONT-SIZE: 14pt; FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">很多人不明白這個</span><span lang="EN" style="FONT-SIZE: 14pt">EOF</span><span style="FONT-SIZE: 14pt; FONT-FAMILY: 宋體; mso-ascii-font-family:
'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">為何物?具體如何操作?以至於讓上面的語句變成了無限迴圈,呵呵,其實</span><span lang="EN" style="FONT-SIZE: 14pt">EOF</span><span style="FONT-SIZE: 14pt; FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family:
'Times New Roman'">。是檔案結束符(</span><span lang="EN" style="FONT-SIZE: 14pt">end of file</span><span style="FONT-SIZE: 14pt; FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">),在第七章有說明。其為系統常量。值為</span><span lang="EN"
style="FONT-SIZE: 14pt">-1</span><span style="FONT-SIZE: 14pt; FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,當然你在終端輸入</span><span lang="EN" style="FONT-SIZE: 14pt">-1</span><span style="FONT-SIZE: 14pt;
FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,迴圈並未結束,</span><span lang="EN" style="FONT-SIZE: 14pt">why</span><span style="FONT-SIZE: 14pt; FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family:
'Times New Roman'">?</span><span lang="EN" style="FONT-SIZE: 14pt">how to do</span><span style="FONT-SIZE: 14pt; FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">?恩,你在《</span><span lang="EN" style="FONT-SIZE:
14pt">c</span><span style="FONT-SIZE: 14pt; FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">專家編程》裡會瞭解到,</span><span lang="EN" style="FONT-SIZE: 14pt">c</span><span style="FONT-SIZE: 14pt; FONT-FAMILY: 宋體;
mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的第一批使用者都是系統設計者和編譯器設計者。在他們的理念裡,資訊往往以檔案為單位的。這個標誌只是檔案結束的狀態,一般不由使用者提供。而鍵盤等輸入端在</span><span lang="EN" style="FONT-SIZE: 14pt">os</span><span style="FONT-SIZE: 14pt; FONT-FAMILY:
宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中是個極其特殊的檔案。需要使用者顯式標誌檔案結束。說是系統常量當然有著系統依賴性,因此不同的系統標誌方式就完全不同。</span><span lang="EN" style="FONT-SIZE: 14pt">Windows</span><span style="FONT-SIZE: 14pt; FONT-FAMILY: 宋體; mso-ascii-font-family:
'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">下是</span><span lang="EN" style="FONT-SIZE: 14pt">ctrl+z</span><span style="FONT-SIZE: 14pt; FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</span><span
lang="EN" style="FONT-SIZE: 14pt">linux</span><span style="FONT-SIZE: 14pt; FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">下是</span><span lang="EN" style="FONT-SIZE: 14pt">ctrl+D.</span><span style="FONT-SIZE:
14pt; FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">另外</span><span lang="EN" style="FONT-SIZE: 14pt">EOF</span><span style="FONT-SIZE: 14pt; FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family:
'Times New Roman'">其實不用顯式說明。上面的代碼與此完全等價:</span><span lang="EN" style="FONT-SIZE: 14pt"><o:p></o:p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 63.75pt"><span lang="EN" style="FONT-SIZE: 14pt">while( getchar() ) {……}<o:p></o:p></span></p> <p
class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 63.75pt; TEXT-INDENT: -42.75pt; tab-stops: list 63.75pt; mso-list: l0 level1 lfo1"><span lang="EN" style="FONT-SIZE: 14pt; mso-bidi-font-family: 宋體"><span style="mso-list: Ignore">第二.</span></span><span style="FONT-SIZE:
14pt; FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">聲明和定義。儘管這個問題地球人都知道,但是真正說的清楚的人並不多。有人認為變數的聲明和定義沒有什麼區別。有人認為這個與初始化有關係(我曾經就這樣白癡,汗~~~),對於後者那就是根本就不明白這兩個概念,聲明只是給編譯器一個提示,有這麼個名字存在於程式中,和運行環境毫無關係。可以重複出現,定義是具體分配記憶體空間和指定了變數的位子(左值),在同一域中只能出現一次;對於前者的觀點。在單檔案程式中。幾乎找不到錯誤。但如果你把這樣的句子放人標頭檔,</span><span
lang="EN" style="FONT-SIZE: 14pt">int a</span><span style="FONT-SIZE: 14pt; FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">;一旦這個標頭檔被重複包涵。必然出現連結錯誤。其實這樣:</span><span lang="EN" style="FONT-SIZE: 14pt"><br
/>extern int a;//</span><span style="FONT-SIZE: 14pt; FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">聲明</span><span lang="EN" style="FONT-SIZE: 14pt"><br />int a;//</span><span style="FONT-SIZE: 14pt; FONT-FAMILY:
宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">定義</span><span lang="EN" style="FONT-SIZE: 14pt"><o:p></o:p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 63.75pt; TEXT-INDENT: -42.75pt; tab-stops: list
63.75pt; mso-list: l0 level1 lfo1"><span lang="EN" style="FONT-SIZE: 14pt; mso-bidi-font-family: 宋體"><span style="mso-list: Ignore">第三.</span></span><span style="FONT-SIZE: 14pt; FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family:
'Times New Roman'">字元數組和字串。有人認為這兩個是同一概念,是這樣嗎?不,完全不是,前者為容器(資料結構)</span><span lang="EN" style="FONT-SIZE: 14pt">,</span><span style="FONT-SIZE: 14pt; FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">後者為資料。這樣說也許太理論化,好,我們來改寫下那個</span><span
lang="EN" style="FONT-SIZE: 14pt">hello world<br /><br />#include <stdio.h><o:p></o:p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 42pt; mso-char-indent-count: 3.0; mso-para-margin-left: 2.0gd"><span lang="EN" style="FONT-SIZE:
14pt">#include <stdlib.h><o:p></o:p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 49pt; mso-char-indent-count: 3.5; mso-para-margin-left: 2.0gd">int main()
{
printf( "/0hello world " );
system("pause");
return 0;
}
呵呵,什麼也沒有?是的。還記得字串是怎麼結束的嗎?/0 "/0hello world "是個常量數組。但是字串卻是“”。字元數組和字元長度是不一樣的,
這章儘管非常簡單,但是每個例子都經典之作,你能從代碼中學到文字中不能學到的東西。建議你每個都抄一遍,