加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

3.08C语言考试

(2012-03-11 18:23:43)
标签:

c语言

考试

分类: CPP

说明:有些题是有争议的。

1,以下合法的变量或者宏的名称有

 A.static  B. INT  C. __zhang   D. 3_args

 Answer:     BC     

2,下面选项中等于数字077的有

 A.0b111111  B. 0x77 C. 0x4d      D. 119

 Answer:     A     

3,已知存在一个数组int  a[10],则下面正确的是

     A.int *p = a[0]      

     B. int *p = a 

     C. int  p = a[10]

     D.int *p = &a[3]

     Answer:   BCD       

4,以下正确的写法是?

     A.int a = 3.3

     B. int * a = (int *)3   

     C. int a[3][5];

     int (*p)(5) = a; 

       D.char a[13];

      char b[13] = “hello world”;

      a = b;

     Answer:     AB     

5,已知整数变量a, b则能正确表达条件两者不等的是?

     A.if ( !(a = b))        

     B. if ( a -b)   

     C. if (a != b)      

     D. if (a % b)

     Answer:     BC     

6,以下正确的声明语句有?

     A.const char *p = “hello world”;

     B. char const *p = “hello world”;

     C. char a[10] = “hello world”;

     D. char *a[10];

     char **p = a ;

     Answer:   ABCD       

7,已知int *a[10]; int b[3][10],则sizeof(a), sizeof(b[2])的结果是?

     A.44     

     B. 440  

     C. 404       

     D. 4040

     Answer:     D 32操作系统    

8,以下哪个指针的更适合指向一个二维数组char a[3][10]?

     A. char *p[10] 

     B. char (*p)[3]

     C. char (*p)[10]        

     D. char **p

     Answer:    C      

9,以下说法正确的是?

     A.3.15float类型。

     B.带参数的宏函数不会检查参数的类型

     C.宏定义必须写到同一行中     

    D.C语言中了逻辑或者比较操作中,大于或者等于0为假,小于0为真

     Answer:     B     

填空题

1C的每条语句后面必须加上  ; 

2int a = 3; int b = a++; b的值是  3 

3char a[20] = “hello world”;*(a+3)的值是  ‘l’   *(a+19)的值是  ‘\0’  

4,已知有如下定义:

          typedef  char  * cc;

          cc a, b;

          #define  dd    char *

          dd c, d;

          sizeof(b), sizeof(d)的值是 8  1      

5,已知有如下定义:

     #define mul(a, b) a*b

     int a = 3, b= 5;

     mul(a+b, a-b)的结果是  13 ==3 + 5 * 3 -5

6,已知有如下定义:

  int a = 3, b = 5; a & b的结果是   1  a ^b的结果是__6__a ^b^b的结果是  3  

7C语言中如何防止头文件被多次包含?

#ifndef 头文件名 

#define 头文件名

#endif                  

8,已知有如下定义:

     struct  bit {

          char a:3;

          char b:5;

          char c:5;

          char d:4;

     }

 sizeof(struct bit)=考虑对齐的情况下是3

9,编写一个带参数的宏,实现返回两个数中较小的一个

#define MIN(a, b)  ((a) > (b) ? (b) :(a))

 实现返回3个数中较小的一个

#define MIN(a, b, c)  (((a) > (b) (b) : (a)) <(c) ? ((a) > (b) ? (b) : (a)) : (c))  

10,以下函数的运行会有什么后果:

          int main(void)

          {

               unsigned char a[255], i;

               for (i=0; i <= 255; i++) {

               a[i] = i;

     }

          }

运行结果:死循环 unsigned char类型的i最大值是255

11,已知有如下定义:

          struct test {

               char c;

               int d;

          };

  在不考虑结构体对齐的情况下,在32位系统下

  struct test *ptr;

  ptr = (struct test *)0x1000;

  ptr + 0x200 = 0x1000 + 0x200 * 5 == 0x1000 + 0A00 == 0x1A00

 (char *)ptr + 0x200 =0x1000 + 0x200 * 1 =0x1200

 (int )ptr + 0x200 = 0x1000 + 0x200 == 0x1200  

12,下面程序的结果是:

          int func(int a)

          {

               int b = 0;

               switch(a) {

                     case 1:

                        b += 10;

                 case 2:

                        b += 10;

                     case 3:

                        b += 10;

                     default:

                        break;

     }

     return b;

     }

     func(1)的结果是      30        

13int (*s[10])(int)表示的是这是一个有10个元素的数组,元素为指针,指针指向一个函数,函数的参数类型为int,返回值类型也为int

14,已知一个数组,数组名是tabel,定义一个宏,来求解元素的个数 #define NUM(tabel)   ((sizeof(tabel) / sizeof(tabel[0])) 

15,已知char str[] = “hello world”;char *p = str

 sizeof(str) = 12 

 sizeof(p) = 8 

16,enum color{red, blue, black = 3, yellow}则如果a = red;b = yellowa=   0     , b=  4         

