zoukankan      html  css  js  c++  java
  • C语言--#、##、__VA_ARGS__ 和##__VA_ARGS__ 的使用

    #  用来把参数转换成字符

    1. #include <stdio.h>
    2.  
    3. #define FUN(X) (printf("%s=%d ",#X,X)) /* #用来把参数转换成字符 */
    4.  
    5. int test(int argc, char ** argv)
    6. {
    7. int a = 1;
    8. int b = 2;
    9.  
    10. FUN(a);
    11. FUN(b);
    12. FUN(a+b);
    13.  
    14. return 0;
    15. }
    16. /** 程序输出结果:
    17. *******************************
    18. a=1
    19. b=2
    20. a+b=3
    21. *******************************
    22. */
    23.  

    2、## 把两个语言符号组合成单个语言符号

    1. #include <stdio.h>
    2.  
    3. #define XNAME(n) x##n /* ## 这个运算符把两个语言符号组合成单个语言符号*/
    4. #define PXN(n) printf("x"#n" = %d ",x##n)
    5.  
    6. int main(int argc, char ** argv)
    7. {
    8. int XNAME(1) = 10886; /*宏展开就是:x1 = 10086*/
    9.  
    10. PXN(1); /*宏展开就是:printf("x1 = %d ",x1) */
    11.  
    12. return 0;
    13. }
    14.  
    15. /** 程序输出结果:
    16. *******************************
    17. x1 = 10886
    18. *******************************
    19. */
    20.  
    21.  

    3、__VA_ARGS__ 和 ##__VA_ARGS__

    1. #include "stdio.h"
    2.  
    3. #define DEBUG1(format, ...) do{
    4. printf(format, __VA_ARGS__);
    5. } while(0)
    6.  
    7. #define DEBUG2(format, args...) do{
    8. printf(format, ##args);
    9. } while(0)
    10.  
    11. #define DEBUG3(format, ...) do{
    12. printf(format, ##__VA_ARGS__);
    13. } while(0)
    14.  
    15. int
    16. main(int argc, char **argv)
    17. {
    18. printf("hello world.1 ");
    19.  
    20. //DEBUG1("hello world.2 ");//错误 参数为零
    21. DEBUG1("hello world.2 %d %d ", 1, 2);
    22.  
    23. DEBUG2("hello world.3 ");
    24. DEBUG2("hello world.3 %d %d %d ", 1, 2, 3);
    25.  
    26. DEBUG3("hello world.4 ");
    27. DEBUG3("hello world.4 %d %d %d %d ", 1, 2, 3, 4);
    28.  
    29. return 0;
    30. }

    应用:

    1. #include "stdio.h"
    2.  
    3. #define DEBUG_ON
    4.  
    5. #ifdef DEBUG_ON
    6. #define DEBUG(format, ...) do{
    7. printf("File:%s, Line:%d, "format"", __FILE__, __LINE__, ##__VA_ARGS__);
    8. } while(0)
    9. #else
    10. #define DEBUG(format, ...)
    11. #endif
    12.  
    13.  
    14. int
    15. main(int argc, char **argv)
    16. {
    17.  
    18. DEBUG("hello world %d %d ", 1, 2);
    19.  
    20. return 0;
    21. }
    22.  
    23. /**程序输出结果:
    24. ***********************************************************
    25. File:E:C Languageprintf.c, Line:19, hello world 1 2
    26. ***********************************************************
    27. */

    1、#用来把参数转换成字符.

    2、##这个运算符把两个语言符号组合成单个语言符号

    3、 __VA_ARGS__ 是一个可变参数的宏,实现思想就是宏定义中参数列表的最后一个参数为省略号(也就是三个点)。

    4、##__VA_ARGS__ 宏前面加上##的作用在于,当可变参数的个数为0时,这里的##起到把前面多余的","去掉的作用,否则会编译出错。

    5、注意宏定义连接符    后面不要有任何操作,直接回车,下一行的前面可以有空格。

    ======================================================-=============================================

    为更好了解C/C++中可变参数的知识,我从网上摘录了两篇文章,算是自己的一个总结。本篇主要是关于“## __VA_ARGS__”宏的介绍和使用。

     

  • 相关阅读:
    Quick Sort
    Binary Search
    trollcave解题
    Openvas简介
    SMB扫描-Server Message Block 协议、nmap
    漏洞基本概念
    防火墙识别、负载均衡识别、waf识别
    Centos7下部署Python项目
    Python-Redis数据类型操作
    MySQL的事务隔离级别
  • 原文地址:https://www.cnblogs.com/zhoug2020/p/13549615.html
Copyright © 2011-2022 走看看