C语言的基础知识点






#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

void test_1()
{
	int const a = 2;
	const int b = 3;

	int x = 22;
	int y = 33;
	int z = 44;
		
	const int* a1 = &x;   //常量指针  可以修改地址  不可以修改值 
	int const *b1 = &y;   //常量指针
	
	a1 = &y;
 
	printf("*a = %d\n",*a1);
	printf("x = %d\n",x);
	
	int* const c1 = &z;   //指针常量   可以修改值   不可以修改地址 
	*c1 = 5;
	printf("*c = %d\n",*c1); 	
	printf("z = %d\n",z);
}
void test_2()
{
	char str1[] = "abc";
	char str2[] = "abc";
	
	if(str1 == str2) printf("str1 = str2\n");   //str1和str2是各自的首地址 
	else printf("str1 != str2\n");
	
	
	char* str3 = "abc";    //字符指针,,以常量形式存储于常量区,,str3和str4都指向同一常量区 
	char* str4 = "abc";
	if(str3 == str4)printf("str3 = str4\n");
	else printf("str3 != str4\n"); 
}
void test_3()    //防止缓冲区溢出 
{
	printf("input ...\n");
	char buff[4];
	gets(buff);
	int num = strlen(buff);
	printf("num is : %d\n",num);
	
	if(num<=4) puts(buff);
}
void test_4()   //测试strlen函数对\0的计算 
{
	printf("\\0 length is: %d\n",strlen("\0"));	//printf函数遇到第一个\0也会结束 
}

void test_5()
{
	struct test
	{
		char x1;     //1Byte
		short x2;    //2Byte
		int x3;      //4Byte
		float x4;    //4Byte	
	};
	struct test t1;
	printf("the Bytes of struct test is : %d\n",sizeof(t1));   //32bit system 4字节对齐 
} 

void test_6(char a[10])
{
	printf("the size of a[10] is :%d\n",sizeof(a));   //数组作为参数传递,,会退化成指针:char* a; sizeof(a)测得是指针a的地址 
}
void test_7()
{
	unsigned int* p1 = NULL;         //防止出现野指针 
	p1 = (unsigned int*)0x80000000;
	printf("%x",p1+1);               //指针移动的结果,,或指针与数字相加的结果 
}

void test_8()
{
	char* p = (char*)malloc(sizeof(char)*20);
	strcpy(p,"Hello");
	free(p);
	
	
	printf("before p = NULL\n");
	if(p != NULL)
	{
		printf("p != NULL \n");
		printf("%s\n",p);//指针p被释放后,指向其他的地方,因此出现乱码 	
	}
	
	//为防止出现野指针,,指针被释放后,,应该强制指向空指针
	p = NULL; 
	
	printf("after p = NULL\n");
	if(p != NULL)
	{
		printf("p != NULL \n");
		printf("%s\n",p);//指针p被释放后,指向其他的地方,因此出现乱码 	
	}
} 
void test_9()
{
	int n = 3;
	int* pt = NULL;
	void* gp = NULL;   //通用指针的使用 
	gp = &n;
	pt = (int*)gp;
	printf("%d\n",*pt);
}
void test_10()   //不适用大于,小于和if语句来比较两个数的大小 
{
	#define Max(a,b) ((a)-(b))== fabs((a)-(b))?a:b
//	#define Min(a,b) ((a)-(b))== fabs((a)-(b))?b:a
	#define Min(a,b) ((a)-(b))!= fabs((a)-(b))?a:b
	
	printf("Max = %d,Min = %d\n",Max(1,2),Min(1,2));
	
}
void test_11()   //如何判断一个数是signed还是unsigned 
{
	#define isUnsigned(n) ((n)>=0)&&((~n)>=0)
	#define isUnsigned_type(type) ((type)(0-1)>0)
	unsigned int aa = 10;
	int bb = 10;
	
	printf("%d\n",isUnsigned(aa));
	printf("%d\n",isUnsigned(bb));
	printf("%d\n",isUnsigned_type(unsigned int));
	printf("%d\n",isUnsigned_type(int));
} 

void test_12()  //不适用sizeof求int占用的字节数
{
	#define Mysize(value) (char*)(&value+1)-(char*)(&value)
	
	int i;
	double d;
	float f;
	char c;
	int arr[10];
	
	printf("%d\n",sizeof(char*));
	printf("%d\n",sizeof(int*));
	
	printf("i : %d\n",Mysize(i));
	printf("d : %d\n",Mysize(d));
	printf("f : %d\n",Mysize(f));
	printf("c : %d\n",Mysize(c));
	printf("arr : %d\n",Mysize(arr));

} 
//-------------------------------------------------------
	#define Offset(type,field) ((size_t)&(((type*)0)->field))
	struct Mystr
	{
		char a;
		int b;
		float c;
		double d;
		char e;	
	};