17,定义两个宏,分别实现对某个整型变量的某一位清零或者置一。

     清零:#define ZERO(a, n)   a & = ~(1 << n) 

     置一:#define ONE(a, n)     a |= 1 <<n 

18,void func ( char str[100] )

     {

           sizeof( str ) = ?

         }

         答案:   8     

19,已知char *p = (char *)malloc(100);sizeof(p)=      8    

三.找错题

1.以下程序会将输入的字符串倒序,请查找下面代码的错误,并指明。

include<string.h>

int reverse_string(const char * src)

{

    char *dest=NULL;

int len=strlen(src);

dest=(char*)malloc(len);

char *d=dest;

char *s=src[len];

while(len--!=0)

       d++=s--;

 printf("%s",dest);

 return 0;

}

修改后:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

int reverse_string(const char * src)

{

        if (src == NULL)

                return -1;

        char * dest = NULL;

        int len = strlen(src);

        if (len == 0)

                return -1;

        dest = (char *)malloc(len+1);

        if (!dest) {

                perror("malloc");

                return -1;

        }

        char *d = dest;

        const char *s = &src[len-1];

        while (len-- > 0)

                *d++ = *s--;

              *d = '\0';

        printf("%s\n", dest);

        free(dest);

        return 0;

}

2.对于下面的函数,要求打印出”hello”,

子程序完全正确的是    1 3 4     

一定能打印出”hello”的是    1 3    

有错误的是     2      

char *GetHellostr(void);

int main(void)

{

     char *ps;

     ps= GetHellostr( );

     if(ps != NULL)

     {

         printf(ps);

     }

     return 0;

}

(1)

char *GetHellostr(void)

{

     char *ps=“hello”;

     return ps;

}

(2)

char *GetHellostr(void)

{

    char a[]=“hello”;

     return (char *)a;

}

//a在栈里,函数调用结束后,a就释放了。

(3)

char *GetHellostr(void)

{

     static char a[]=“hello”;

     return (char *)a;

}

(4)

char *GetHellostr(void)

{

     char *ps;

     ps = malloc(10);

     if(NULL ==ps) return NULL;

     strcpy(ps,”hello”);

     return ps;

}//没有释放ps

四.问答题

1.堆和栈的区别?

  堆由程序员决定大小;堆在开辟堆空间语句调用时开辟空间;堆由程序员使用free()函数手动释放空间.

  栈由编译器决定大小;栈在函数调用时分配空间;栈在函数调用结束时自动释放空间.

2.比较带参数的宏和函数的优缺点?

  带参数的宏工作在预处理阶段;带参数的宏在编译时发生,运行速度较快;带参数的宏代码量大;带参数的宏不会验证参数的类型;带参数的宏不需要压栈;带参数的宏不能递归;带参数的宏的局限性。

  函数在运行时工作;函数在运行时发生,运行速度较慢;函数代码量小;函数会验证参数的类型;函数需要压栈;函数可以递归

3.写出gcc编译程序的主要步骤(写出编译语句即可)。

  1,预处理:分析各种预处理命令:gcc -o 1.i 1.c -E

  2,编译:根据输入文件,产生汇编语言的程序:gcc -o 1.s 1.i -S

  3,  汇编:将汇编语言输入,产生扩展名为.o的目标文件:gcc -o 1.o 1.s -c

  4,链接:以.o目标文件,库文件作为输入,生成可执行文件:gcc -o test 1.o

五.编程题

1.实现一个mystrcpy函数,作用如同标准库函数strcpy

1.c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

char * mystrcpy(char * s1, const char * s2)

{

    char * p = s1;

    while(*s1++ = *s2++);

 

    return p;

}

 

int main(void)

{

    char a[15] = {};

    char b[] = "Hello world!\n";

 

    printf("%s", mystrcpy(a, b));

 

    return 0;

}

2.实现一个函数在一个整形数组里面找出最大的那个数,函数原型如下所示:

2.c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

int get_max(int a[], int n)

{

    int max = a[0], i = 0;

 

    for(i = 0; i < n; i++){

        if(max < a[i])

            max = a[i];

    }

 

    return max;

}

 

int main(void)

{

    int a[5] = {1, 3, 5, 8, 2};

    int i = 0;

 

    printf("%d\n", get_max(a, 5));

 

    return 0;

}

3.不使用中间变量交换两个整形变量的值,写出两种方式。

#define SWAP1(a, b) a = a + b;b = a - b; a = a- b

#define SWAP2(a, b) a = a ^ b;b = a ^ b;a = a ^ b;

4.函数atoi用于将类似于”123”这样的由阿拉伯数字构成的字符字符串转化成整数123,请编程实现。

4.c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>

 

int myatoi(const char * v)

{

    int tmp = 0, sum = 0, flag = 1, i = 0;

 

    if('-' == *v){

        flag = -1;

        v++;

    }

    while(isdigit(*v++)){

        tmp = v[-1] - '0';

        sum = sum * 10 + tmp;

    }

 

    return sum * flag;

}

