欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

Linux C 中连接操作符##

发布时间:2025/3/13 30 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Linux C 中连接操作符## 小编觉得挺不错的,现在分享给大家,帮大家做个参考.
Linux C 中连接操作符## #include <stdio.h> #define test(x) test ## x #define DPRINT( fmt, args...) \ { \printf("File : %s Funtion : %s Line : %d \t", __FILE__, __FUNCTION__, __LINE__ );\printf( fmt, ##args );\ }void test1(int a) {DPRINT("Integer : %d \n", a); }void test2(char *s) {DPRINT("String : %s \n", s); }int main(void) {test(1)(100);test(2)("hello");return 0; }

打印信息:

***************************************************

 File : test.c Funtion : test1 Line : 11         Integer : 100
 File : test.c Funtion : test2 Line : 16         String : hello

***************************************************

 

#define DPRINT( fmt, args...) \
{ \
printf("File : %s Funtion : %s Line : %d  \t", __FILE__, __FUNCTION__, __LINE__ );\
printf( fmt, ##args );\
}

这样定义宏有个问题, 标准printf()函数有返回值, 只是我们很少用

另外一种定义:

#define DPRINT( fmt, args...)     \
  printf("File : %s Funtion : %s Line : %d  \t"fmt, __FILE__, __FUNCTION__, __LINE__ ,##args )

fmt不能为指针

*****************************************

const  char *s= "string";

printf(s);

*****************************************

是合法的,可以打印出string

但DPRINT(s)就不合法

posted on 2012-02-02 10:19 Neddy11 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/Neddy/archive/2012/02/02/2335399.html

总结

以上是生活随笔为你收集整理的Linux C 中连接操作符##的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。