void test_13()     //使用宏求结构体的内存偏移 
{
 
//	struct Mystr my;
//	int addr = Offset(my,a);
	printf("%d\n",Offset(struct Mystr,a));
	printf("%d\n",Offset(struct Mystr,b));
	printf("%d\n",Offset(struct Mystr,c));
	printf("%d\n",Offset(struct Mystr,d));
	printf("%d\n",Offset(struct Mystr,e));
	
}
//----------------------------------------------------------

void test_14()
{
	//使用sizeof计算数组中有多少个元素
	#define My_size(arr,element) (sizeof(arr)/sizeof(element))
	
	int array[] = {1,2,3,4,5,6,7};
	int num = My_size(array,array[0]);
	printf("the array size is : %d\n",num); 
}


//----------------------------------------------
void test_15()    //最有效的计算2*8
{
	int i = (2<<3);
	printf("2*8 = %d\n",i);
	
	int num = 5;     //最高效的计算一个数的7倍 
	int result = (num<<3)-num;
	printf("5*7 = %d\n",result);   //num*(8-1)  num*8-num -> num<<3-num
} 
//------------------------
void test_16()
{
	//大小端的识别
	unsigned int i = 0x1122;
	unsigned char j = (unsigned char)i;
	printf("j =  %x\n",j); 
	if(j == 0x22)
	{
		printf("this computer is litter endian.\n");   //小端  低字节存在低地址	
	}
	else if(j == 0x11)
	{
		printf("this computer is big endian.\n");	  //大端  低字节存在高字节 
	}
	else
	{
		printf("Unkown.\n");	
	}
} 

//-----------------------------------
int Sum_1(int num,...)   //可变参数的使用 
{
	int i = 0;
	int sum = 0;
	int* pt = NULL;
	pt = (int*)&num + 1;
	for(i=0;i<(int)num;i++)
	{
		printf("%d,%d\n",i,*pt);
		sum += *pt;
		pt++;	
	} 
	return sum;
}
void test_17()
{
	//可变参数的使用
	int array[] = {1,2,3,4,5,6,7,8,9};
	int len = sizeof(array)/sizeof(array[0]);
	
	int i = 10;
	int j = 20;
	
	printf("sum = %d\n",Sum_1(2,i,j));
		
	printf("sum = %d\n",Sum_1(len,array[0],array[1],array[2],array[3],array[4],array[5],array[6],array[7],array[8]));
}
//int add2(int num,...)
//{
//	int sum = 0;
//	int index = 0;
//	int *p = NULL;
//	p = (int*)&num+ 1;
//	for(index=0;index<(int)num;index++)
//	{
//		sum += *p++;	
//	}
//	return sum;
//}
//void test_17()
//{
//	int i = 1;
//	int j = 2;
//	int k = 3;
//	printf("%d\n",add2(3,i,j,k));
//}
//---------------------

//函数指针的使用 
typedef int (*func)(int a,int b);


int add(int a,int b)
{
	return a+b;
}
int multi(int a,int b)
{
	return a*b;
}

void test_18()
{
	
	func f;
	f = add;
	printf("f(1,2) = %d\n",f(1,2));
	f = multi;
	printf("f(2,5) = %d\n",f(2,5));
}
//---------------------
//数组指针:指向数组的一个指针 
void test_19()
{
	int (*pt)[4] = NULL;
	int array[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
	pt = &array[0]; 
	int index = 0;
	for(index=0;index<12;index++)
	{
		printf("%d\n",(*pt)[index]);
	} 
} 
//指针数组:包含的元素是指针
void test_20()
{
	int* pt[4];
	int array[4] = {1,2,3,4};
	pt[0] = &array[0];
	pt[1] = &array[1];
	pt[2] = &array[2];
	pt[3] = &array[3];
	
	int index = 0;
	for(index=0;index<4;index++)
	{
		printf("%d\n",*pt[index]);	
	}
} 

//---------------------
int main(int argc, char *argv[]) 
{
	int i = 0;
	for(i=0;i<argc;i++)
	{
		printf("%d,%s\n",i,argv[i]);	
	} 
//	test_1();
//	test_2();
//	test_3();
//	test_4(); 
//	test_5();
//	char a[10] = {0};test_6(a);
//	test_7();
//	test_8();
//	test_9();
//	test_10();
//	test_11();
//	test_12();
//	test_13();
//	test_14();
//	test_15();
//	test_16();
//	test_17();
//	test_18();
//	test_19();
	test_20();
	 
	return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值