复习题
1.C语言的基本模块是什么?
函数
2.什么是语法错误?写出一个英语例子和C语言例子。
英语例子:I are a student.
C语言例子:printf(hello);
3.什么是语义错误?写出一个英语例子和C语言例子。
英语例子:Dog enjoy eating love.
C语言例子:printf(“num square = %d”, n* n* n);
4.Indiana Sloth编写了下面的程序,并征求你的意见。请帮助他评定。
include studio.h
int main{void} /* 该程序打印一年有多少周 /*
(
int s
s := 56;
printf(There are s weeks in a year.);
return 0;
#include<studio.h>
int main()
主函数缺少大括号
int s = 52;一年有52周
printf(“There are %d weeks in a year”, s)
5.假设下面的4个例子都是完整程序中的一部分,它们都输出什么结果?
a. printf("Baa Baa Black Sheep.");
printf("Have you any wool?\n");
b. printf("Begone!\nO creature of lard!\n");
c. printf("What?\nNo/nfish?\n");
d. int num;
num = 2;
printf("%d + %d = %d", num, num, num + num);
a:Baa Baa Black Sheep.Have you any wool?
b:Begone!
O creature of lard!
c:What?
No/nfish?
d:2 + 2 = 4
6.在main、int、function、char、=中,哪些是C语言的关键字?
关键字是int和char(main是一个函数名;function是函数的意思;=是一个运算符)。
7.如何以下面的格式输出变量words和lines的值(3020和350代表两个变量的值)?
There were 3020 words and 350 lines.
printf(“There were %d words and %d lines.\n”, words, lines);
8.考虑下面的程序,在执行第7、8、9行后程序的状态分别是什么?
#include <stdio.h>
int main(void)
{
int a, b;
a = 5;
b = 2; /* 第7行 */
b = a; /* 第8行 */
a = b; /* 第9行 */
printf("%d %d\n", b, a);
return 0;
}
第七行:a = 5,b = 2
第八行:a = 5,b = 5
第九行:a = 5,b = 5
9.考虑下面的程序,在执行第7、8、9行后程序的状态分别是什么?
#include <stdio.h>
int main(void)
{
int x, y;
x = 10;
y = 5; /* 第7行 */
y = x + y; /*第8行*/
x = x*y; /*第9行*/
printf("%d %d\n", x, y);
return 0;
}
第七行:x = 10,y = 5
第八行:x = 10,y = 15
第九行:x = 150,y = 15
编程练习
1.编写一个程序,调用一次printf()函数,把你的名和姓打印在一行。再调用一次printf()函数,把你的名和姓分别打印在两行。然后,再调用两次printf()函数,把你的名和姓打印在一行。输出应如下所示(当然要把示例的内容换成你的名字):
Gustav Mahler ←第1次打印的内容
Gustav ←第2次打印的内容
Mahler ←仍是第2次打印的内容
Gustav Mahler ←第3次和第4次打印的内容
int main(){
printf("liu mengyuan\n");
printf("liu\nmengyuan\n");
printf("liu ");
printf("mengyuan");
}
2.编写一个程序,打印你的姓名和地址。
int main(){
printf("Name:liu mengyuan\n");
printf("Address:Northeastern University");
}
3.编写一个程序把你的年龄转换成天数,并显示这两个值。这里不用考虑闰年的问题。
int main(){
int years, days;
printf("Enter your age ");
scanf("%d", &years);
days = years * 365;
printf("The number of days for your age transition is %d:", days);
}
4.编写一个程序,生成以下输出:
For he's a jolly good fellow!
For he's a jolly good fellow!
For he's a jolly good fellow!
Which nobody can deny!
除了main()函数以外,该程序还要调用两个自定义函数:一个名为jolly(),用于打印前3条消息,调用一次打印一条;另一个函数名为deny(),打印最后一条消息。
#include<stdio.h>
void jolly();
void deny();
int main(){
for(int i = 0;i < 3;i++){
jolly();
}
deny();
}
void jolly(){
printf("For he's a jolly good fellow!\n");
}
void deny(){
printf("Which nobody can deny!\n");
}
5.编写一个程序,生成以下输出:
Brazil, Russia, India, China
India, China
Brazil, Russia
除了main()以外,该程序还要调用两个自定义函数:一个名为br(),调用一次打印一次“Brazil, Russia”;另一个名为ic(),调用一次打印一次“India, China”。其他内容在main()函数中完成。
#include<stdio.h>
void br();
void ic();
int main(){
br();
printf(", ");
ic();
printf("\n");
ic();
printf("\n");
br();
}
void br(){
printf("Brazil, Russia");
}
void ic(){
printf("India, China");
}
6.编写一个程序,创建一个整型变量toes,并将toes设置为10。程序中还要计算toes的两倍和toes的平方。该程序应打印3个值,并分别描述以示区分。
#include<stdio.h>
#include<math.h>
int main(){
int toes = 10;
printf("%d\n", toes);
printf("%d\n", toes * 2);
printf("%d\n", (int)pow(toes, 2));
}
7.许多研究表明,微笑益处多多。编写一个程序,生成以下格式的输出:
Smile!Smile!Smile!
Smile!Smile!
Smile!
该程序要定义一个函数,该函数被调用一次打印一次“Smile!”,根据程序的需要使用该函数。
#include<stdio.h>
void smile();
int main(){
int times;
for(int i = 3;i > 0;i--){
times = i;
while(times){
smile();
times--;
}
printf("\n");
}
}
void smile(){
printf("Smile!");
}
8.在C语言中,函数可以调用另一个函数。编写一个程序,调用一个名为one_three()的函数。该函数在一行打印单词“one”,再调用第2个函数two(),然后在另一行打印单词“three”。two()函数在一行显示单词“two”。main()函数在调用one_three()函数前要打印短语“starting now:”,并在调用完毕后显示短语“done!”。因此,该程序的输出应如下所示:
starting now:
one
two
three
done!
#include<stdio.h>
void one_three();
void two();
int main(){
printf("starting now\n");
one_three();
printf("done!");
}
void one_three(){
printf("one\n");
two();
printf("three\n");
}
void two(){
printf("two\n");
}