簡體   English   中英

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

[英]what is the purpose of “(void) ( { CODE } )” in c?

在生成的c代碼中,我發現了類似這樣的內容(已編輯):

#include <stdio.h>

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. e.g.

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM