考查嵌入式C开发人员的最好的0x10道题

来源:互联网 发布:枫树浏览器 知乎 编辑:程序博客网 时间:2024/05/16 15:20
约定:
   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题:考查类型转换
main(){   struct node    {     int a;     int b;     int c;        };   struct node  s= { 3, 5,6 };   struct node *pt = &s;   printf("%d" ,  *(int*)pt);}  
这段程序的输出是:
(a) 3
(b) 5
(c) 6
(d) 7

第3题:考查递归调用

 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) xn
(b) x*n
(c) nx
(d) 以上均不是

第4题:考查指针
main() {  int  a[5] = {1,2,3,4,5};  int *ptr =  (int*)(&a+1);  printf("%d %d" , *(a+1), *(ptr-1) );}  
这段程序的输出是:

(a) 2 2
(b) 2 1
(c) 2 5
(d) 以上均不是

第5题:考查多维数组与指针
void foo(int [][3] );     main(){  int a [3][3]= { { 1,2,3} , { 4,5,6},{7,8,9}};  foo(a);  printf("%d" , a[2][1]);}void foo( int b[][3])   {  ++ b;  b[1][1] =9;}  
这段程序的输出是:

(a) 8
(b) 9
(c) 7
(d)以上均不对


第6题目:考查逗号表达式
main(){  int a, b,c, d;  a=3;  b=5;  c=a,b;  d=(a,b);  printf("c=%d" ,c);  printf("d=%d" ,d);}
这段程序的输出是:

(a) c=3 d=3
(b) c=5 d=3
(c) c=3 d=5
(d) c=5 d=5

第7题:考查指针数组

main(){  int a[][3] = { 1,2,3 ,4,5,6};  int (*ptr)[3] =a;  printf("%d %d "  ,(*ptr)[1], (*ptr)[2] );  ++ptr;  printf("%d %d"  ,(*ptr)[1], (*ptr)[2] );}
这段程序的输出是:

(a) 2 3 5 6
(b) 2 3 4 5
(c) 4 5 0 0
(d) 以上均不对

第8题:考查函数指针
int *f1(void){  int x =10;  return(&x);}int *f2(void){  int*ptr;  *ptr =10;  return ptr;}int *f3(void){  int *ptr;  ptr=(int*) malloc(sizeof(int));  return ptr;}
上面这3个函数哪一个最可能引起指针方面的问题

(a) 只有 f3
(b) 只有f1 and f3
(c) 只有f1 and f2
(d) f1 , f2 ,f3

第9题:考查自加操作(++)

main(){  int i=3;  int j;  j = sizeof(++i+ ++i);  printf("i=%d j=%d", i ,j);}
这段程序的输出是:

(a) i=4 j=2
(b) i=3 j=2
(c) i=3 j=4
(d) i=3 j=6

第10题:考查形式参数,实际参数,指针和数组
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;  *p = q;  q= tmp;}void f2( int* p , int q){  int tmp;  tmp =*p;  *p = q;  q= tmp;}  
这段程序的输出是:

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

第11题:考查自减操作(--)
void e(int );   main(){  int a;  a=3;  e(a);}void e(int n){  if(n>0)  {    e(--n);    printf("%d" , n);    e(--n);  }}
这段程序的输出是:

(a) 0 1 2 0
(b) 0 1 2 1
(c) 1 2 0 1
(d) 0 2 1 1

第12题:考查typedef类型定义,函数指针
typedef int (*test) ( float * , float*)test tmp;
 tmp 的类型是

(a) 函数的指针,该函数以 两个指向浮点数(float)的指针(pointer)作为参数(arguments)
      Pointer to function of having two arguments that is pointer to float
(b) 整型
(c) 函数的指针,该函数以 两个指向浮点数(float)的指针(pointer)作为参数(arguments),并且函数的返回值类型是整型
      Pointer to function having two argument that is pointer to float and return int
(d) 以上都不是


第13题:数组与指针的区别与联系
main(){  char *p;  char buf[10] ={ 1,2,3,4,5,6,9,8};  p = (buf+1)[5];  printf("%d" , p);}
这段程序的输出是:

(a) 5
(b) 6
(c) 9
(d) 以上都不对

第14题:

Void f(char**);main(){  char * argv[] = { "ab" ,"cd" , "ef" ,"gh", "ij" ,"kl" };  f( argv );}void f( char **p ){  char* t;  t= (p+= sizeof(int))[-1];  printf( "%s" , t);}
这段程序的输出是:

(a) ab
(b) cd
(c) ef
(d) gh

第15题:

