C++/C 宏定义(define)中# ## 的含义(转)
来源:互联网 发布:mac更改桌面图标大小 编辑:程序博客网 时间:2024/06/03 02:18
参考:http://www.cnblogs.com/little-ant/p/3463080.html
http://hi.baidu.com/kiraversace/item/1148ee057147981a4ac4a3e9
C++/C 宏定义(define)中# ## 的含义
define 中的# ## 一般是用来拼接字符串的,但是实际使用过程中,有哪些细微的差别呢,我们通过几个例子来看看。
#是字符串化的意思,出现在宏定义中的#是把跟在后面的参数转成一个字符串;
#define ENUM_to_String(enum_id) #enum_id
eg:
#define strcpy__(dst, src) strcpy(dst, #src)
strcpy__(buff,abc) 相当于 strcpy__(buff,“abc”)
##是连接符号,把参数连接在一起
#define FUN(arg) my##arg
则 FUN(ABC)
等价于 myABC
再看一个具体的例子
#include <iostream>
using
namespace
std;
#define OUTPUT(A) cout<<#A<<":"<<(A)<<endl;
int
main()
{
int
a=1,b=2;
OUTPUT(a);
OUTPUT(b);
OUTPUT(a+b);
return
1;
}
<iframe id="iframe_0.24500557948733026" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://a.hiphotos.baidu.com/album/pic/item/2fdda3cc7cd98d1081fdc3df213fb80e7bec9037.jpg?_=3463080%22%20style=%22border:none;max-width:999px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.24500557948733026',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 991px; height: 660px;"></iframe>
去掉#号我们得到这样的结果,直接把a,b的值打印出来了,这是符合语法规则的,所以#的作用显而易见。
#include <iostream>
using
namespace
std;
#define OUTPUT(A) cout<<A<<":"<<(A)<<endl;
int
main()
{
int
a=1,b=2;
OUTPUT(a);
OUTPUT(b);
OUTPUT(a+b);
return
1;
}
<iframe id="iframe_0.5359436411709109" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://a.hiphotos.baidu.com/album/pic/item/2cf5e0fe9925bc31fc2ace765edf8db1ca1370ed.jpg?_=3463080%22%20style=%22border:none;max-width:999px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.5359436411709109',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 958px; height: 602px;"></iframe>
- C++/C 宏定义(define)中#和## 的含义
- C++/C 宏定义(define)中# ## 的含义
- C++/C 宏定义(define)中# ## 的含义
- C++/C 宏定义(define)中# ## 的含义(转)
- C++/C 宏定义(define)中# ## 的含义(转)
- C++/C 宏定义(define)中# ## 的含义 宏拼接
- C宏定义中## 和# 的含义
- C语言#define宏中省略号的含义?
- C语言#define宏中省略号的含义
- C语言中宏定义(#define)时do{...}while(0)的价值
- C++/C宏定义中##与#的含义
- C宏定义#define
- C宏定义# ##含义
- C语言宏定义中UL的含义
- LINUX C中用define定义可变参数的宏 (转)
- C语言之#define(宏定义命令)
- C语言-#define宏定义
- C-049.宏定义#define
- I/O多路复用select、poll、epoll的区别使用
- 百度地图测加载的两种 方式 直接加载和异步加载
- iOS 10 消息推送(UserNotifications)秘籍总结(二)
- 郁闷的C小加(三)
- spring中自定义属性编辑器CustomEditorConfigurer
- C++/C 宏定义(define)中# ## 的含义(转)
- ios 10进制转16进制
- Java Web项目中文乱码问题及解决方案剖析
- 【数据结构】算法2.8-2.122 线性表的单链表
- nginx
- 面试题总结(二)
- Cordova 搭建IOS开发环境-console日志调试
- Jdbc入门介绍第二章——Jdbc结合JSP和Servlet的简单综合应用
- Spring源码解析---spring源码下载导入eclipse