18 # 和 ##

1 # 运算符

  • # 运算符用于在预处理期将宏参数转换为字符串

  • # 的转换作用是在预处理期完成的,因此只在宏定义中有效

  • 编译器不知道# 的转换作用

  • 用法

    #include <stdio.h>
    
    #define STRING(x) #x
    
    int main()
    {
        
        printf("%s
    ", STRING(Hello world!));
        printf("%s
    ", STRING(100));
        printf("%s
    ", STRING(while));
        printf("%s
    ", STRING(return));
    
        return 0;
    }
    
    //输出结果
    Hello world!
    100
    while
    return
    
  • 示例

    #include <stdio.h>
    
    //动态打印被调用函数的函数名
    #define CALL(f, p) (printf("Call function %s
    ", #f), f(p))
       
    int square(int n)
    {
        return n * n;
    }
    
    int func(int x)
    {
        return x;
    }
    
    int main()
    {
        int result = 0;
        
        result = CALL(square, 4);
        
        printf("result = %d
    ", result);
        
        result = CALL(func, 10);
        
        printf("result = %d
    ", result);
    
        return 0;
    }
    
    //运行结果
    Call function square
    result = 16
    Call function func
    result = 10
    
    • 单步编译:gcc -E test.c -o test.i

      int square(int n)
      {
          return n * n;
      }
      
      int func(int x)
      {
          return x;
      }
      
      int main()
      {
          int result = 0;
          
          result = (printf("Call function %s
      ","square"),square(4));
          
          printf("result = %d
      ", result);
          
          result = (printf("Call function %s
      ","func"),func(10));
          
          printf("result = %d
      ", result);
      
          return 0;
      }
      

2 ## 运算符

  • ## 运算符用于在预处理期粘贴两个标识符

  • ## 的连接作用是在预处理期完成的,因此只在宏中有效

  • 编译器不知道## 的连接作用

  • 用法

    #define CONNECT(a,b) a##b
    int CONNECT(a,1);  //int a1;
    a1 = 2;
    
  • 示例1

    #include <stdio.h>
    
    #define NAME(n) name##n
    
    int main()
    {
        
        int NAME(1);
        int NAME(2);
        
        NAME(1) = 1;
        NAME(2) = 2;
        
        printf("%d
    ", NAME(1));
        printf("%d
    ", NAME(2));
    
        return 0;
    }
    //输出结果
    NAME1 = 1
    NAME2 = 2
    
  • 示例2

    • 解决定义完结构体后,每次生成一个结构体变量都需要使用 struct 关键字的问题
    #include <stdio.h>
    
    #define STRUCT(type) typedef struct _tag_##type type;
                         struct _tag_##type
    
    STRUCT(Student)
    {
        char* name;
        int id;
    };
    
    int main()
    {
        
        Student s1;
        Student s2;
        
        s1.name = "s1";
        s1.id = 0;
        
        s2.name = "s2";
        s2.id = 1;
        
        printf("s1.name = %s
    ", s1.name);
        printf("s1.id = %d
    ", s1.id);
        printf("s2.name = %s
    ", s2.name);
        printf("s2.id = %d
    ", s2.id);
    
        return 0;
    }
    
原文地址:https://www.cnblogs.com/bky-hbq/p/13646513.html