一。#define的用法
1.简单的define定义
#define MAXTIME 1000
2.define的“函数定义”
define可以像函数那样接受一些参数,如下
#define max(x,y) (x)>(y)?(x):(y);
3.宏的单行定义
#define A(x) T_##x
#define B(x) #@x
#define C(x) #x
我们假设:x=1,则有:
A(1)------)T_1
B(1)------)'1'
C(1)------)"1"
4.define的多行定义
define可以替代多行的代码,例如MFC中的宏定义(非常的经典,虽然让人看了恶心)
#define MACRO(arg1, arg2) do { \
/* declarations */ \
stmt1; \
stmt2; \
/* ... */ \
} while(0) /* (no trailing ; ) */
关键是要在每一个换行的时候加上一个"\"
5.取消宏
#undef [MacroName]
6.条件编译
#ifdef XXX…(#else) …#endif
7宏中"#"、“#@”和"##"的用法
用# 把宏参数变为一个字符串,
用# 把宏参数变为一个字符串,
用#@把宏参数变为一字符,
用## 把两个宏参数连接起来。
例:
#define STR(s) #s
#dedine TOCHAR(ch) #@ch