在C源代码一些官方的代码和Liunx中内核中会很多的宏定义中使用了do {…} while(0)这样的语法,当时觉得很奇怪,感觉这样的语法并没有意义,后来在网上查了相关的资料才发现它的微妙之处。
假如我们有这样一个例子:
#define FUNC(x) func1(x);func2(x)
int main()
{
int x = 0;
FUNC(x);
}
那么在编译的预处理的过程中,编译器会对宏进行展开:
int main()
{
int x = 0;
func1(x);func2(x);
}
这样程序是没有任何问题的。如果程序是这样调用宏的呢?
#define FUNC(x) func1(x);func2(x);
int main()
{
int x = 0;
if (test(x))
FUNC(x);
}
对宏进行展开:
int main()
{
int x = 0;
if (test(x))
func1(x);
func2(x);
}
那么这样展开后,无论test(x)是否为true,程序都会执行func2(x),这就有可能会出现问题了。其实我们的目的是如果test(x)为true就执行func1(x)和func2(x),否则都不执行。
宏定义加上do(…)while(0)就可以解决这个问题了:
#define FUNC(x) do (func1(x);func2(x);) while(0)
int main()
{
int x = 0;
if (test(x))
FUNC(x);
}
对宏进行展开:
int main()
{
int x = 0;
if (test(x))
do (func1(x);func2(x);) while(0)
}
这样程序就能按照我们的想法运行了。
概括来说,宏定义中使用do(…)while(0)可以使得宏能够不受大括号,分号等的影响,总是能够按照预期的方式运行。