int main(int c, char **v)

{

    if(c != 2)

        return 0;

   

    printf("%d\n", myatoi(v[1]));

 

    return 0;

}

5.使用任意方法实现将数组int a[5]排序。

冒泡排序:

5.c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

int main(void)

{

    int i = 0, j = 0, n = 10;

    int a[10] = {12, 15, 16, 23, 9, 8, 5, 67, 78, 99};

 

    for(i = 0; i < n - 1; i++){

        for(j = 0; j < n - 1 - i; j++){

            if(a[j] > a[j + 1]){

                a[j] = a[j] + a[j + 1];

                a[j + 1] = a[j] - a[j + 1];

                a[j] = a[j] - a[j + 1];

            }

        }

    }

    for(i = 0; i < 10; i++){

        printf("%d ", a[i]);

    }

    printf("\n");

 

    return 0;

}

6.使用任意方法实现文件复制程序。

6.c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <fcntl.h>

#include <sys/types.h>

#include <sys/uio.h>

 

#define BUF 512

 

int main(int c, char ** v)

{

    if(c != 3)

        return 0;

    int fd1 = 0, fd2 = 0;

    ssize_t ret;

    char buf[BUF] = {};

 

    fd1 = open(v[1], O_RDONLY);

    if(fd1 < 0){

        perror("open fd1");

        exit(0);

    }

    fd2 = open(v[2], O_WRONLY | O_TRUNC | O_CREAT, 0777);

    if(fd2 < 0){

        perror("open fd2");

        exit(0);

    }

    while(1){

        ret = read(fd1, buf, BUF);

        if(0 == ret){

            break;

        }

        write(fd2, buf, ret);

    }

 

    close(fd1);

    close(fd2);

 

    return 0;

}

7, 使用任意方法实现文件剪切程序。

ln -s test test1创建软链接(快捷方式)

ln test test1创建硬链接

ls -l test查看test信息

link()函数创建硬链接

unlink()函数删除硬链接

 

删除文件就是删除硬链接

7.c

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <unistd.h>

 

int main(void)

{

    int ret = link("test", "test1");

 

    if(ret < 0){

        perror("link");

        exit(-1);

    }

 

    unlink("test");

 

    return 0;

}

8.编写一个程序将输入字符串中的大写字母转化为小写字母,并返回大写字母的个数。函数原型如下:

int func(char * p)参数src为输入的字符串,函数的返回值为字符串中大写字母的个数。

8.c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

int func(char * p)

{

    int n1 = 0, n2 = 0, i = 0;

 

    for(i = 0; i < strlen(p); i++){

        if(p[i] >= 'a' && p[i] <= 'z'){

            p[i] -= 32;

            n1++;

        }else if(p[i] >= 'A' && p[i] <= 'Z'){

            p[i] += 32;

            n2++;

        }

    }

 

    return n2;

}

 

int main(int c, char **v)

{

    if(c != 2)

        return 0;

    char * p = v[1];

   

    printf("%d\n%s\n", func(p), p);

 

    return 0;

}

9.编写一个函数,用来在一个给定的整形数组里面找出第2大的数。函数原型如下

int func(int a[], int n)

参数a为数组的地址,n为数组的个数。

函数返回值就是要找的数。

9.c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <math.h>

 

int func(int a[], int n)

{

    int max = 0, send = 0, i = 0;

 

    max = a[0] < a[1] ? a[1] : a[0];

    send = a[0] > a[1] ? a[1] : a[0];

    for(i = 2; i < n; i++){

        if(a[i] > max){

            send = max;

            max = a[i];

        }else if(a[i] > send){

            send = a[i];

        }

    }

 

 

    return send;

}

 

int main(void)

{

    int a[10] = {12, 15, 17, 26, 78, 90, 45, 76, 87, 43};

 

   printf("%d\n", func(a, 10));

 

    return 0;

}

                                                                                                                      

10.用递归方法判断一个数组是否为递增数组(使用递归时充分考虑递归终止条件,以及函数调用思想),函数原型如下;

int func(int a[], int n)

参数a为数组的地址,n为数组的个数。

函数的返回值如果为0则该数组为递增数组,为1则是递减数组。

10.c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

int func(int * a, int n)

{

    if(2 == n){

        if(a[0] > a[1]){

            return -1;

        }else if(a[0] < a[1]){

            return 1;

        }else{

            return 0;

        }

    }

 

    if(-1 == func(a, n - 1)){

        if(a[n - 2] > a[n - 1]){

            return -1;

        }else

            return 0;

    }else if(1 == func(a, n - 1)){

        if(a[n - 2] < a[n - 1]){

            return 1;

        }else

            return 0;

    }else

        return 0;

}

 

int main(void)

{

    int a[5] = {};

    int i = 0;

 

    for(i = 0; i < 5; i++){

        scanf("%d", &a[i]);

    }

    printf("%d\n", func(a, 5));

 

    return 0;

}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有