zoukankan      html  css  js  c++  java
  • c宏定义#define、#、##、__VA_ARGS__符号的理解

    c宏定义#define、#、##、__VA_ARGS__符号的理解

    #define 宏定义就是预处理时字符串替换的过程,不涉及内存分配,效率比较高,所以简单的表达式有些人就喜欢使用宏定义实现
    注意:宏定义只是替换操作,不进行任何逻辑运算操作,所以不涉及内存分配

    //例子1
    #define M 5 	//将M替换为5
    printf("结果 = %d\n", M) //编译器预处理后实际为 printf("结果 = %d\n", 5)
    
    //例子2
    #define SUM(a,b) a+b	//SUM和(a,b)之间不能用空格,不然就变为将SUM替换为(a,b) a+b了
    printf("结果 = %d\n", SUM(1,2)) //编译器预处理后实际为 printf("结果 = %d\n", 1+2)
    
    //例子3,有参宏定义时最好加上括号,以免替换后意思变了
    #define COUNT(M) M * M 
    printf("结果 = %d\n", COUNT(1+2)) //实际为 printf("结果 = %d\n", 1+2 * 1+2) 结果是5,而不是想要的9。。。
    
    //例4
    #define COUNT(M) ((M) * (M)) 
    printf("结果 = %d\n", COUNT(1+2)) //实际为 printf("结果 = %d\n", ((1+2) * (1+2)) ) 结果是9,是对的
    
    #define COUNT(M) ((M) * (M)) 
    //使用COUNT宏定义
    COUNT(10) 相当于
    #define SUM(a,b) a + b 
    

    #运算符 把参数字符串化,功能就是转为字符串

    //例子
    #define SUM(a, b) printf(#a " + " #b " = %d\n",((a) + (b)))
    调用 SUM(1 + 2, 3 + 4);
    相当于 printf("1 + 2" " + " "3 + 4" " = %d\n",((1 + 2) + (3 + 4))) //#a替换为"1 + 2",#b替换为"3 + 4"
    字符串本身具有链接功能,所以相当于 printf("1 + 2 + 3 + 4 = %d\n",(3 + 7))
    

    ##运算符 参数拼接粘合作用,所以称为“预处理器的粘合剂”

    //例子
    #define NAME(n) num ## n 
    int num0 = 100;		//必须定义num0变量,不然下面的NAME(0)调用时会提示找不到num0变量
    printf("num0 = %d\n", NAME(0)); //实际为 printf("num0 = %d\n", num0);  也就是NAME(0)替换为了num0
    

    __VA_ARGS__符号 可变参数列表,如果是多个参数,必须是最后一个参数,代表剩余的。

    //例子__VA_ARGS__表示前面的...所有参数,前面再加个#表示字符串化即转为字符串形式
    #define LOG(...) printf("log-->%s\n",#__VA_ARGS__)
    LOG("hello,world");	//打印出 log-->"hello,world"
    LOG(123);	//打印出 log-->123
    
    //例子2
    #define LOG(...) printf("log-->%s\n",#__VA_ARGS__)
    调用:LOG("hello,world");
    调用:LOG(123);
    
    #define LOG2(TAG, ...) printf(TAG"-->%s\n",#__VA_ARGS__)
    调用:LOG2("TAG", "hello,world"); //必须带引号"TAG",不能是TAG
    
    #define LOG3(TAG, ...) printf(#TAG"-->%s\n",#__VA_ARGS__)
    LOG3(TAG, 123); //不能带引号"TAG",只能是TAG当然也可以是其他的但是不能带引号,因为#define时会添加了#转为字符串
    

    小技巧:使用CLION或者开发工具,调用宏定义时,工具会出现Replacement的替换提示结果,我们可以工具提示查看替换后的结果是不是我们想要的

  • 相关阅读:
    韩式英语
    Daily dictation 听课笔记
    words with same pronunciation
    you will need to restart eclipse for the changes to take effect. would you like to restart now?
    glottal stop(britain fountain mountain)
    education 的发音
    第一次用Matlab 的lamada语句
    SVN的switch命令
    String的split
    SVN模型仓库中的资源从一个地方移动到另一个地方的办法(很久才解决)
  • 原文地址:https://www.cnblogs.com/yongfengnice/p/15768358.html
Copyright © 2011-2022 走看看