📄 c语言常见错误小结.htm
字号:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>中国PHP联盟 - 喜悦国际村 - C语言常见错误小结</title>
<meta http-equiv="Content-type" content="text/html; charset=gb2312">
<meta name="keywords" content="中国PHP联盟 , php programming ,php tutorials ,php faq ,php howto ,php examples,php source code ,php news ,php resource ,php templates , php classes ,smarty php ,php mysql ,php scripts ,php hosting ,using php ,php manual ,zend ,php forum ,php reference ,php content management , php hosting ,php encoder , 域名注册 , 虚拟主机 .">
<meta name="description" content="中国PHP联盟 , php programming ,php tutorials ,php faq ,php howto ,php examples,php source code ,php news ,php resource ,php templates , php classes ,smarty php ,php mysql ,php scripts ,php hosting ,using php ,php manual ,zend ,php forum ,php reference ,php content management , php hosting ,php encoder , 域名注册 , 虚拟主机 .">
<STYLE type=text/css>
BODY { background:url(/pic/bg.gif) repeat-y center;; FONT-FAMILY:宋体; FONT-SIZE:12px }
A:link { COLOR: navy; TEXT-DECORATION: none}
A:visited { COLOR: #336699; TEXT-DECORATION: none }
A:hover { COLOR: #cc0000; TEXT-DECORATION: underline }
td { FONT-FAMILY:宋体; FONT-SIZE: 12px }
</STYLE>
</head>
<body bgcolor="#E6E6FA">
<script src="/js/box.js"></script>
<center>
<!--top-->
<div style="width:800px; clear:left; display:block;">
<div style="float:left;"><img src="/pic/logos.gif" style="float:left"></div>
<div style="float:right;"><script type="text/javascript"><!--google_ad_client = "pub-6064700219302133";google_ad_width = 468;google_ad_height = 60;google_ad_format = "468x60_as";google_color_border = "F1F1F1";google_color_bg = "DFDFDF";google_color_link = "0033FF";google_color_url = "000000";google_color_text = "000000";//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script></div>
<div style="margin-top:18px;float:left;"><script>makenav(1);</script></div>
</div>
<!--end of top-->
<!--top ad-->
<div style=" margin-top:0px; width:800px;clear:left; display:block;">
<div style="margin-top:5px;margin-left:5px;float:left;width:800px;"><p align=left>当前位置--> <a href='index.php'>首 页</a> --> <a href='list.php'>文 章</a> --><a href='cml05.html'>C/C++</a></p></div>
</div>
<!--end of top ad-->
<div style=" margin-top:0px; width:800px;clear:left; display:block;">
<!--left-->
<div style=" float:left;width:798px;">
<!--article-->
<div style="margin-top:0px;float:left">
<table border="0" cellspacing="0" width=100% cellpadding="1" style="border-top: #b7c9e6 1px solid;border-left: #b7c9e6 1px solid;border-right: #b7c9e6 1px solid;">
<tr><td width="27" height="20" bgcolor="#87CEEB" align="center">
<img border="0" src="/pic/arrow.gif" width="14" height="14"></td>
<td width="100%" bgcolor="#87CEEB" valign="middle"><b>※阅读文章※</b></td>
</tr>
</table>
<table border="0" width="798" cellspacing="0" cellpadding="2" bgcolor="#F5FEE7" style="border-right: #b7c9e6 1px solid; border-left: #b7c9e6 1px solid;border-bottom: #b7c9e6 1px solid;">
<tr><td>
<h2> C语言常见错误小结</h2><br>
作者:<b>天津</b> [文章出自: www.fanqiang.com]<br>
<!--right-->
<!--right ad-->
<script type="text/javascript"><!--google_ad_client = "pub-6064700219302133";google_ad_width = 728;google_ad_height = 90;google_ad_format = "728x90_as";google_color_border = "F1F1F1";google_color_bg = "DFDFDF";google_color_link = "0033FF";google_color_url = "000000";google_color_text = "000000";//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<!--end of right ad-->
<!--end right-->
<br>
<!--HTML_BEGIN-->
<table width="744" border="0" cellspacing="0" cellpadding="0" height="76">
<tr>
<td>
<p>C语言的最大特点是:功能强、使用方便灵活。C编译的程序对语法检查并不象其它高级语言那么严格,这就给编程人员留下“灵活的余地”,但还是由于这个灵活给程序的调试带来了许多不便,尤其对初学C语言的人来说,经常会出一些连自己都不知道错在哪里的错误。看着有错的程序,不知该如何改起,本人通过对C的学习,积累了一些C编程时常犯的错误,写给各位学员以供参考。<br>
1.书写标识符时,忽略了大小写字母的区别。<br>
main()<br>
{<br>
int a=5;<br>
printf("%d",A);<br>
}<br>
编译程序把a和A认为是两个不同的变量名,而显示出错信息。C认为大写字母和小写字母是两个不同的字符。习惯上,符号常量名用大写,变量名用小写表示,以增加可读性。<br>
2.忽略了变量的类型,进行了不合法的运算。<br>
main()<br>
{<br>
float a,b;<br>
printf("%d",a%b);<br>
}<br>
%是求余运算,得到a/b的整余数。整型变量a和b可以进行求余运算,而实型变量则不允许进行“求余”运算。<br>
3.将字符常量与字符串常量混淆。<br>
char c;<br>
c="a";<br>
在这里就混淆了字符常量与字符串常量,字符常量是由一对单引号括起来的单个字符,字符串常量是一对双引号括起来的字符序列。C规定以“\”作字符串结束标志,它是由系统自动加上的,所以字符串“a”实际上包含两个字符:‘a'和‘\',而把它赋给一个字符变量是不行的。<br>
4.忽略了“=”与“==”的区别。<br>
在许多高级语言中,用“=”符号作为关系运算符“等于”。如在BASIC程序中可以写<br>
if (a=3) then …<br>
但C语言中,“=”是赋值运算符,“==”是关系运算符。如:<br>
if (a==3) a=b;<br>
前者是进行比较,a是否和3相等,后者表示如果a和3相等,把b值赋给a。由于习惯问题,初学者往往会犯这样的错误。<br>
5.忘记加分号。<br>
分号是C语句中不可缺少的一部分,语句末尾必须有分号。<br>
a=1<br>
b=2<br>
编译时,编译程序在“a=1”后面没发现分号,就把下一行“b=2”也作为上一行语句的一部分,这就会出现语法错误。改错时,有时在被指出有错的一行中未发现错误,就需要看一下上一行是否漏掉了分号。<br>
{ z=x+y;<br>
t=z/100;<br>
printf("%f",t);<br>
}<br>
对于复合语句来说,最后一个语句中最后的分号不能忽略不写(这是和PASCAL不同的)。<br>
6.多加分号。<br>
对于一个复合语句,如:<br>
{ z=x+y;<br>
t=z/100;<br>
printf("%f",t);<br>
};<br>
复合语句的花括号后不应再加分号,否则将会画蛇添足。<br>
又如:<br>
if (a%3==0);<br>
I++;<br>
本是如果3整除a,则I加1。但由于if (a%3==0)后多加了分号,则if语句到此结束,程序将执行I++语句,不论3是否整除a,I都将自动加1。<br>
再如:<br>
for (I=0;I<5;I++);<br>
{scanf("%d",&x);<br>
printf("%d",x);}<br>
本意是先后输入5个数,每输入一个数后再将它输出。由于for()后多加了一个分号,使循环体变为空语句,此时只能输入一个数并输出它。<br>
7.输入变量时忘记加地址运算符“&”。<br>
int a,b;<br>
scanf("%d%d",a,b);<br>
这是不合法的。Scanf函数的作用是:按照a、b在内存的地址将a、b的值存进去。“&a”指a在内存中的地址。<br>
8.输入数据的方式与要求不符。<br>
①scanf("%d%d",&a,&b);<br>
输入时,不能用逗号作两个数据间的分隔符,如下面输入不合法:<br>
3,4
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -