c中“(void)({CODE})”的目的是什么?

在生成的c代码中,我找到了类似这样的内容(已编辑):

#include  int main() { (void) ( { int i = 1; int y = 2; printf("%d %d\n", i,y); } ); return 0; } 

我相信我以前从未见过构造(void) ( { CODE } ) ,也无法弄清楚目的是什么。

那么,这个结构有什么作用呢?

({ })是一个名为语句表达式gcc扩展。

http://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html

语句表达式产生一个值,并且(void)强制转换可能在此处删除编译器警告或明确表示未使用语句表达式的值。

Now (void) ({ })与简单的复合语句{}相同,没有使用它的意义。

({ })一个应用是用代码块替换表达式的能力。 通过这种方式,可以将非常复杂的宏嵌入到表达式中。

 #define myfunc() { } // can be a typical way to automatize coding. eg myfunc(x,y,z); myfunc(y,x,z); myfunc(x,z,y); // would work to eg. unroll a loop int a = myfunc()*123; // but this wouldn't work 

代替

 #define myfunc(a,b,c) ({printf(a#b#c);}) int a= myfunc(a,b,c) * 3; // would be legal