天天看點

嵌入式面試C語言試題

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);

}

這段程式的輸出是:

(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) x^n (x的n次幂)

(b) x*n(x與n的乘積)

(c) n^x(n的x次幂)

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

5

考查多元數組與指針

void foo(int [][3]);     

    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

考查逗号表達式

    int a, b,c, d;

    a=3;

    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

考查指針數組

    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));