北阳电子_C语言_面试试题

来源:互联网 发布:自己编写炒股软件 编辑:程序博客网 时间:2024/05/17 23:34

1. 已知变量data1定义如下

union data{        int i;        char ch;        float f;}data1;
CPU为32位系统,问sizeof(data1)占多少个字节?

答:12个


2. 如下程序

void main(void){        char string1[4] = {'A', 'B', 'C', 'D'};        char string2[4] = {'E', 'F', 'G', 'H'};        strcpy(string1, string2);               //where is wrong here?        printf("string1 : %s\n"string2);        //where is wrong here?}
答:1) 没有加'\0'

2) 输出乱码, 同样是没有加'\0


3. 请说明宏定义的作用

#ifndef __INVvxWorksh#define __INVvxWorksh   //what is the purpose?#ifdef __cplusplus      //what is the purpose?extern "c"{             //what is the purpose?#endif#ifdef __cplusplus}#endif#endif /* __INVvxWorksh*/
答:1) 防止文件重复引用, 若有此宏定义则不会执行此行代码

2)作用也是条件编译

3)告诉编译器按照C语言的各类来编译


4. 在Intel 32位X86平台上, 假定一段内存地址为0X00001000开始的内存的值为以下内容

0X00001000: 00001000   00011003   00040000 00060000

int *p = (int *)0X00001000; p+=2; *p=_____________;

答:0x40000


5. 实现strc()函数

char *my_strcat(char *dest, const char *src){        char *head = dest;        if(head == NULL || src == NULL)        {                   return NULL;        }           while(*head)                head++;        while(*src)                *head++ = *src++;        *head = '\0';        return dest;}

6. 计算一个short类型的数中有多少个1

int count_short(int num){        int count = 0;        while(num)        {                if(num % 10 == 1)                {                        count++;                }                num /=10;        }        return count;}

8. 实现atoi()函数,并将“123446”转换成123456

int my_atoi(const char *pInput){        char *tmp = pInput;        char ch;        int num = 0;        while(ch = *tmp++)        {                if(ch >= '0' && ch <= '9')                {                        num = num * 10 + ch - '0';                }                else                        return -1;        }        return num;}


10. 说明以下static的作用

a)在函数体内staitc修饰的变量 //静态的局部变量,只初始化一次,多次调用保留上一次的值

b)在函数体外static修饰的变量 //静态全局变量,其他文件不能访问

c)static修饰的函数 //静态函数,有文件作用域,其他文件不能访问


12. 请指出以下程序的错误

char *getmemory(void){        char p[] = "helloworld";        return p;}void test(void){        char *str = NULL;        str = getmemory();        printf(str);}
答:在getmemory()函数中,变量p是局部变量,函数介绍就释放,返回的不是有效的地址


13. 下面程序输出结果是什么?

int main(void){        unsigned int a = 6;        int b = -20;        (a + b > 6)?printf(">6"):printf("<6");}
答: >6, 因为在运算 a + b时进行了类型转换, int 转换成了 unsigned int, a + b = 20;







0 0