实验3
(1) 程序
#include <stdio.h>
int main()
{
int a=3,b=5,c=7,x=1,y,z;
a=b=c;
x+2=5;
z=y+3;
return 0;
}
程序错误;提示语言
--------------------Configuration: fd - Win32 Debug--------------------
fd.cpp
C:\Documents and Settings\Administrator\桌面\fd.cpp(2) : error C2447: missing function header (old-style formal list?)
执行 cl.exe 时出错.
fd.exe - 1 error(s), 0 warning(s)
修改为:
#include <stdio.h>
int main()
{
int a=3,b=5,c=7,x=1,y,z;
a=b=c;
x=5+3;
z=y+3;
return 0;
}
分析结果:在赋值和运算中是从右到左。
分析:
A 变量名可以是数字,英文字母(大小写均可)。下划线。
B大小写可以区分是成不同文件。
C 赋值运算的特点是自右向左的。
实验4
(1) 整数除的危险性
#include <stdio.h>
int main()
{
int a=5,b=7,c=100,d,e,f;
d=a/b*c;
e=a*c/d;
f=c/b*a;
}
程序在运行结果为:0 0 0
分析原因:
A  5/7*100,结果是5/7等于0,再0乘上100等于0
B  5*100/7,结果是5*100等于500,再500/7等于0
C  100/7*5  结果是100/7等于0,再0*5等于0
结论:原因在于,当整除不成立时,结果为0,所以结论会影响下一次的运行。
(2)
#include <stdio.h>
main()
{
int a=5,b=8;
printf(“a++=%d”,a++);
printf(“a=%d”,a);
printf(“++b=%d”,++b);
printf(“b=%d”,b);
}
得到结论为
分析结果:i++“先引用,后增值”:++i“先增值,后引用”
所以 a++5,因为先引用,a6,因为是增值的结果,++b9,是因为先增值,b9,是因为后引用的结果。
(3)对这些表达式进行测试分析。
b+a+++a
b+(a++)+a
b+a+(++a)
b+a+++a++
编程:
#include <stdio.h>
main()
{
int a=1,b=1;
printf(“b+a+++a=%d”, b+a+++a);
}
结果:
#include <stdio.h>
main()
{
int a=1,b=1;
printf(“b+(a++)+a=%d”, b+(a++)+a);
}
结果:
#include <stdio.h>
main()
{
int a=1,b=1;
printf(“b+a+(++a)=%d”, b+a+(++a));
}
结果:
#include <stdio.h>
main()
{
int a=1,b=1;
printf(“b+a+++a++=%d”, b+a+++a++);
}
结果:
#include <stdio.h>
main()
{
int a=1,b=;
printf函数中大小写d通用吗
printf(“b+a+++a=%d”, b+a+++a);
}
(4) 对这些表达式进行测试分析。
i,j
i+1,j+1
i++,j++
++i,++j
i+++++j
编程:
#include <stdio.h>
main()
{
int i=1,j=1;
printf(“i=%d”,i);
printf(“j=%d”,j);
}
结果:
#include <stdio.h>
main()
{
int i=1,j=1;
printf(“i+1=%d”,i+1);
printf(“j+1=%d”,j+1);
}
结果:
#include <stdio.h>
main()
{
int i=1,j=1;
printf(“i++=%d”,i++);
printf(“j++=%d”,j++);
}
结果:
#include <stdio.h>
main()
{
int i=1,j=1;
printf(“++i=%d”,++i);
printf(“++j=%d”,++j);
}
结果:
#include <stdio.h>
main()
{
int i=1,j=1;
printf(“i+++++j=%d”,i+++++j);
}
结果:--------------------Configuration: Cpp1 - Win32 Debug--------------------
Cpp1.cpp
C:\Documents and Settings\Administrator\桌面\Cpp1.cpp(5) : error C2105: '++' needs l-value
C:\Documents and Settings\Administrator\桌面\Cpp1.cpp(6) : warning C4508: 'main' : function should return a value; 'void' return type assumed
执行 cl.exe 时出错.
- 1 error(s), 0 warning(s)
结论:程序是错误的。
1.分析结果:
整除有危险性。
A.因为小数不能除大的数字,会显示0。还有结果一定是整数。
B.算数运算的方向是自左向右。
2.分析结果:
Ai++“先引用,后增值”:++i“先增值,后引用”
所以 a++5,因为先引用,a6,因为是增值的结果,
B++b9,是因为先增值,b9,是因为后引用的结果。
3.分析结果:
A.可靠性低
B.不容易读懂
实验 5
printf (“long”,sizeof(long));
编程:
#include <stdio.h>