C 语言 编译预处理 实验报告
实验名称:编译预处理
一 . 实验目的
(1) 掌握文件包含、宏定义、条件编译、 assert 宏的使用。 (2)练习带参数的宏定义、条件编译的使用。
( 3)练习 assert 宏的使用。 (4)使用集成开发环境中的调试功能:单步执行,设置断点,观察变量。
二、实验内容
源程序改错:
源程序: #include #define SUM a+b #define DIF a-b #define SWAP(a,b) a=b, b=a
int main()
{
int b, t;
printf("Input two integers a,b:"); scanf("%d, %d", &a, &b);
square bprintf("\nSUM=%d\n the difference between square of a and is:%d",SUM,SUM*DIF);
square b
SWAP(a,b);
Printf("\nNow a=%d,b=%d\n", a, b); return 0;
}.
源程序修改替换
源程序 : void main(void)
{
int a, b, c;
float d, e;
printf("Enter three integers:"); scanf("%d, %d, %d", &a, &b, &c);
printf("\nthe maximum of them is %d\n", max(a,b,c)); printf("Enter two floating point numbers:");
scanf("%f, %f", &d, &e); printf("\nthe sum of them is%f\n",sum(d,e));
return 0;
int max(int x, int y, int z)
{
int t; if (x > y) t = x;
else
t = y;
if (t < z)
t = z; return t;
}
跟踪调试程序
程序代码:
#define R
int main(void)
{
float r, s; int s_integer = 0; printf("input a number:");
scanf("%f", &r);
#ifdef R s = 3.14159 * r * r; printf("area of round is:%f\n", s); s_integer = integer_fraction(s); printf("the integer fraction of area is %d\n", s_integer); assert((s-s_integer) < 1.0);
#endif
return 0;
}
int integer_fraction(float x)
{
int i = x; return i; }
程序设计
三、实验步骤及结果
步骤:1.编写源程序
对源程序进行编译,修改错误,运行修改后的程序
3?发现结果有误,进行调试。
C: \Us-e r a \ zucke B.C:\Users\zucke _
C: \Us-e r a \ zucke B.
C:\Users\zucke _ .. -10
C: \Users\zucke10
C: \Userjs\zueke. - -10
C:\xuak9.?■12
C:??■ 13
C■匚 \Us&rs Vxucrks. . , g
C: \口三亡匸三、蛊口uJce-?-15
In function Tsnai!!ii :
errori " ar undeclared first 口勻皀 ±el tfciis function)
eezqez (Each undeclared idenier ±s reported only once error s f or each func&ioxi i€ appeals ia.)
^4 rniEg r l#f b-hdnd open nd cjf oojzuti# exprejs xgh h?> no *£fecfa rarni^g:t d#fl^rdof functicj: pPriistfr
irarning; :yjnj#來iritr
errorz ez^eexed Ldeni;if±er or ,r (" before 1 . 1 token
= Build finished: 4 errors r 3 warnings =
第一次修改后的程序和结果:
#i nclude
#defi ne SUM a+b
#defi ne D