一、单项选择题
1. 在C语言中,下列语句中正确的是( )。
t a, b = 3, c = 4; c; b,
解析C语言中,定义变量时可以同时初始化,也可以先定义再初始化。选项C中,a、c都被定义并初始化,符合语法规则。
2. 下列程序段的输出结果是( )。
t = 0;
while (i<= 10) { += i;
i++;
}tf);
B. 55
C. 50
D. 10
解析程序中使用while循环计算1~10的和,终输出结果为55。
3. 以下程序的输出结果是( )。
if (a >b && a >c)tf("a is the largest.");
else if (b >a && b >c)tf("b is the largest.");
elsetf("c is the largest.");
. a is the largest.
B. b is the largest.
C. c is the largest.
D. 编译错误。
解析程序中使用if语句判断a、c中的值,并输出结果。由于c的值,因此输出结果为“c is the largest”。
二、填空题
1. 下列程序的输出结果是________。
tf("%d,%d,%d", a++, ++b, c += 2);
答案2, 3, 5
tf函数输出a、c的值,其中a使用后自增操作符,b使用前自增操作符,c使用复合赋值运算符。因此输出结果为2, 3, 5。
2. 下列程序的输出结果是________。
for (i = 0; i< 5; i++) {
if (i == 2)tinue;tf("%d", i);
答案0134
tinue语句跳过本次循环。因此输出结果为0134。
三、编程题
之间的所有素数。
clude
tain() {t, i, j, flag;tfterumber ");f);tfeumbersd);; i++) {
flag = 1;
for (j = 2; j< i; j++) {
if (i % j == 0) {
flag = 0;
break;
}
}
if (flag == 1)tf("%d, ", i);
} 0;
之间的整数,第二个for循环用于判断当前数是否为素数。如果当前数可以被除了1和自身之外的数整除,则不是素数,否则是素数。终输出所有素数的值。
以上是,希望对C语言爱好者有所帮助。