C语言的几道题
来源:互联网 发布:辽艺配音知乎 编辑:程序博客网 时间:2024/05/29 03:59
这是考察C语言的几道题,原文出处:http://stevenkobes.com/ctest.html,若想看所有的题,请访问此页
1. Consider the following program:
#include <stdio.h>
#include <setjmp.h>
static jmp_buf buf;
int main(void)
{
volatile int b = 3;
if (setjmp(buf) != 0)
{
printf("%d/n", b);
exit(0);
}
b = 5;
longjmp(buf, 1);
}
What is the output of this program?
(a) 3
(b) 5
(c) 0
(d) none of the above
题解:此题也就是程序上设置断点,在setjmp处,顺序执行到setjmp(buf)!=0时判断失败,执行b=5,到longjmp时又返回到setjmp处,执行输出,当然是输出5了
4. Consider the following program:
#include <stdio.h>
int main(void)
{
int a[5] = { 1, 2, 3, 4, 5 };
int *ptr = (int*)(&a + 1);
printf("%d %d/n", *(a + 1), *(ptr - 1));
return 0;
}
What is the output of this program?
(a) 2 2
(b) 2 1
(c) 2 5
(d) none of the above
5. Consider the following program:
#include <stdio.h>
void foo(int[][3]);
int main(void)
{
int a[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
foo(a);
printf("%d/n", a[2][1]);
return 0;
}
void foo(int b[][3])
{
++b;
b[1][1] = 9;
}
What is the output of this program?
(a) 8
(b) 9
(c) 7
(d) none of the above
题解:此题考察的是二维数组的指针表示:对于二维数组来说,a[i][j]表示的是数组a的第i行第j列的数组元素,等价的指针表示为:*(*(a+i)+j). a[i]表求数组a的第i行第0列的数组元素的地址,等价表示是*(a+i), 但另外一个需要注意的地方是a+i和*(a+i)的指是相同的,a+i虽然是指向a[i][0]的,但是*(a+i)并不是指向a[i][0],它是表示数组a的行的首地址。这样也就有一个有趣的现象:a+i=*(a+i); *(a+i)!=**(a+i), 呵呵,这一点一定要注意!
6. Consider the following program:
#include <stdio.h>
int main(void)
{
int a, b, c, d;
a = 3;
b = 5;
c = a, b;
d = (a, b);
printf("c=%d ", c);
printf("d=%d/n", d);
return 0;
}
What is the output of this program?
(a) c=3 d=3
(b) c=5 d=3
(c) c=3 d=5
(d) c=5 d=5
题解:逗号操作符对两边的操作数起作用,其返回的值是右边的操作数;c=a,b:因为逗号的优先级小于等号,所以c=a,而b和c=a语句是相当的;而(a,b)返回的是b的值,d=(a,b)也就是d=b了
7. Consider the following program:
#include <stdio.h>
int main(void)
{
int a[][3] = {1, 2, 3, 4, 5, 6};
int (*ptr)[3] = a;
printf("%d %d ", (*ptr)[1], (*ptr)[2]);
++ptr;
printf("%d %d/n", (*ptr)[1], (*ptr)[2]);
return 0;
}
What is the output of this program?
(a) 2 3 5 6
(b) 2 3 4 5
(c) 4 5 0 0
(d) none of the above
题解:此题考察的是指向数组的指针,其int (*ptr)[3]指定义了一个指针ptr,其指向一个int型的三维数组,开始ptr的值为数组a的首地址,自增后,其指向{4,5,6},注意:指针数组与指向数组的指针不要弄混淆了,指针数组是这样定义的:int *p[5];其中p[0],p[1],p[2],p[3],p[4]都是指向int型的指针
8. Consider the following code segment:
#include <stdlib.h>
int *f1(void)
{
int x = 10;
return &x;
}
int *f2(void)
{
int *ptr;
*ptr = 10;
return ptr;
}
int *f3(void)
{
int *ptr;
ptr = malloc(sizeof *ptr);
return ptr;
}
Which of these functions uses pointers incorrectly?
(a) f3 only
(b) f1 and f3
(c) f1 and f2
(d) f1, f2, and f3
题解:f1返回临时变量的指针地址,一旦执行完毕,临时对象被销毁,该指针也就失去了意义;f2中ptr并未被初始化,谁知道ptr指向到哪里;
9. Consider the following program:
#include <stdio.h>
int main(void)
{
int i = 3;
int j;
j = sizeof(++i + ++i);
printf("i=%d j=%d/n", i, j);
return 0;
}
What is the output of this program on an implementation where int occupies 2 bytes?
(a) i=4 j=2
(b) i=3 j=2
(c) i=5 j=2
(d) the behavior is undefined
题解: ++i,+i如果是用在sizeof外面,那么结果i就变为5了,可惜,它用在了sizeof里面了,i不变,标准答案给出j=2,但是我在G++下面编译,得出j=4, 不知这是什么原因。
12. Consider the following code segment:
typedef int (*test)(float*, float*);
test tmp;
What is the type of tmp?
(a) function taking two pointer-to-float arguments and returning pointer to int
(b) pointer to int
(c) pointer to function taking two pointer-to-float arguments and returning int
(d) none of the above
题解:定义了一个指针,其指向一个拥有两个float指针类型参数的函数
15. Consider the following program:
#include <stdarg.h>
#include <stdio.h>
int ripple(int n, ...)
{
int i, j, k;
va_list p;
k = 0;
j = 1;
va_start(p, n);
for (; j < n; ++j)
{
i = va_arg(p, int);
for (; i; i &= i - 1)
++k;
}
return k;
}
int main(void)
{
printf("%d/n", ripple(3, 5, 7));
return 0;
}
What is the output of this program?
(a) 7
(b) 6
(c) 5
(d) 3
题解:此题有点难,参考http://baike.baidu.com/view/1340814.htm
做了一上午,发现很多东西,尤其是指针,还是不是很熟练,学了几年了,看来还是不精通啊,还是要努力学。。。
- C语言的几道题
- 关于C语言的几道题
- C语言,美丽的语言
- 与C语言“无关”的C语言
- 与C语言“无关”的C语言
- 初学C语言 C语言的认识
- c语言003---c语言的基本概念
- C语言:C语言指针的概念
- 【C语言】C语言里面的冒号
- C语言:C语言的一些基本概念
- C语言-C的世界观
- C语言的问题
- C语言的学习方法
- 伟大的C语言
- c 语言的 static
- C语言的数组
- C语言的指针
- C语言的指针
- CSS基础:DIVcss初学者需要引起重视的10个问题与技巧
- okgo浏览器0.3.0版放出
- PKU ACM 1080-human gene function
- 这种形式,不能将 this 传递吗?
- e.preventdefault实例说明
- C语言的几道题
- 游标使用技巧
- 解决不同浏览器上中文文件名的下载乱码问题php验证可行的测试过ie ff 和chrome的
- MFC edit control 用法
- Oracle数据库10个小问题
- 开始学习shell
- Qt 文件处理
- 开源协议(OSS License)详解
- java 中 Collection, List, Map, Set的总结