2013-10-16 76 views
7

在我的C語言實踐中,我面臨着一個表達式,然後我把它簡化爲:如何解釋這個表達式「int a =({10;});」在C語言中?

int a=({10;}); 

這是一個合法的表達,因爲它得到過去的gcc編譯器。 請關注此零件:({10;})。有人能解釋嗎?越詳細越好。謝謝!

+1

我幾乎肯定這是一個gcc擴展,而不是一個合法的C代碼。不過,我很想知道它是什麼。 – templatetypedef

+0

試試這也'int a = [10];' –

+2

意義已被解釋(這是一個海灣合作委員會擴展名),但它更清晰和可移植的書面作爲'int a = 10;' –

回答

7

這是一個聲明表達式。這是一個gcc extension並根據文檔6.1 Statements and Declarations in Expressions

The last thing in the compound statement should be an expression followed by a semicolon; the value of this subexpression serves as the value of the entire construct.

所以對於這一段代碼:

int a=({10;}); 

根據這些規則的值將是10其將被分配給a

該擴展是一個many gcc extensions used in the Linux kernel,雖然鏈接的文章實際上並不涵蓋語句表達式,這kernel newbies FAQ entry解釋了背後使用語句表達式在Linux內核中的推理。

由於GCC文件筆記與-pedantic選項編譯會提醒您,當您使用GCC擴展

1

這不是標準的C,而是GCC的擴展名爲statement expression。用括號括起來的複合語句可以作爲表達式出現。

The last thing in the compound statement should be an expression followed by a semicolon; the value of this subexpression serves as the value of the entire construct.

回在你的榜樣:

int a=({10;}); 

{10;}作爲複合語句表達,所以a10值。