资源描述:
《c语言编译预处理实验报告》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、C语言编译预处理实验报告实验名称:编译预处理一.实验目的(1)掌握文件包含、宏定义、条件编译、assert宏的使用。(2)练习带参数的宏定义、条件编译的使用。(3)练习assert宏的使用。(4)使用集成开发环境中的调试功能:单步执行,设置断点,观察变量。二、实验内容1.源程序改错:源程序:#include#defineSUMa+b#defineDIFa-b#defineSWAP(a,b)a=b,b=aintmain(){intb,t;printf("Inputtwointeger
2、sa,b:");scanf("%d,%d",&a,&b);printf("SUM=%dthedifferencebetweensquareofaandsquarebis:%d",SUM,SUM*DIF);SWAP(a,b);Printf("Nowa=%d,b=%d",a,b);return0;}.2.源程序修改替换源程序:voidmain(void){inta,b,c;floatd,e;printf("Enterthreeintegers:");scanf("%d,%d,%d",&a
3、,&b,&c);printf("themaximumofthemis%d",max(a,b,c));printf("Entertwofloatingpointnumbers:");scanf("%f,%f",&d,&e);printf("thesumofthemis%f",sum(d,e));return0;}intmax(intx,inty,intz){intt;if(x>y)t=x;elset=y;if(t4、intmain(void){floatr,s;ints_integer=0;printf("inputanumber:");scanf("%f",&r);#ifdefRs=3.14159*r*r;printf("areaofroundis:%f",s);s_integer=integer_fraction(s);printf("theintegerfractionofareais%d",s_integer);assert((s-s_integer)<1.0);#endifreturn0;}i
5、ntinteger_fraction(floatx){inti=x;returni;}4.程序设计三、实验步骤及结果步骤:1.编写源程序1.对源程序进行编译,修改错误,运行修改后的程序。2.发现结果有误,进行调试。3.发现逻辑错误,修改后运行得到正确结果。结果:1、第一次编译后的报错:第一次修改后的程序和结果:#include#defineSUMa+b#defineDIFa-b#defineSWAP(a,b)a=b,b=aintmain(){inta,b;printf("Input
6、twointegersa,b:");scanf("%d%d",&a,&b);printf("SUM=%dthedifferencebetweensquareofaandsquarebis:%d",SUM,SUM*DIF);SWAP(a,b);printf("Nowa=%d,b=%d",a,b);return0;}发现程序仍有错误,进行调试得:#include#defineSUM(a+b)#defineDIF(a-b)#defineSWAP(a,b)((t)=(a))
7、,((a)=(b)),((b)=(t))intmain(){inta,b,t=0;printf("Inputtwointegersa,b:");scanf("%d%d",&a,&b);printf("SUM=%dthedifferencebetweensquareofaandsquarebis:%d",SUM,SUM*DIF);SWAP(a,b);printf("Nowa=%d,b=%d",a,b);return0;}2、编译后的报错:修改后得到的正确程序和结果:#include
8、tdio.h>#definemax(a,b,c)((a)>(b)?(a>c?a:c):(b>c?b:c))floatsum(floata,floatb);intmain(void){inta,b,c;floatd,e;printf("Enterthreeintegers:");scanf("%d%d%d",&a,&b,&c);printf("themaximumofthemis%d",max(a,b,c));printf("Entertwofloatingpo