?? 教學--第四章 數據類型.htm
字號:
<TABLE height=100 width="55%" border=1>
<TBODY>
<TR>
<TD width="58%" height=19>二進制值(1字節)</TD>
<TD width="42%" height=19>十進制值</TD></TR>
<TR>
<TD width="58%" height=19><FONT color=#ff0000>1</FONT>000 0000</TD>
<TD width="42%" height=19>-128</TD></TR>
<TR>
<TD width="58%" height=19><FONT color=#ff0000>1</FONT>000 0001</TD>
<TD width="42%" height=19>-127</TD></TR>
<TR>
<TD width="58%" height=19><FONT color=#ff0000>1</FONT>000 0010</TD>
<TD width="42%" height=19>-126</TD></TR>
<TR>
<TD width="58%" height=19><FONT color=#ff0000>1</FONT>000 0011</TD>
<TD width="42%" height=19>-125</TD></TR>
<TR>
<TD width="58%" height=19>...</TD>
<TD width="42%" height=19>...</TD></TR>
<TR>
<TD width="58%" height=19><FONT color=#ff0000>1</FONT>111 1110</TD>
<TD width="42%" height=19>-2</TD></TR>
<TR>
<TD width="58%" height=19><FONT color=#ff0000>1</FONT>111 1111</TD>
<TD width="42%" height=19>-1</TD></TR></TBODY></TABLE>
<P> </P>
<P>首先我們看到,從-1到-128,其二進制的最高位都是1(表中標為紅色),正如我們前面的學。</P>
<P>然后我們有些奇怪地發現,<FONT color=#ff0000>1</FONT>000 0000 并沒有拿來表示 -0;而<FONT
color=#ff0000>1</FONT>000 0001也不是拿來直觀地表示-1。事實上,-1 用<FONT
color=#ff0000>1</FONT>111 1111來表示。</P>
<P>怎么理解這個問題呢?先得問一句是-1大還是-128大?</P>
<P>當然是 -1 大。-1是最大的負整數。以此對應,計算機中無論是字符類型,或者是整數類型,也無論這個整數是幾個字節。它都用全1來表示
-1。比如一個字節的數值中:<FONT color=#ff0000>1</FONT>111 1111表示-1,那么,<FONT
color=#ff0000>1</FONT>111 1111 - 1 是什么呢?和現實中的計算結果完全一致。<FONT
color=#ff0000>1</FONT>111 1111 - 1 = <FONT color=#ff0000>1</FONT>111
1110,而<FONT color=#ff0000>1</FONT>111
1110就是-2。這樣一直減下去,當減到只剩最高位用于表示符號的1以外,其它低位全為0時,就是最小的負值了,在一字節中,最小的負值是<FONT
color=#ff0000>1</FONT>000 0000,也就是-128。</P>
<P>我們以-1為例,來看看不同字節數的整數中,如何表達-1這個數:</P>
<TABLE width="100%" border=1>
<TBODY>
<TR>
<TD width="16%">字節數</TD>
<TD width="50%">二進制值</TD>
<TD width="34%">十進制值</TD></TR>
<TR>
<TD width="16%">單字節數</TD>
<TD width="50%"><FONT color=#ff0000>1</FONT>111 1111</TD>
<TD width="34%">-1</TD></TR>
<TR>
<TD width="16%">雙字節數</TD>
<TD width="50%"><FONT color=#ff0000>1</FONT>111 1111 1111 1111</TD>
<TD width="34%">-1</TD></TR>
<TR>
<TD width="16%">四字節數</TD>
<TD width="50%"><FONT color=#ff0000>1</FONT>111 1111 1111 1111 1111
1111 1111 1111</TD>
<TD width="34%">-1</TD></TR></TBODY></TABLE>
<P> </P>
<P>可能有同學這時會混了:為什么 1111 1111
有時表示255,有時又表示-1?所以我再強調一下本節前面所說的第2點:你自已決定一個數是有符號還是無符號的。寫程序時,指定一個量是有符號的,那么當這個量的二進制各位上都是1時,它表示的數就是-1;相反,如果事選聲明這個量是無符號的,此時它表示的就是該量允許的最大值,對于一個字節的數來說,最大值就是255。</P>
<P>這一節課,看似羅嗦,但我希望每位沒有編程基礎,或者以前對進制,對負值、補碼、反碼等概念,對數據類型理解不透徹的學員,都能多花時間反復閱讀,直到看懂得文中的每一張圖表的意思為止。如果有困難,請發信到問答處的專門信箱:<A
href="mailto:wenda@bcbschool.com">wenda@bcbschool.com</A>
(為了方便我的查閱,請無關課程的問答或其它來信,不要發到這個郵址,謝謝)。</P>
<P> </P>
<H3><A name=4.3>4.3</A> 字符集和保留字</H3>
<H4><A name=4.3.1>4.3.1</A> 字符集</H4>
<P> </P>
<P>字符集和保留并不專屬于“數據類型”的基礎知識。它是一門語言最基礎的東西。就像字母A-Z對于英語的作用一樣。我把它放到這里,更多的是因為這是我們第一次要碰到它,在下一節,馬上就要用了。幸好,
它的難度和學會26個字母差多少。</P>
<P>每種語言都使用-組字符來構造有意義的語句,組成C++程序的,最終的是以下這些字符(空格這個字符不好表示,就直接寫上“空格”兩字了,以后同):</P>
<P>26個字母的大小寫形式:ABCDEFGHIJKLMNOPQRSTUVWXYZ,abcdefghijklmnopqrst;</P>
<P>10個阿拉伯數字:0123456789;</P>
<P>其它符號:+ - * / = , . _ : ; ? \ " ' ~ | ! # % & ( ) [ ] { } ^
< > (空格)</P>
<P>其它符號,包括漢字,則可能作為程序中字符串的內容,用于顯示等。</P>
<P>最后,C/C++語言是區分大小的語言,也就是說ABC和abc并不相同。這一點我們將在下一章特別強調。</P>
<P> </P>
<H4><A name=4.3.2>4.3.2</A> 保留字</H4>
<P>保留字也稱關鍵字。它是預先定義好的標識符,這些標識符必須保留給C++語言自身專用。因為它們用來在編譯過程中表示特殊的含義。比如,我們想定義一個量為整數類型,那么C++就必須有一個詞來表示什么是整數類型,這個詞就是一個關鍵字。</P>
<P>C,C++主要的關鍵字,我們在章末附表列出。下面先列出本章要用的關鍵字。</P>
<P>char :字符類型</P>
<P>int : 整型</P>
<P>float :單精度實型(浮點型)</P>
<P>double : 雙精度實型</P>
<P>unsigned : 無符號類型</P>
<P>signed : 有符號類型</P>
<P> </P>
<P>bool : 布爾類型</P>
<P>true : 布爾類型的真值</P>
<P>false : 布爾類型的假值</P>
<P>void : 無類型</P>
<P> </P>
<P>sizeof : 取得指定類型的所占用的范圍</P>
<P>typedef : 為某種類型取一別名</P>
<H3><A name=4.4>4.4</A> 基本數據類型</H3>
<P>前面講的一些有關數據類型的各種基本概念,下面是數據類型這一課真正開始的時候。如果在其中你有什么概念不能理解,最好的方法就是首先回頭看本章前面的內容。</P>
<H4><A name=4.4.1>4.4.1</A> 字符型和各種整型、實型</H4>
<P> </P>
<TABLE height=273 width="100%" border=1>
<TBODY>
<TR>
<TD width="22%" height=19>類型標識符</TD>
<TD width="19%" height=19>類型說明</TD>
<TD width="10%" height=19>長度<BR> (字節)</TD>
<TD width="29%" height=19>范圍</TD>
<TD width="20%" height=19>備注</TD></TR>
<TR>
<TD width="22%" height=18><B>char</B></TD>
<TD width="19%" height=18>字符型</TD>
<TD width="10%" height=18>1</TD>
<TD width="29%" height=18>-128 ~ 127</TD>
<TD width="20%" height=18>-2<SUP>7</SUP> ~ (2<SUP>7</SUP> -1)</TD></TR>
<TR>
<TD width="22%" height=19><B>unsigned char</B></TD>
<TD width="19%" height=19>無符字符型</TD>
<TD width="10%" height=19>1</TD>
<TD width="29%" height=19>0 ~ 255</TD>
<TD width="20%" height=19>0 ~ (2<SUP>8</SUP> -1)</TD></TR>
<TR>
<TD width="22%" height=18><B>short int</B></TD>
<TD width="19%" height=18>短整型</TD>
<TD width="10%" height=18>2</TD>
<TD width="29%" height=18>-32768 ~ 32767</TD>
<TD width="20%" height=18>2<SUP>-15</SUP> ~ (2<SUP>15</SUP> -
1)</TD></TR>
<TR>
<TD width="22%" height=19><B>unsigned short int</B></TD>
<TD width="19%" height=19>無符短整型</TD>
<TD width="10%" height=19>2</TD>
<TD width="29%" height=19>0 ~ 65535</TD>
<TD width="20%" height=19>0 ~ (2<SUP>16</SUP> - 1)</TD></TR>
<TR>
<TD width="22%" height=19><B>int</B></TD>
<TD width="19%" height=19>整型</TD>
<TD width="10%" height=19>4</TD>
<TD width="29%" height=19>-2147483648 ~ 2147483647</TD>
<TD width="20%" height=19>-2<SUP>31</SUP> ~ (2<SUP>31</SUP> -
1)</TD></TR>
<TR>
<TD width="22%" height=19><B>unsigned int</B></TD>
<TD width="19%" height=19>無符整型</TD>
<TD width="10%" height=19>4</TD>
<TD width="29%" height=19>0 ~ 4294967295</TD>
<TD width="20%" height=19>0 ~ (2<SUP>32</SUP>-1)</TD></TR>
<TR>
<TD width="22%" height=19><B>float</B></TD>
<TD width="19%" height=19>實型(單精度)</TD>
<TD width="10%" height=19>4</TD>
<TD width="29%" height=19>1.18*10<SUP>-38</SUP> ~
3.40*10<SUP>38</SUP> </TD>
<TD width="20%" height=19>7位有效位</TD></TR>
<TR>
<TD width="22%" height=19><B>double</B></TD>
<TD width="19%" height=19>實型(雙精度)</TD>
<TD width="10%" height=19>8</TD>
<TD width="29%" height=19>2.23*10<SUP>-308 </SUP>~
1.79*10<SUP>308</SUP> </TD>
<TD width="20%" height=19>15位有效位</TD></TR>
<TR>
<TD width="22%" height=19><B>long double</B></TD>
<TD width="19%" height=19>實型(長雙精度)</TD>
<TD width="10%" height=19>10</TD>
<TD width="29%" height=19>3.37*10<SUP>-4932</SUP> ~
1.18*10<SUP>4932</SUP> </TD>
<TD width="20%" height=19>19位有效位</TD></TR></TBODY></TABLE>
<P> </P>
<P>unsigned 用于修飾 int 和 char 類型。它使int 或 char 類型成為無符號類型。</P>
<P>signed 是 unsigned 反義詞,如 signed int
表示有符號類型,不過signed可以省略,所以上面列出char,short int,int 都是有符號類型。</P>
<P> </P>
<P>有 short int (短整型) ,所以也就有對應 long int (長整型)。long int
用于表示4個字節(32位)的整數。但是在我們現在普通使用的32位計算機中,int 默認就是4個字節,所以long也是可以省略的。</P>
<P>(較早幾年,也就是Windows 3.1/DOS 流行的時候,那時的機器及操作系統都是16位的,這種情況下,int
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -