新编 C 语言程序设计教程练习一参考答案
练习一
1.1 写出最简单的 C 程序。 答:
void main( ) { }
1.2 用一条 C 语句完成下述要求。
a)把变量 x,y,z 和 result 定义为 int 类型。 b)提示用户输入三个整数。
c)从键盘读取三个整数并把它们存储在变量 x,y,z 中。 d)计算变量 x,y 和 z
所存储的整数的和,并把计算结果赋给变量 result.
e)打印出“The sum is ”并紧接着打印出变量 result 的值。
答:
a)int x, y, z, result;
b)printf(〞请输入三个整数\n〞); c)scanf(〞%d%d%d〞, &x, d)result =
x + y + z;
e)printf(〞The sum is %d〞, result);
1.3 用练习 1.2 中的语句写出计算三个整数和的完整的程序。
答:
void main( ) {
int x, y, z, result;
printf(〞请输入三个整数\n〞); scanf(〞%d%d%d〞, &x, result = x + y
+ z;
printf(〞The sum is %d〞, result); }
1.4 指出并改正下列语句中的错误(每条语句可能不止一个错误)。
a)scanf(〞%d,value〞);
b)printf(〞The sum of %d and %d is %d〞\n,x,y); c)*/Program to determine
the largest of three in integers/*
d)printf(〞The value you entered is %d〞\n, e)int return=10; 答:
a)scanf(〞%d〞,
b)printf(〞The sum of %d and %d is %d\n〞, x, y, x + y);