300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > C语言 宏定义#define #ifdef #undef #endif 调试宏

C语言 宏定义#define #ifdef #undef #endif 调试宏

时间:2018-12-23 17:37:05

相关推荐

C语言 宏定义#define #ifdef #undef #endif 调试宏

宏定义的注意事项:

#define 宏名 宏体

宏名:大写字母表示

宏体:1.一定要用括号括起来2.语义一定要清楚

#define SECOND_OF_YEAR (365*24*3600)UL

由于是秒,一定要用无符号常整形

#if 0这里面可以随便写啥,就相当与注释#endif

#include <stdio.h>int main(){printf("%s %s\n",__DATE__,__TIME__);printf("%s\n",__FILE__);printf("[%s:%d]\n",__FUNCTION__,__LINE__);printf("Warning msg!\n");}/*ANSI C标准中有几个标准预定义宏(也是常用的):__FUNCTION__:当前处在哪个函数中__LINE__:在源代码中插入当前源代码行号;__FILE__:在源文件中插入当前源文件名;__DATE__:在源文件中插入当前的编译日期__TIME__:在源文件中插入当前编译时间;__STDC__:当要求程序严格遵循ANSI C标准时该标识被赋值为1;__cplusplus:当编写C++程序时该标识符被定义。*/

/*交换两个数*/#include <stdio.h>#define SWAP(T,x,y) {T t=x;x=y;y=t;}int main(){int a=1,b=2;double c=1.1,d =2.2;SWAP(int, a,b);SWAP(double,c,d);return 0;}

#include <stdio.h>#define swap(type,x,y) {type temp =(x);(x) =(y);(y) =temp;}#define print(x,y) printf(#x"=%d," #y"=%d \n",x,y)#define paste(front,back) front ## backint main(){int x =1;int y =2;swap(int,x,y);print(x,y);char* paste(name,1) ="hello world";printf("%s \n",name1);return 0;}

#include <stdio.h>#define Pi 3.14#define S(r) Pi*(r)*(r)int main(){printf("%g",S(2));return 0;}

#include <stdio.h>#define MAX(x,y) ((x)>(y)?(x):(y))//remember the bracesint main(){int a =1,b=2;int c =3,d =4;printf("%d\n",MAX(a,b)+MAX(c,d));return 0;}

/*克服i++给宏函数带来的弊端*/#include <stdio.h>#define max1(x,y) ({typeof(x) A = x,B = y;\(A)>(B) ? (A):(B);})// typeof(x):获取x的类型int main(){int a=1, b=2;printf("%d \n",max1(a++,b++));//printf("%d \n",({int A = a++,B = b++; (A)>(B) ? (A):(B);}));return 0;}

#include <stdio.h>/* 受上面程序启发,使用宏定义一个自己的SIZEOF运算符*/#define SIZEOF(var) ({typeof(var)* p = &var;(char*)(p+1)-(char*)p;})int main(){long i = 0;printf("%ld \n",SIZEOF(i));return 0;}

一个#号,字符串化, #define ABC(x) #x //x所代表的值会被加双引号替换成“x”,当成一个字符串

#include<stdio.h>#define print(x) printf(#x)int main(){print(hello world\n);return 0;}#include <stdio.h>#define STR(x) puts(#x)int main(){STR(hello);//puts("hello")return 0;}

两个##号,连接符号 #define ABC(x) day#x//相当于day作为前缀,和x的值合成一个新单词

#include<stdio.h>#define DAY(a) myday##aint main(){int myday1 =1;int myday2 =2;printf("%d %d",DAY(1),DAY(2));return 0;}#include <stdio.h>#define P(XXXXX) print##XXXXX()//##replace the stringvoid printhello(){printf("hello \n");}void printworld(){printf("world \n");}int main(){P(hello);//printhello()P(world);//printworld()return 0;}

C语言#if、##ifdef、#ifndef的用法详解,C语言条件编译详解

宏定义来实现条件编译(#define #undef #ifdef)_麒麒川的博客-CSDN博客_宏定义条件编译

#include <stdio.h>#ifndef varX#define varX 1int x =10000;#endif#ifdef varX#undef varX//cancel define varX#endif#ifndef varX#define varX 2//int X =10086;//cause conflict#endifint main(){printf("%d \n",x);printf("%d \n",varX);return 0;}

调试宏命令

#include<stdio.h>#define DEBUG//打开调试模式,去掉就可以成为发布模式int main(){#ifdef DEBUGprintf("%s\n",__FUNCTION__);printf("%s\n",__FILE__);printf("%d\n",__LINE__);printf("%s\n",__DATE__);printf("%s\n",__TIME__);printf("%d\n",__STDC__);printf("%ld\n",__cplusplus);#endifprintf("hello world\n");return 0;}

如果不加 #define DEBUG相当于调试模式没有打开,通过gcc -D DEBUG main.c -o main 相当于程序中写了#define debug ,这就相当于一个开关量

#define language cpp#if language == cpp //不同语言添加不同头文件#define HDR <iostream>#elif language == c#define HDR <stdio>#endif#include HDRusing namespace std;#define swap(T,x,y) {T temp; temp = x; x = y; y = temp;}int main() {int a =1;int b =2;swap(int,a,b);cout << a << b << endl;return 0;}

#、##、__VA_ARGS__的使用_萧国浪子的博客-CSDN博客___va_args__使用打印信息的接口时,经常见到__VA_ARGS__和##__VA_ARGS__这两个字符串,花时间学习下这部分的知识,发现还有#和##这两个比较有意思的字符串,记下他们的用法:#: 用来把参数转换成字符串;例:#include <iostream>#define LOG(x) do { printf("%s=%d\n",#x,x); }while(0)int .../auccy/article/details/88833659

C语言宏定义,内置宏,__FILE__,__LINE__,## 用法_tomtc123的博客-CSDN博客当然宏定义非常重要的,它可以帮助我们防止出错,提高代码的可移植性和可读性等。下面列举一些成熟软件中常用得宏定义1,防止一个头文件被重复包含#ifndefCOMDEF_H#defineCOMDEF_H//头文件内容 …#endif2,重新定义一些类型,防止由于各种平台和编译器的不同,而产生的类型字节数差异,方便移植。typedefunsigne/tomtc123/article/details/8875468

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。