90.#define高级用法


  • define把参数变成字符串
    1 #define f(x)  printf("%s",#x);

     


  • define连接两个字符串
    1 #define a(x) a##x

     


  • define把参数变成字符
    1 #define  N(x)   #@x

     


  • 取消#define的作用域
    1 #undef  MN  //取消define的作用域

     


  • define内联函数
    1 #define run() {puts("锄禾日当午1") ;\
    2              puts("锄禾日当午2");\
    3          puts("锄禾日当午3");}

     


  • define定义assert
  •  assert用法
    1 assert(a = 0);

     

  • 宏定义assert
    1 #define ASSERT(x)  \
    2        if((x))\
    3        {       \
    4          printf("ASSERT %s 代码出错\n",#x)  ;     \
    5          printf("文件%s %d行\n",__FILE__,__LINE__)  ;           \
    6            printf("函数%s \n",__FUNCTION__)  ;           \
    7          abort(), getchar();\
    8          return ;   \
    9        }

  • #ifdef,#error用法
    1 #ifdef  N
    2 #error 错误,不能定义N
    3 #endif

     


  • #if用法
    1 #if num==1
    2 #error 小伙子你的除数不要写为0,写了哥会溢出的
    3 #endif

     

  • 内置宏定义__LINE__,__FILE__,__DATE__,__TIME__,__FUNCTION__
     1 #define _CRT_SECURE_NO_WARNINGS
     2 #include<stdio.h>
     3 #include<stdlib.h>
     4 #line  1
     5 void main() //计数,从这一行开始
     6 {
     7     //改变行数,某一段到某一段一共多少行
     8     printf("%d\n", __LINE__);
     9     char path[100];
    10     //获取当前文件地址
    11     sprintf(path, "%s", __FILE__);
    12     printf("%s", path);
    13     getchar();
    14 }
    1 printf("代码编译时间 %s %s\n", __DATE__,  __TIME__);
    1 printf("函数名%s\n", __FUNCTION__);

     

 

  • 条件编译 #if   #else   #endif
     1 #include<stdio.h>
     2 #define findWC 10
     3 void main()
     4 {
     5 #if findWC==1
     6     printf("");
     7 #else
     8     printf("");
     9 #endif
    10     getchar();
    11 
    12 }

    #if   #elif   #endif

     1 // 'b'北京  's'四川, h 菏泽  a美帝
     2 #define  manspeak  'b'
     3 
     4 void main3()
     5 {
     6 
     7 #if   manspeak=='b'
     8     puts(" love  you 1314");
     9 #elif  manspeak=='h'
    10     puts("俺很稀罕你");
    11 #elif  manspeak=='a'
    12     puts("i love you for ever");
    13 #elif  manspeak=='s'
    14     puts("老子喜欢你,不喜欢我,你仙人板板的");
    15 #endif
    16 
    17 }

    #ifdef #ifndef

    #ifdef Debug
        printf("love you");
    #endif
    
    #ifndef test
        printf("没有定义test");
    #endif

     

posted @ 2018-02-23 12:09  喵小喵~  阅读(616)  评论(0编辑  收藏  举报