#include<stdarg.h>int ripple ( int , ...);main(){  int num;  num = ripple ( 3, 5,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

第16题:
int counter (int i){  static int count =0;  count = count +i;  return (count );}main(){  int i , j;  for (i=0; i <=5; i++)    j = counter(i);}
The value of j at the end of the execution of the this program is:

(a) 10
(b) 15
(c) 6
(d) 7







详细参考答案

第1题:   (b)
volatile字面意思是易于挥发的。这个关键字来描述一个变量时,意味着 给该变量赋值(写入)之后,马上再读取,写入的值与读取的值可能不一样,所以说它"容易挥发"的。
这是因为这个变量可能一个寄存器,直接与外部设备相连,你写入之后,该寄存器也有可能被外部设备的写操作所改变;或者,该变量被一个中断程序,或另一个进程
改变了.
volatile variable isn't affected by the optimization. Its value after the longjump is the last value variable assumed.

b last value is 5 hence 5 is printed.

setjmp : Sets up for nonlocal goto /* setjmp.h*/

Stores context information such as register values so that the lomgjmp function can return control to the statement following the _disibledevent="file:///D:/下载资料/网页/CVC/考查嵌入式C开发人员的最好的0x10道题%20-%20hecrics的BLOG%20-%2052RD%20BLOG.files/image5.jpg" target="_blank"> 
Taking a pointer to the element _disibledevent="file:///D:/下载资料/网页/CVC/考查嵌入式C开发人员的最好的0x10道题%20-%20hecrics的BLOG%20-%2052RD%20BLOG.files/image4.jpg" target="_blank">


第6题:  (c)
The comma separates the elements of a function argument list. The comma is also used as an operator in comma s. Mixing the two uses of comma is legal, but you must use parentheses to distinguish them. the left operand E1 is evaluated as a void , then E2 is evaluated to give the result and type of the comma . By recursion, the 

E1, E2, ..., En

results in the left-to-right evaluation of each Ei, with the value and type of En giving the result of the whole .
c=a,b;  / *yields c=a* /d=(a,b); /* d =b  */


第7题:  (a)

/* ptr is pointer to array of 3 int */


第8题:  (c)
f1 and f2 return address of local variable ,when function exit local variable disappeared

第9题:  (b)
sizeof operator gives the number of bytes required to store an object of the type of its operand . The operands is either an , which is not evaluated ( (++i + ++ i ) is not evaluated so i remain 3 and j is sizeof int that is 2) or a parenthesized type name.

第10题:  (a)
void(*p[2]) ( int *, int);
define array of pointer to function accept two argument that is pointer to int and return int. 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 _disibledevent="file:///D:/下载资料/网页/CVC/考查嵌入式C开发人员的最好的0x10道题%20-%20hecrics的BLOG%20-%2052RD%20BLOG.files/image1.jpg" target="_blank">

 


第12题:  (c)
C provide a facility called typedef for creating new data type names, for example declaration
typedef char string
Makes the name string a synonym for int .The type string can be used in declaration, cast, etc, exactly the same way that the type int can be. Notice that the type being declared in a typedef appears in the position of a variable name not after the word typedef.

第13题:  (c)
If the type of an is "array of T" for some type T, then the value of the is a pointer to the first object in the array, and the type of the is altered to "pointer to T"
So (buf+1)[5] is equvalent to *(buf +6) or buf[6]

第14题:  (b)

p+=sizeof(int) point to argv[2]

(p+=sizeof(int))[-1] points to argv[1]

第15题:  (c)
When we call ripple value of the first argument passed to ripple is collected in the n that is 3. va_start initialize p to point to first unnamed argument that is 5 (first argument).Each call of va_arg return an argument and step p to the next argument. va_arg uses a type name to determine what type to return and how big a step to take Consider inner loop
(; i; i&=i-1) k++ /* count number of  1 bit in i *

in five number of 1 bits is (101) 2
in seven number of 1 bits is (111) 3
hence k return 5

example

let  i= 9 = 1001     i-1  = 1000            (i-1) +1 = i               1000                 +1              1 001
The right most 1 bit of i has corresponding 0 bit in i-1 this way i & i-1, in a two complement number system will delete the right most 1 bit I(repeat until I become 0 gives number of 1 bits) 


第16题:  (b)
The answer is (b)

Static variable count remain in existence rather than coming and going each time function is called
so first call counter(0) count =0
second call counter(1) count = 0+1;
third call counter(2) count = 1+2; /* count = count +i */
fourth call counter(3) count = 3+3;
fifth call counter(4) count = 6+4;
sixth call counter(5) count = 10+5;
0 0
原创粉丝点击