## 连接前后两个值
#define connect(a, b) a##b
int c = combom(1, 2);
//c = 12;
# 给后面的字符添加双引号
#define combom(b) #b
std::string c = combom(laji);
cout << c << endl;
##和#在一个宏语句中只能使用一个
#@给后面添加单引号
用##和#综合拼接字符串
#include <iostream>
#include "test.h"
#define A(a,b) a##b
#define g(a) #a
#define h(a) g(a)
int main(int argc, const char * argv[]) {
int a = A(1, 2);
std::string b = g(A(2,3));
std::string c = h(A(2,3));
std::string d = g(h(A(2,3)));
std::string e = h(g(h(A(2,3))));
std::string f = h(h(h(A(2,3))));
printf("a = %d\n",a);
printf("b = %s\n",b.c_str());
printf("c = %s\n",c.c_str());
printf("d = %s\n",d.c_str());
printf("e = %s\n",e.c_str());
printf("f = %s\n",f.c_str());
return 0;
}
/*
* a: A(1,2) ==> 12
* b: g(A(2,3)) ==> "A(2,3)" //如果遇到#开头直接“”替换,不管后面是什么
* c: h(A(2,3)) ==> g(A(2,3)) ==> g(23) ==> "23" //执行完外层的h()后不会马上执行产生的g(),而是先继续向里执行
* d: g(h(A(2,3))) ==> "h(A(2,3))" //如果遇到#开头直接“”替换,不管后面是什么
* e: h(g(h(A(2,3)))) ==> "g(g(h(A(2,3))))" ==> g("h(A(2,3))") ==> ""h(A(2,3))""
* f: h(h(h(A(2,3)))) ==> g(h(h(A(2,3)))) ==> g(g(h(A(2,3)))) ==> g(g(g(A(2,3)))) ==> g(g(g(23))) ==> g(g("23")) ==> g(""23"") ==> """23""" //先从外往里,再从里往外
*/
//结果如下
a = 12
b = A(2,3)
c = 23
d = h(A(2,3))
e = "h(A(2,3))"
f = "\"23\""
#ifdef DEBUG
调试时的添加打印的用法
#include <iostream>
#undef TEST_DEBUG
#define TEST_DEBUG
int main(int argc, const char * argv[]) {
using std::cout;
using std::endl;
#ifdef TEST_DEBUG
cout << "debug" << endl;
#endif
#ifndef TEST_DEBUG
cout << "no debug" << endl;
#endif
return 0;
}
#if #elif #else #endif
#include <iostream>
#define TEST 3
int main(int argc, const char * argv[]) {
using std::cout;
using std::endl;
#if TEST == 2
cout << "test = " << 2 << endl;
#elif TEST == 3
cout << "test = " << 3 << endl;
#else
cout << "test = " << 100 << endl;
#endif
return 0;
}
内置宏
int main(int argc, const char * argv[]) {
using std::cout;
using std::endl;
printf("data = %s\n",__DATE__); //编译时的日期
printf("time = %s\n",__TIME__); //编译时的时间
printf("fun = %s\n",__FUNCTION__); //函数名
printf("line = %d\n",__LINE__); //所在行数
printf("line = %s\n",__FILE__); //文件名
return 0;
}
#error会在编译时打印语句,用xcode时,会在编写时就直接提示。