C语言的一些题目(最终)(二)

2014-11-24 11:51:11 · 作者: · 浏览: 5
编译器处理方式不同
  define宏是在预处理阶段展开。
  const常量是编译运行阶段使用。
(2) 类型和安全检查不同
  define宏没有类型,不做任何类型检查,仅仅是展开。
  const常量有具体的类型,在编译阶段会执行类型检查。
(3) 存储方式不同
  define宏仅仅是展开,有多少地方使用,就展开多少次,不会分配内存。
  const常量会在内存中分配(可以是堆中也可以是栈中)。
____________________________________________________________


15.下面代码的输出结果是什么


#include
   
    


struct{
	short a1; 
	short a2;
	short a3;
	
}A;
struct{
	long a1;
	short a2;
}B;


int main()
{
	char* ss1 = "0123456789";
	char ss2[] = "0123456789";
	char ss3[100] = "0123456789";
	int ss4[100];
	char q1[] = "abc";
	char q2[] = "a\n";
	char* q3 = "a\n";
	char (*str1)[100] = (char(*)[100])malloc(100);
	void* str2 = (void*)malloc(100);
	printf("%d\n",sizeof(ss1));		//4
	printf("%d\n",sizeof(ss2));		//11
	printf("%d\n",sizeof(ss3));		//100
	printf("%d\n",sizeof(ss4));		//400
	printf("%d\n",sizeof(q1));		//4
	printf("%d\n",sizeof(q2));		//3
	printf("%d\n",sizeof(q3));		//4
	printf("%d\n",sizeof(A));		//6 结构体都跟其中最大的的元素类型对齐
	printf("%d\n",sizeof(B));		//16 跟long对齐,8*2
	printf("%d\n",sizeof(*str1));		//100
	printf("%d\n",sizeof(str2));		//4
return 0;
}

   



____________________________________________________________


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


#include
   
    


main()
{
	char *a []={"hello","the","world"};
	char **pa=a;
	pa++;
	printf(*pa);			//the
}

   


____________________________________________________________


17.以下代码的输出结果是什么?


#include
   
    


int main()
{
	int x=10,
	    y=10,
	    i;	
	for(i=0;x>8;y=i++)
	{
		printf("%d,%d",x--,y);		//10, 10
		return 0;
	}
}
   


____________________________________________________________


18.下列程序会在哪一行崩溃?
typedef struct s {
	int i;
	int * p;
}S;


main()
{
	S s;			//定义一个结构体,其中有两个元素,i 和 *p
	int *p = &s.i;		//定义指针变量p,装着s中i的地址	    
	p[0] = 4;		//下标运算,在i中放入4
	p[1] = 3;		//在p中放入3
	s.p = p;		//把指针p中的地址,也就是s中i的地址给了s中的p
	s.p[1] = 1;		//下标运算,把s中的p的东西,也就是i的地址的下一位,也就是
					//s中p装入一个1
	s.p[0] = 2;		//在这一行崩溃,p中装着一个常量,无法进行写操作
}




____________________________________________________________


19.
写出函数指针、 // void (*pf)(); void func(); pf = &func;
函数返回指针、 // void* func();
const指针、 //char *const p;
指向const的指针、 // const char *p;
指向const的const指针。 // const char *const p;