📄 csdn_文档中心_新的c语言:c99中的整型 part 1.htm
字号:
style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">是从非类型语言</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'">中演化而来的,其本身在发展初期的短暂时期内也同样是非类型语言。非类型语言并非真的完全没有类型,更精确的说应该是它们只有一种类型</SPAN><SPAN
lang=EN-US>----</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">机器字(</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'">),但在其上的大多数操作都把它当作整型来对待。毫无疑问,在那些语言中的声明是不需要包含类型说明符的(</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'">),因为在那些语言中只有一种(但没有类型的名字)类型是有效的。当</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'">语言中的时候,被加入的仅仅是整型和字符。我不知道这样的行为是否是在认同</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'">语言的根基是非类型语言,或者整型在</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'">语言中的威望就由此而来,也或者这同时就是</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'">语言著名的简洁性的来由。无论如何,</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'">语言从其源头开始一直到</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'">定案前,整型就一直是语言的默认数据类型。如果你声明一个对象或者函数而没有指定其类型,亦或者通过调用隐式的声明了一个函数,这些默认的类型都将会是整型。</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'">之前,如果下面的代码属于一个翻译单元:</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'">的类型都将是整型,并且函数</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'">要求在任何时候如果运用到早期</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'">语言定义的属性</SPAN><SPAN
lang=EN-US>-----</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">一个类型采用整型作为其默认值的话,编译器都要给出一个诊断。给出诊断的好处是因为很多隐式整型都会带来错误而且这样的错误不易定位。就象下面这样的完整翻译单元:</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'">因为</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'">是隐式声明的,编译器将让其返回一个整型。当</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'">正确的存储其</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'">型返回值时,主函数</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>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>d</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">主函数将把这个值从整型转换成</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'">型。因为返回值本身就已经是</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'">型了(假设主函数</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'">返回结果的彻底混乱。</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>Ritchie</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">本人也提到他喜欢使用强制函数原型声明的编译器。</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'">并不要求编译器把隐式整型诊断看成错误而终止编译。一个聪明的编译器将仅仅把诊断看成是一个警告,并且根据程序员的选择看是给出一个错误的消息或者是完全忽略掉。</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>对该文的评论</FONT></TD>
<TD align=middle>
<SCRIPT src="CSDN_文档中心_新的C语言: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>我要评论</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('评论不能为空!!!!') ;
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> 评论人:xyj0323
评论:<BR> <TEXTAREA cols=104 name=Critique_Content rows=8></TEXTAREA><BR> <INPUT name=ubmit onclick=javascript:fubmitok(); type=button value=发表评论>
<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">网站简介</A> - <A
href="http://www.csdn.net/intro/intro.asp?id=5">广告服务</A> - <A
href="http://www.csdn.net/map/map.shtm">网站地图</A> - <A
href="http://www.csdn.net/help/help.asp">帮助信息</A> - <A
href="http://www.csdn.net/intro/intro.asp?id=2">联系方式</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语言:C99中的整型 Part 1.files/biaoshi.gif"
width=40></A></TD></TR>
<TR align=middle>
<TD vAlign=top>百联美达美公司 版权所有 京ICP证020026号</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><!--内容结束//--><!--结束//--></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -