复习题
1.C语言的基本模块是什么?
答:函数
2.什么是语法错误?写出一个英语例子和C语言例子
答:语法错误违反了组成语句或程序的规则
英语例子:Me speak english good
c语言例子:printf("where are rhe parentherer?");
3.什么是语义错误?写出一个英语例子和C语言例子
答:语义错误是指含义错误
英文例子:this is sentence is excellent Czech
4.Indiana Sloth编写了下面的程序,并征求你的意见。请帮助他评定。
include studio.h
int main{void} /* 该程序打印一年有多少周 /*
(
int s
s:= 56;
print(There are s weeks in a year.);
return 0;
}
答: 修改后的程序
#include <stdio.h> int main (void)/* 注释部分*/ { int s; s = 52; printf("There are %d weeks in a year.\n", s); return 0; }
5.假设下面的4个例子都是完整程序中的一部分,它们都输出什么结
果?
a.printf("Baba Baa Black Sheeo.");
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输出Baba Baa Black Sheeo.
b输出Begone!
O creature of lard!
c输出 what? No/nfish?
d输出2+2=4
6.在main、int、function、char、=中,哪些是C语言的关键字?
答:int main
7.如何以下面的格式输出变量words和lines的值(这里,3020和350代表
两个变量的值)?
There were 3020 words and 350 lines.
答:printf("There were %d words and %d lines.\n", words, lines);
8.考虑下面的程序:
#include <stdio.h>
{
int a,b;
a= 5;
b = 2; /* 第7行 */
b = a; /* 第8行 */
a = b; /* 第9行 */
printf("%d%d\n",b,a);
return 0;
}
请问,在执行完第7、第8、第9行后,程序的状态分别是什么?
答:a=5,b=2,b=5 a=b=2
9.
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; } 请问,在执行完第7、第8、第9行后,程序的状态分别是什么?
x=10 b=5 x=10 y=15 x=150 y=15
编程题
1.编写一个程序,调用一次 printf()函数,把你的姓名打印在一行。再调
用一次 printf()函数,把你的姓名分别打印在两行。然后,再调用两次printf()
函数,把你的姓名打印在一行。输出应如下所示(当然要把示例的内容换成
你的姓名):
#include <stdio.h>
int main (void) { printf("gustav mahler \n"); printf("gustav\n"); printf("mahler \n"); printf("gustav "); printf("mahler \n"); return 0; }
2.编写一个程序,打印你的姓名和地址。
#include <stdio.h> int main (void) { printf("My name is Trumple. My family lives in the White House.\n"); return 0; }
3.编写一个程序把你的年龄转换成天数,并显示这两个值。这里不用考
虑闰年的问题。
#include <stdio.h> int main (void) { printf("Age:21,days:%d\n",21*365); return 0; }
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); void demy(void); int main (void) { jolly(); demy(); return 0; } void jolly(void) { printf("For he's a jolly good fellow\n"); printf("For he's a jolly good fellow\n"); printf("For he's a jolly good fellow\n"); } void demy(void) { printf("Which nobody can deny!\n"); }
7.许多研究表明,微笑益处多多。编写一个程序,生成以下格式的输
出:
Smile!Smile!Smile!
Smile!Smile!
Smile!
该程序要定义一个函数,该函数被调用一次打印一次“Smile!”,根据程
序的需要使用该函数。
#include <stdio.h> void smile(void); int main (void) { smile(); return 0; } void smile(void) { int i, j; for(i = 0; i < 3; i++) { for(j = 3; j>i; j--) { printf("Smile!"); } printf("\n"); } }