在开源代码中看到,宏定义经常这样用
变为
这样直接加个花括号不久行了,为什么还用do......while()?假如加上花括号
变为
- #define some()
- do {
- do_somt_thing();
- } while (0)
为什么这样用?
可以试一下,假如一个普通宏定义
- #define some(x) Fun1(x);Fun2(x)
- if(condition)
- some(x);
变为
- if(condition)
- Fun1(x);
- Fun2(x);
这样直接加个花括号不久行了,为什么还用do......while()?假如加上花括号
- #define some(x) {Fun1(x);Fun2(x);}
- if(condition)
- some(x);
- else
- someelse(x);
变为
- if(condition)
- {
- Fun1(x);
- Fun2(x);
- };//多了个分号
- else
- someelse(x);