C语言基础试题

来源:互联网 发布:闪电侠剧情介绍知乎 编辑:程序博客网 时间:2024/06/10 01:10
C基础补习考试题
一、简答题(每题4分,部分题3分,共70分)
1、C中的内存有四种,代码段,数据段,栈,堆;请问:全局变量、static 修饰的局部变量、字符串常量、普通的局部变量、malloc开辟内存、函数分别是存储在什么内存当中的,并且分别说明什么时候开辟内存,什么时候销毁内存。(4分)    
2、运行char a[] = ”abc”后,内存会开辟几块内存,这此内存共占多少个字节? (4分) 
3、运行char *p = ”abc”后,内存会开辟几块内存,这此内存共占多少个字节? (4分)
4、简述const的作用?至少写两种? (4分)
5、int *(*p)[3]; 那么指针p的偏移量是多少字节?(4分)
6、头文件中的 ifndef/define/endif 干什么用?(4分)
7、#include  <filename.h>   和  #include  “filename.h” 有什么区别? (4分)
8、以下代码的的打印结果相同吗? (4分)
char a1[]=”abc”, char a2[]=”abc”, printf(“%p\n”,a1);printf(“%p\n”,a2);
9、以下代码的的打印结果相同吗?(4分)
Char *s1 =”abc”, char *s2 =”abc”, printf(“%p\n”,a1);printf(“%p\n”,a2);
10、char x = 1;  char y = x<<7; 请问y是正数还是负数? (4分)
11、char x = 1;  char y =~x;请问y是正数还是负数?(4分)
12、int x=0;   int y=0;  int z = (x++,    y=x++,    y=++x) ;请问z的值是多少?(3分)
13、int x=0; int y=0; int r = x&&y++;请问y的值是多少?(3分)
14、static 修饰全局变量与函数时的作用是什么? (4分)
15、char *s1 = “abc”; char *s2=”xyz”;  *s1 = *s2;以上代码有错吗?如果有错,请说明理由。(4分)
16、int a[6]={1,2,3,4,5,6}; printf(“%d\n”,*((int *)(&a+1)-1));;打印结果是什么? (4分)
17、int a[2][3]={1,2,3,4,5,6},printf(“%d”, *((int *)(&a+1)-3)  );打印结果是什么?(4分)
18、以下打印结果是不是全部相同?(4分)
int a[2][3]; 
printf(“%p\n”,&a[0][0]); 
printf(“%p\n”, a);  
printf(“%p\n”a[0]);    
printf(“%p\n”&a);  
                  
























二、指出以下程序的问题(每题5分,共30分)


void getMemory(char *p)
{
p = (char *)malloc(100);
}
void Test(void) 
{
char *str = NULL;
getMemory(str);
strcpy(str, "hello world");
printf(str);
}
char *getMemory(void)
{
char p[] = "hello world";
return p;
}
void Test(void)
{
char *str = NULL;
str = getMemory();
printf(str);
}


void getMemory(char **p, int num)
{
*p = (char *)malloc(num);
}
void test(void)
{
char *str = NULL;
GetMemory(&str, 100);
strcpy(str, "hello");
printf(str);
} void test(void)
{
char *str = (char *) malloc(100);
strcpy(str, “hello”);
free(str);    
if(str != NULL)
{
      strcpy(str, “world”); 
printf(str);
}
}


void fun(char* str1, char* str2)
{
  *str1 = *str2;
}
main()
{
    char *str1="ABC\n";
    char *str2="BCD\n";
    fun(str1, str2);
    printf(str1);
} void f1(char *p)
{
p = (char *)malloc(100);
}
int test() 
{
char *str = NULL;
f1(&str);
strcpy(str, "hello world");
printf(str);
}

原创粉丝点击