- 写出执行完下列各行后quack的值是多少?后5行中使用的是第1行quack的值。
int quack = 2;
quack += 5; // 7
quack *= 10; // 20
quack -= 6; // -4
quack /= 8; // 0
quack %= 3; // 3
- 假设 value 是int类型,下面循环的输出是什么?
for (value 36; value > 0; value /=2)
printf("%3d",value);
如果value是double类型,会出现什么问题?
36 18 9 4 2 1
%3d的说明符不能适用double类型,所以不正确
- 用代码表示以下测试条件
a. x大于5 // x>5
b. scanf() 读取一个名为x的double类型值且失败 // scanf("%lf",&x)!=1
c. x的值等于5 // x==5
- 用代码表示以下测试条件
a. scanf() 成功读入一个整数 // scanf("%d",&x)==1
b. x不等于5 // x!=5
c. x大于或等于20 // x>=20
- 下面的程序有点问题,请找出问题所在。
#include <stdio.h>
int main(void)
{
int i,j,list(10);
for (i=1,i<=10,i++)
{
list[i] = 2 * i + 3;
for (j = 1,j>=i,j++)
printf(" %d",list[j]);
printf("\n");
}
修改后的程序为:
#include <stdio.h>
int main(void)
{
int i,j,list[10];
for(i = 0;i<=9;i++)
{
list[i] = 2 * i + 3;
for(j = 1;j<=i;j++)
printf(" %d",list[j]);
printf("\n");
}
return 0;
}
- 编写一个程序打印下面的图案,要求使用嵌套循环:
$$$$$$$$
$$$$$$$$
$$$$$$$$
$$$$$$$$
代码如下:
#include <stdio.h>
int main(void)
{
int i,j;
for(i = 0;i<=4;i++){
for(j=0;j<=8;j++)
printf("$");
printf("\n");
}
return 0;
}
- 下面的程序各打印什么内容?
a.
#include <stdio.h>
int main(void)
{
int i = 0;
while (++i < 4)
printf("Hi! ");
do
printf("Bye! ");
while (i++ < 8)
return 0;
}
// 打印结果为:3次(1,2,3) Hi! Hi! Hi!
// 打印结果为:5次(4,5,6,7,8) Bye! Bye! Bye! Bye! Bye! do while是先打印,再判断循环,
b.
#include <stdio.h>
int main(void)
{
int i;
char ch;
for (i = 0,ch='A',i<4;i++,ch +=2 * i)
printf("%c",ch);
return 0;
}
// 打印结果为:ACGM
- 假设用户输入的是 Go west,young man!,下面各程序的输出是什么?(在ASCII码中,! 紧跟在空格字符后面)
a.
#include <stdio.h>
int main(void)
{
char ch;
scanf("%c",&c);
while (ch!='g')
{
printf("%c",ch)
scanf("%c",&ch);
}
return 0;
}
// 打印输出为:Go West,youn
b.
#include <stdio.h>
int main(void)
{
char ch;
scanf("%c",&ch);
while (ch!='g')
{
pritnf("%c",++ch);
scanf("%c",&ch);
}
return 0;
}
// 打印输出为:Hp!xftu-!zpvo
c.
#include <stdio.h>
int main(void)
{
char ch;
do{
pritnf("%c",++ch);
scanf("%c",&ch);
}while (ch!='g')
return 0;
}
// 打印结果为:Go west,young
d.
#include <stdio.h>
int main(void)
{
char ch;
scanf("%c",&ch);
for(ch = '$';ch!='g';scanf("%c",&ch))
printf("%c",ch);
return 0;
}
// 打印结果为:$o west,youn
- 下面的程序打印什么内容?
#include <stdio.h>
int main(void)
{
int n, m;
n = 30;
while (++n <= 33)
printf("%d|", n);
// 打印结果为:31|32|33|34
n = 30;
do
printf("%d|", n);
while (++n <= 33);
// 打印结果为:30|31|32|33|
printf("\n***\n"); // 打印 ***
for (n = 1; n*n < 200; n += 4)
printf("%d\n", n);
// 打印结果为:1 5 9 13 (各自占一行)
printf("\n***\n"); //打印 ***
for (n = 2, m = 6; n < m; n *= 2, m += 2)
printf("%d %d\n", n, m);
/*打印结果为:
2 6
4 8
8 10
*/
printf("\n***\n"); // 打印 ***
for (n = 5; n > 0; n--)
{
for (m = 0; m <= n; m++)
printf("=");
printf("\n");
}
return 0;
/*打印结果为:
======
=====
====
===
==
*/
}
- 考虑下面的声明
double mint[10];
a. 数组名是什么? // 答案:mint
b. 该数组有多少个元素? // 答案:10
c. 每个元素可以储存什么类型的值? // 答案:double
d. 下面的哪一个scanf() 的用法正确? // 答案:ii
i. scanf("%lf",mint[2]) //
ii. scanf("%lf",&mint[2])
iii. scanf("%lf",&mint) // 可以改为 scanf("lf",&mint[0])时即为正确
- Noah先生喜欢以2计数,所以编写了下面的程序,创建了一个储存2、4、6、8等数字的数组。整个程序是否有错误之处?如果有,请指出。
#include <stdio.h>
#define SIZE 8
int main(void)
{
int by_twos[SIZE];
int index;
for (index = 1;index <= SIZE;index++)
by_twos[index] = 2 * index;
for(index = 1;index <=SIZE;index++)
prinf("%d",by_twos);
printf("\n");
return 0;
}
完全修改并调试后的程序为:
#include <stdio.h>
#define SIZE 8
int main(void)
{
int by_twos[SIZE];
int index;
for (index = 0;index < SIZE;index++)
{ by_twos[index] = 2 * (index+1);
//for(index = 0;index < SIZE;index++)
printf("%d ",by_twos[index]);
}
printf("\n");
return 0;
}
- 假设要编写一个返回long类型值的函数,函数定义中应包含什么?
返回值的类型设置为 long类型,return语句返回一个long类型值
- 定义一个函数,接受一个int类型的参数,并以long类型返回参数的平方值。
long square(int n)
{
return n * n;
}
- 下面的程序打印什么内容?
#include <stdio.h>
int main(void)
{
int k;
for(k = 1,printf("%d: Hi! \n",k);printf("k = %d \n",k),k*k <26;
k+=2,printf("Now k is %d \n",k))
printf("k is %d in the loop\n",k);
return 0;
}
输出结果为:
1: Hi!
k = 1
k is 1 in the loop
Now k is 3
k = 3
k is 3 in the loop
Now k is 5
k = 5
k is 5 in the loop
Now k is 7
k = 7