C语言知识点
宏定义的知识.c或.cpp文件 预编译 const int maxint=12.常变量在编译时替换后面有分号。 宏不开辟空间 .i文件 编译 #define PI 3.14 宏定义在预编译时替换。宏定义后面没有分号。 **在这里插入代码片*#include #define MAXINT 12 #define PI 3.14 int main() { double r = MAXINT;`` double s; s = r*r*PI;//s=r*r*3.14 预编译时。 printf("%f",s); }* 宏在预编译时替换。 enum在预编译时不替换。 int Max(int a,int b) { return a > b ? a : b; } #define MAX(a,b)(a>b? a:b) int main() { int x = 10,y = 20; //Max(++x,y); //printf("%d n",x);//x=11 MAX(x,++y); printf("%dn",y);//y=22 return 0; } MAX(x>++y? x:++y);预编译时宏的替换,所以是22。所以加两次。 #define NUM(a,b) a*b int main() { int x = 3,y = 4; printf("%dn",NUM(x + 5,y + 6)); return 0; } 所以要达成效果是时要这么定义#define NUM(a,b)(a)*(b) 结果就是80. #define DEBUG struct node { int a; char b; }; #define DEBUG //这是已定义状态,会进行if判空 enum status { point1error,point2error }; void *my_memcpy(const void *p1,void p2,unsigned int n)//参数表的类型应该为无类型,这样即使在调用结构体或者数组是依旧可以使用,n表示字节数 { //另外之所以返回时是一个泛型指针(void),为的是个strcpy一样可以多次复制更加方便 #ifdef DEBUG //这是另一种宏定义的方式,当我们给DEBUG进行#define的宏定义时那么程序中的判断指针是否为NULL就会起作用,若不进行定义则在编译的过程中会把判空函数给自动取消编译 if ((p1 == NULL) || (p2 == NULL)) { return NULL; } #endif char p3 = (char)p1;//之所以强转是为了达到每一个字节都复制的效果 char p4 = (char)p2;//这里就是为了实现链式表达的作用从而达到多是使用 while (n–) { *p4++ = *p3++; } return p2; } void main() { node a = { 3,‘m’ }; node b; node c; my_memcpy(&a,&b,sizeof(node)); my_memcpy(my_memcpy(&a,sizeof(node)),&c,sizeof(node)); } 无类型指针(1)能指向任何地址,(2)不能给别的赋值,(3)不能自加 ``#ifdef SDBJ ** #else ** #endif 相当于 if else语句。在预编译时。 #ifdef #endif` #include的实质是在预编译时将文件拷贝到源文件里。 #ifdef 和 #ifndef 是相反的 前者以有为真 后者以没有为真。 ` (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |