1.建立并运行由Kernighan和Ritchie编写的著名的“hello world”程序:
#include <stdio.h>
main()
{
printf("hello ,world\n");
}
在编译时是否有警告信息?如果有,需要如何进行修改呢?
解答:在VC6中,无警告信息。
2.思考下面的程序:
#include <stdio.h>
main()
{
printf("Parkinson's Law:\nWork expands so as to ");
printf("fill the time\n");
printf("available for its completion.\n");
return 0;
}
(a)请指出程序中的指令和语句。
解答:
#include <stdio.h> //这是指令
printf("Parkinson's Law:\nWork expands so as to "); //这是语句
printf("fill the time\n"); //这是语句
printf("available for its completion.\n"); //这是语句
return 0; //这是语句
(b)程序的输出是什么?
解答:输出为:
Parkinson's Law:
Work expands so as to fill the time
available for its completion.
Press any key to continue
3.编写一个程序,程序要使用printf在屏幕上显示出下面的图形:
*
*
*
* *
* *
*
解答:程序如下:
#include <stdio.h>
main()
{
printf(" *\n");
printf(" *\n");
printf(" *\n");
printf(" * *\n");
printf(" * *\n");
printf(" *\n");
return 0;
}
4.通过下列方法编写程序dweight.c:(1)用初始化语句替换对变量height、length、width的赋值语句;(2)去年变量weight,在最后的printf语句中计算(volume + 165) / 166
解答:
#include <stdio.h>
main()
{
int height = 8,length = 12, width = 10,volume;
volume = height * length * width;
printf("Dimensions:%d * %d * %d\n",length,width,height);
printf("Volume (cubic inches): %d\n",volume);
printf("Dimensional weight (pounds): %d\n",(volume + 165) / 166);
return 0;
}
程序输出结果:
Dimensions:12 * 10 * 8
Volume (cubic inches): 960
Dimensional weight (pounds): 6
Press any key to continue
.
5.编写一个计算球体体积的程序,其中球体半径为10m,参考公式v= 4/3πr³。注意,分数4/3应写成4.0/3.0。(如果分数写成4/3,会产生什么结果?)
解答:程序如下:
#include <stdio.h>
main()
{
int r = 10;
float v;
v = 4.0/3.0 *3.14159 * r * r * r;
printf("v = %f\n",v);
return 0;
}
程序输出:
v = 4188.786667
Press any key to continue
6.编写一个程序用来声明向个int型和float型变量,不对这些变量初始化,然后打印他们的值。这些数值是否有规律?(通常情况下没有。)
解答:程序如下:
#include <stdio.h>
main()
{
int a,b,c;
float i,j,k;
printf("%d %d %d %f %f %f\n",a,b,c,i,j,k);
return 0;
}
在vc6里程序输出:
-858993460 -858993460 -858993460 -107374176.000000 -107374176.000000 -107374176.000000
Press any key to continue
7.修改练习5中的程序,使用户可以自行录入球体的半径。
解答:程序如下:
#include <stdio.h>
main()
{
int r;
float v;
printf("Please input data:");
scanf("%d",&r);
v = 4.0/3.0 *3.14159 * r * r * r;
printf("v = %f\n",v);
return 0;
}
输出如下:
Please input data:10
v = 4188.786667
Press any key to continue
8.编写一个程序,要求用户输入一个美金数量,然后显示出加了5%税率的相应金额。格式如下所示:
Enter a dollar amount:100.00
with tax added:105.00
解答:程序如下:
#include <stdio.h>
main()
{
float num;
printf("Enter a dollar amount:");
scanf("%f",&num);
printf("with tax added:%f\n",num * 1.05);
return 0;
}
程序输出如下:
Enter a dollar amount:100
with tax added:105.000000
9.修改练习7,要求用名为PI的宏表示π的值。
解答:程序如下:
#include <stdio.h>
#define PI 3.14159
main()
{
int r;
float v;
printf("Please input data:");
scanf("%d",&r);
v = 4.0/3.0 * PI * r * r * r;
printf("v = %f\n",v);
return 0;
}
程序输出如下:
Please input data:10
v = 4188.786667
10.判断下列哪些是不合法的C语言标识符?
(a)100_bottles
(b)_100_bottles
(c)one_hundred_bottles
(d)bottles_by_the_hundred
解答:(a)不合法。
11.判断下列哪些是C语言的关键字?
(a) for
(b) If
(c) main
(d) printf
(e) while
解答:除了(b)不是,其他均是C语言关键字。
12.下面的语句中有多少记号?
a=(3*q-p*p)/3;
解答:共有14个记号。
13.在练习12的记号之间插入足够多的空格,使得上面的语句易于阅读。
解答:如下
a = ( 3 * q - p * p ) / 3;