?? csdn_文檔中心_新的c語(yǔ)言:c99中的整型 part 1.htm
字號(hào):
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">是從非類型語(yǔ)言</SPAN><SPAN
lang=EN-US>BCPL</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">和</SPAN><SPAN
lang=EN-US>B</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">中演化而來(lái)的,其本身在發(fā)展初期的短暫時(shí)期內(nèi)也同樣是非類型語(yǔ)言。非類型語(yǔ)言并非真的完全沒有類型,更精確的說(shuō)應(yīng)該是它們只有一種類型</SPAN><SPAN
lang=EN-US>----</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">機(jī)器字(</SPAN><SPAN
lang=EN-US>machine word</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">),但在其上的大多數(shù)操作都把它當(dāng)作整型來(lái)對(duì)待。毫無(wú)疑問,在那些語(yǔ)言中的聲明是不需要包含類型說(shuō)明符的(</SPAN><SPAN
lang=EN-US>type specifier</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">),因?yàn)樵谀切┱Z(yǔ)言中只有一種(但沒有類型的名字)類型是有效的。當(dāng)</SPAN><SPAN
lang=EN-US>Dennis Ritchie</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">第一次把類型加到</SPAN><SPAN
lang=EN-US>C</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">語(yǔ)言中的時(shí)候,被加入的僅僅是整型和字符。我不知道這樣的行為是否是在認(rèn)同</SPAN><SPAN
lang=EN-US>C</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">語(yǔ)言的根基是非類型語(yǔ)言,或者整型在</SPAN><SPAN
lang=EN-US>C</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">語(yǔ)言中的威望就由此而來(lái),也或者這同時(shí)就是</SPAN><SPAN
lang=EN-US>C</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">語(yǔ)言著名的簡(jiǎn)潔性的來(lái)由。無(wú)論如何,</SPAN><SPAN
lang=EN-US>C</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">語(yǔ)言從其源頭開始一直到</SPAN><SPAN
lang=EN-US>C99</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">定案前,整型就一直是語(yǔ)言的默認(rèn)數(shù)據(jù)類型。如果你聲明一個(gè)對(duì)象或者函數(shù)而沒有指定其類型,亦或者通過調(diào)用隱式的聲明了一個(gè)函數(shù),這些默認(rèn)的類型都將會(huì)是整型。</SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US> <o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-tab-count: 1">
</SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">舉例子來(lái)說(shuō),在</SPAN><SPAN
lang=EN-US>C99</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">之前,如果下面的代碼屬于一個(gè)翻譯單元:</SPAN></P><PRE style="MARGIN: 0cm 36pt 0pt"><SPAN lang=EN-US>extern x;</SPAN></PRE><PRE style="MARGIN: 0cm 36pt 0pt"><SPAN lang=EN-US>f(y)</SPAN></PRE><PRE style="MARGIN: 0cm 36pt 0pt"><SPAN lang=EN-US>{</SPAN></PRE><PRE style="MARGIN: 0cm 36pt 0pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes"> </SPAN>register z = g(x) + y;</SPAN></PRE><PRE style="MARGIN: 0cm 36pt 0pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes"> </SPAN>return z;</SPAN></PRE><PRE style="MARGIN: 0cm 36pt 0pt"><SPAN lang=EN-US>}</SPAN></PRE>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">變量</SPAN><SPAN
lang=EN-US>X</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">,</SPAN><SPAN
lang=EN-US>Y</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">,</SPAN><SPAN
lang=EN-US>Z</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">的類型都將是整型,并且函數(shù)</SPAN><SPAN
lang=EN-US>f</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">和</SPAN><SPAN
lang=EN-US>g</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">也都返回整型。</SPAN><SPAN
lang=EN-US>C99</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">要求在任何時(shí)候如果運(yùn)用到早期</SPAN><SPAN
lang=EN-US>C</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">語(yǔ)言定義的屬性</SPAN><SPAN
lang=EN-US>-----</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">一個(gè)類型采用整型作為其默認(rèn)值的話,編譯器都要給出一個(gè)診斷。給出診斷的好處是因?yàn)楹芏嚯[式整型都會(huì)帶來(lái)錯(cuò)誤而且這樣的錯(cuò)誤不易定位。就象下面這樣的完整翻譯單元:</SPAN></P><PRE style="MARGIN: 0cm 36pt 0pt"><SPAN lang=EN-US>int main()</SPAN></PRE><PRE style="MARGIN: 0cm 36pt 0pt"><SPAN lang=EN-US>{</SPAN></PRE><PRE style="MARGIN: 0cm 36pt 0pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes"> </SPAN>double d;</SPAN></PRE><PRE style="MARGIN: 0cm 36pt 0pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes"> </SPAN>d = sqrt(2.0);</SPAN></PRE><PRE style="MARGIN: 0cm 36pt 0pt"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes"> </SPAN>return 0;</SPAN></PRE><PRE style="MARGIN: 0cm 36pt 0pt"><SPAN lang=EN-US>}</SPAN></PRE>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">因?yàn)?lt;/SPAN><SPAN
lang=EN-US>sqrt</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">是隱式聲明的,編譯器將讓其返回一個(gè)整型。當(dāng)</SPAN><SPAN
lang=EN-US>sqrt </SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">正確的存儲(chǔ)其</SPAN><SPAN
lang=EN-US>double</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">型返回值時(shí),主函數(shù)</SPAN><SPAN
lang=EN-US>main</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">并不需要知道返回值存儲(chǔ)在哪個(gè)位置(在一些機(jī)器上,整型變量和浮點(diǎn)變量使用不同的寄存器),當(dāng)主函數(shù)</SPAN><SPAN
lang=EN-US>main</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">加載這個(gè)返回值時(shí)(從可能錯(cuò)誤的地方),為了能把它賦給</SPAN><SPAN
lang=EN-US>d</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">主函數(shù)將把這個(gè)值從整型轉(zhuǎn)換成</SPAN><SPAN
lang=EN-US>double</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">型。因?yàn)榉祷刂当旧砭鸵呀?jīng)是</SPAN><SPAN
lang=EN-US>double</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">型了(假設(shè)主函數(shù)</SPAN><SPAN
lang=EN-US>main</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">正確的找到了它),這將造成</SPAN><SPAN
lang=EN-US>sqrt</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">返回結(jié)果的徹底混亂。</SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US> <o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-tab-count: 1">
</SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">很多種情況下都可能導(dǎo)致這種性質(zhì)的錯(cuò)誤。比如,你可能包含了錯(cuò)誤的頭文件并且遺漏了必須的函數(shù)聲明,或者在聲明一個(gè)外部變量的時(shí)候忘記使用類型說(shuō)明符了。隱式整型特性在帶來(lái)方便同時(shí)也將會(huì)帶來(lái)查找隱晦錯(cuò)誤的巨大困難。即使是</SPAN><SPAN
lang=EN-US>Ritchie</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">本人也提到他喜歡使用強(qiáng)制函數(shù)原型聲明的編譯器。</SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US> <o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-tab-count: 1">
</SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">注意,</SPAN><SPAN
lang=EN-US>C99</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">并不要求編譯器把隱式整型診斷看成錯(cuò)誤而終止編譯。一個(gè)聰明的編譯器將僅僅把診斷看成是一個(gè)警告,并且根據(jù)程序員的選擇看是給出一個(gè)錯(cuò)誤的消息或者是完全忽略掉。</SPAN></P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'"></SPAN> </P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">-完-</SPAN></P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'"></SPAN> </P>
<P class=MsoNormal><SPAN
style="FONT-FAMILY: 宋體; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">歡迎訪問:<A
href="http://nishuo.35123.net/">http://nishuo.35123.net/</A></SPAN></P><BR></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR>
<TABLE align=center bgColor=#006699 border=0 cellPadding=0 cellSpacing=0
width=770>
<TBODY>
<TR bgColor=#006699>
<TD align=middle bgColor=#006699 id=white><FONT
color=#ffffff>對(duì)該文的評(píng)論</FONT></TD>
<TD align=middle>
<SCRIPT src="CSDN_文檔中心_新的C語(yǔ)言:C99中的整型 Part 1.files/readnum.htm"></SCRIPT>
</TD></TR></TBODY></TABLE><BR>
<DIV align=center>
<TABLE align=center bgColor=#cccccc border=0 cellPadding=2 cellSpacing=1
width=770>
<TBODY>
<TR>
<TH bgColor=#006699 id=white><FONT
color=#ffffff>我要評(píng)論</FONT></TH></TR></TBODY></TABLE></DIV>
<SCRIPT language=javascript>
<!--
function isEmpty(s)
{
return ((s == null) || (s.length == 0))
}
function fubmitok()
{
if (isEmpty(document.add_critique.Critique_Content.value))
{
alert('評(píng)論不能為空!!!!') ;
return false;
}
document.add_critique.submit();
}
//-->
</SCRIPT>
<DIV align=center>
<TABLE border=0 width=770>
<TBODY>
<TR>
<TD>
<FORM action=Critique_Sql.asp method=post name=add_critique><INPUT
name=Critique_State type=hidden value=add> 評(píng)論人:xyj0323
評(píng)論:<BR> <TEXTAREA cols=104 name=Critique_Content rows=8></TEXTAREA><BR> <INPUT name=ubmit onclick=javascript:fubmitok(); type=button value=發(fā)表評(píng)論>
<INPUT name=Topic_id type=hidden value=21954> <INPUT name=From type=hidden
value=/Develop/Build_Article.asp?id=21954>
</FORM></TD></TR></TBODY></TABLE></DIV><BR>
<HR noShade SIZE=1 width=770>
<TABLE border=0 cellPadding=0 cellSpacing=0 width=500>
<TBODY>
<TR align=middle>
<TD height=10 vAlign=bottom><A
href="http://www.csdn.net/intro/intro.asp?id=2">網(wǎng)站簡(jiǎn)介</A> - <A
href="http://www.csdn.net/intro/intro.asp?id=5">廣告服務(wù)</A> - <A
href="http://www.csdn.net/map/map.shtm">網(wǎng)站地圖</A> - <A
href="http://www.csdn.net/help/help.asp">幫助信息</A> - <A
href="http://www.csdn.net/intro/intro.asp?id=2">聯(lián)系方式</A> - <A
href="http://www.csdn.net/english">English</A> </TD>
<TD align=middle rowSpan=3><A
href="http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001032100010"><IMG
border=0 height=48 src="CSDN_文檔中心_新的C語(yǔ)言:C99中的整型 Part 1.files/biaoshi.gif"
width=40></A></TD></TR>
<TR align=middle>
<TD vAlign=top>百聯(lián)美達(dá)美公司 版權(quán)所有 京ICP證020026號(hào)</TD></TR>
<TR align=middle>
<TD vAlign=top><FONT face=Verdana>Copyright © CSDN.net, Inc. All rights
reserved</FONT></TD></TR>
<TR>
<TD height=15></TD>
<TD></TD></TR></TBODY></TABLE></DIV>
<DIV></DIV><!--內(nèi)容結(jié)束//--><!--結(jié)束//--></BODY></HTML>
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -