C语言程序设计答案(黄保和编)第7章

更新时间:2023-04-22 12:47:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

C语言程序设计答案,答案不唯一,仅供参考

一、选择题

1、下列叙述中错误的是C)预处理命令必须位于源文件的开始处

2、C语言编译系统对宏替换的处理是在A)源程序编译前进行的

3、在宏定义“#define PI 3.14159”中,3.14159是C)字符串

4、设有宏定义“#define A B abcd”,则宏替换时,A)宏名A用B abcd替换

5、下列有关宏的叙述中错误的是A)宏名必须使用大写英文字母

6、下列宏定义中,最不会引起二义性的是D)#define ADD(a,b) ((a)+(b))

7、设有宏定义“#define M 3+2”,则表达式2*M*3的值为B)12

8、设有宏定义“#define Y(n) (4*n)”,则表达式3+Y(5+1)的值为B)24

9、设有宏定义

#define N 3

#define M N+2

则表达式2*M/N的值为A)6

10、在#include命令中,#include后面的文件名用双引号定界,则系统寻找被包含文件的方

式是D)先在源程序所在文件夹查找,查找失败后再到C系统的Include文件夹中查找

11、设有以下A、B两个程序段,则说法正确的是B)两个程序的目标代码不同,但运行结

果相同

//A程序段 //B程序段

#define N 3 #define N 3

{ {

if(N>0) #if(N>0)

{ printf(“N=%d\n”,N);

printf(“N=%d\n”,N); #else

} printf(“N<0”);

else #endif

printf(“N<0”); }

}

12、执行以下程序,结果为A)DEBUG OK

#define DEBUG

void main()

{

#ifdef DEBUG

printf(“DEBUG”);

#endif

printf(OK);

}

二、编程题

1、 编写一个计算圆的程序,将pi值定义为符号常量

#include <stdio.h>

#define pi 3.14

void main()

{

float r;

第1页/共2页 void main() void main()

C语言程序设计答案,答案不唯一,仅供参考

printf("输入圆的半径:"); scanf("%f", &r); printf("圆的面积为%f\n", pi*r*r);

}

2、 将求圆柱体的体积写成带参数的宏定义,并使用该宏定义计算圆柱体的体积 #include <stdio.h>

#define volumn(r,h) 3.14*r*r*h

void main(){

float r, h; printf("输入圆柱底面积半径:"); scanf("%f", &r); printf("输入圆柱的高:"); scanf("%f", &h); printf("圆柱的体积为:%f", volumn(r,h));

}

3、 定义一个带有3个参数的宏MAX,求3个参数中的最大值

#include <stdio.h>

#define MAX(a,b,c) a > b ? (a > c ? a : c) : (b > c ? b : c)

void main()

{

float a, b, c; printf("输入三个数:"); scanf("%f%f%f", &a, &b, &c); printf("最大值为%f", MAX(a,b,c));

}

4、 给年份YEAR定义一个宏,判定该年份是否是闰年

#include <stdio.h>

#define YEAR(a) (a % 4 == 0 && a % 100 != 0) || (a % 400 == 0)

void main()

{

} int year; printf("输入年份:"); scanf("%d", &year); if(YEAR(year)) { printf("%d是闰年\n", year); } else { } printf("%d不是闰年\n", year);

第2页/共2页

本文来源:https://www.bwwdw.com/article/2n2q.html

Top