I come, I see, I conquer

                    —Gaius Julius Caesar

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

编者按:

非常基本的C语言问题, 一个信息类(计算机,资讯工程,电子工程, 通信工程)专业的本科毕业生应该达到的水平。题目不难,全部都能快速地答完, 当然也需要一定的知识储备。对于大多数人, 我们预期你可能答错 3)   4)  15)题, 所以答错3道以内的, 我们认为你很棒。答错5道题以内, 我们认为你还不错(你还可能答错第9题)。如果你有6道以上的题目不能答对,基本上我们都不好说什么了....

约定:

1) 下面的测试题中, 认为所有必须的头文件都已经正确的包含了
2) 数据类型    
   char 一个字节 1 byte
   int 两个字节 2 byte (16位系统, 认为整型是2个字节)
   long int 四个字节 4 byte
   float  四个字节4 byet
   double 八个字节 8 byte
   long double 十个字节 10 byte
   pointer 两个字节 2 byte(注意, 16位系统, 地址总线只有16位)


第1题: 考查对volatile关键字的认识

#include<setjmp.h>

static jmp_buf buf;

main()    
{
    
volatile  int b;
    b 
=3;
    
    
if(setjmp(buf)!=0)
    {
        printf(
"%d ", b); 
        exit(
0);
    }
    b
=5;
    longjmp(buf , 
1);

 

请问, 这段程序的输出是
(a) 3
(b) 5
(c) 0
(d) 以上均不是

 

第2题:考查递归调用

int  foo ( int x , int  n) 
{
    
int val;
    val 
=1;
    
    
if (n>0
    {
        
if (n%2 == 1)  val = val *x;
        val 
= val * foo(x*x , n/2);
    }
    
return val;
}

 

这段代码对x和n完成什么样的功能(操作)?
(a) x^n (x的n次幂)
(b) x*n(x与n的乘积)
(c) n^x(n的x次幂)
(d) 以上均不是

 

第3题:考查形式参数, 实际参数, 指针和数组

void f1(int *int); 
void f2(int *int); 
void(*p[2]) (int *int);

main()
{
    
int a;
    
int b;
    
    p[
0= f1;
    p[
1= f2;
    a
=3;
    b
=5;
    
    p[
0](&a, b);
    printf(
"%d\t %d\t", a, b);

    p[
1](&a, b);
    printf(
"%d\t %d\t", a, b);
}

void f1(int* p , int q)
{
    
int tmp;
    tmp 
=*p;
    
*= q;
    q
= tmp;
}

void f2(int* p , int q)
{
    
int tmp;
    tmp 
=*p;
    
*= q;
    q
= tmp;
}  

 

这段程序的输出是:
(a) 5 5 5 5
(b) 3 5 3 5
(c) 5 3 5 3
(d) 3 3 3 3


第4题:此题考查的是C的变长参数,就像标准函数库里printf()那样,这个话题一般国内大学课堂是不会讲到的,不会情有可原

#include <stdarg.h>
int ripple ( int , );

main()
{
    
int num;
    num 
= ripple ( 35,7);
    printf( 
" %d" , num);
}

int ripple (int n, )
{
    
int i , j;
    
int 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;
}

 

这段程序的输出是:

(a) 7
(b) 6
(c) 5
(d) 3

 

详细参考答案

第1题:   (b)
volatile字面意思是易于挥发的。这个关键字来描述一个变量时, 意味着 给该变量赋值(写入)之后, 马上再读取, 写入的值与读取的值可能不一样,所以说它"容易挥发"的。
这是因为这个变量可能一个寄存器, 直接与外部设备相连, 你写入之后, 该寄存器也有可能被外部设备的写操作所改变;或者, 该变量被一个中断程序, 或另一个进程
改变了.
volatile 不会被编译器优化影响, 在longjump 后,它的值 是后面假定的变量值,b最后的值是5,所以5被打印出来.

setjmp : 设置非局部跳转 /* setjmp.h*/

Stores context information such as register values so that the lomgjmp function can return control to the statement following the one calling setjmp.Returns 0 when it is initially called.

Lonjjmp: 执行一个非局部跳转 /* setjmp.h*/

Transfers control to the statement where the call to setjmp (which initialized buf) was made. Execution continues at this point as if longjmp cannot return the value 0.A nonvolatile automatic variable might be changed by a call to longjmp.When you use setjmp and longjmp, the only automatic variables guaranteed to remain valid are those declared volatile.

Note: Test program without volatile qualifier (result may very)

更详细介绍, 请参阅 C语言的setjmp和longjmp


第2题:  (a)
此题目较难.

这个程序的非递归版本

int  what ( int x , int  n)
{
    
int val;
    
int product;
    product 
=1;
    val 
=x;
    
    
while(n>0)
    {
        
if (n%2 == 1
        product 
= product*val;   /*如果是奇数次幂,  x(val)要先乘上一次,;   
                                         偶数次幂, 最后返回时才会到这里乘以1
*/
        val 
= val* val;                  
        n 
= n/2
    }
    
return product;
}

 

/* 用二元复乘策略 */
算法描述
(while n>0) 
{
    if  next most significant binary digit of  n( power)  is one
    then multiply accumulated product by current val,
    reduce n(power)  sequence by a factor of two using integer division.
    get next val by multiply current value of itself                  
}

 

第3题:  (a)
很显然选a.
f1交换*p 和 q的值, f1执行完后, *p 和 q的值的确交换了,  但q的改变不会影响到b的改变,  *p 实际上就是a
所以执行f1后,  a=b=5
这道题考查的知识范围很广,包括typedef自定义类型,函数指针,指针数组void(*p[ 2 ]) ( int *, int);
定义了一个函数指针的数组p,p有两个指针元素. 元素是函数的指针, 函数指针指向的函数是一个带2个参数,返回void的函数, 所带的两个参数是指向整型的指针, 和整型
p[ 0 ] = f1; p[ 1 ] = f2 contain address of function .function name without parenthesis represent address of function Value and address of variable is passed to function only argument that is effected is a (address is passed). Because of call by value f1, f2 can not effect b

 

第4题:  (c)

在C编译器通常提供了一系列处理可变参数的宏, 以屏蔽不同的硬件平台造成的差异, 增加程序的可移植性。这些宏包括va_start、va_arg和va_end等。
采用ANSI标准形式时, 参数个数可变的函数的原型声明是:type funcname(type para1, type para2, ...)
这种形式至少需要一个普通的形式参数, 后面的省略号不表示省略, 而是函数原型的一部分。type是函数返回值和形式参数的类型。
不同的编译器, 对这个可变长参数的实现不一样 , gcc4.x中是内置函数.
关于可变长参数,可参阅
http://www.upsdn.net/html/2004-11/26.html
http://www.upsdn.net/html/2004-11/24.html

程序分析

va_list p;  /*定义一个变量 ,保存  函数参数列表 的指针*/
va_start( p , n);     
/*用va_start宏 初始化变量p, va_start宏的第2个参数n, 是一个固定的参数, 必须是我们自己定义的
                        变长函数的最后一个入栈的参数也就是调用的时候参数列表里的第1个参数
*/
for (; j<n;  ++j)     /* j从1开始, 遍历所有可变参数 */
{
    i 
=  va_arg( p , int);      /*va_arg取出当前的参数,并认为取出的参数是一个整数(int)  */
    
for (; i;    i &=i-1  )     /*判断取出的i是否为0*/
      
++k;                      /* 如果i不为0, k自加, i与i-1进行与逻辑运算, 直到i 为0
                                   这是一个技巧, 下面会谈到它的功能
*/
}

 

当我们调用ripple函数时, 传递给ripple函数的 参数列表的第一个参数n的值是3. va_start 初始化p士气指向第一个未命名的参数(n是有名字的参数) ,也就是 is 5 (第一个). 每次对 va_arg的调用, 都将返回一个参数, 并且把 p 指向下一个参数. va_arg 用一个类型名来决定返回的参数是何种类型,以及在 var_arg的内部实现中决定移动多大的距离才到达下一个参数
(; i; i&=i-1) k++        /* 计算i有多少bit被置1 */

5用二进制表示是 (101) 2
7用二进制表示 (111) 3
所以 k 返回 5(2+3),也即本题应该选c

举个例子, 就很好理解了
令  i= 9 = 1001
    i-1  = 1000       
    (i-1) +1 = i
    1000
      +1
    1001
因为i与i-1的最右边的那位(最低位) 肯定是不同, 如果i1,i-1肯定是0, 反之亦然. i & i-1 这个运算, 在二相补的数字系统中, 将会消除最右边的1位

posted on 2008-09-17 18:56  jcsu  阅读(2983)  评论(0编辑  收藏  举报