2013-04-13 21 views
3

我給出了一個例子來說明我在第6行中的意思是什麼樣的作業風格。這樣的分配風格是否有明確的定義?

1 #include<stdio.h> 
    2 
    3 int main(int argc,char *argv[]) 
    4 { 
    5   int a,b,c; 
    6   c = ({ a=5; b = a+1;}); 
    7   printf("%d\n%d\n%d\n",a,b,c); 
    8   return 0; 
    9 } 

我不知道什麼{} is.It不是初始化數組int arr[]={1,2,3}使用的列表。


更新: 可能使用這種方法,我可以定義功能在GCC的功能或者一個錯誤(版本4.7.2(Ubuntu的/ Linaro的4.7.2-2ubuntu1))

1 #include<stdio.h> 
    2 #include<math.h> 
    3 int main(int argc,char *argv[]) 
    4 { 
    5   int a,b; 
    6   b = ({int cos(i){return 0;};a = 0;cos(a);}); 
    7   printf("%d\n%d\n",a,b); 
    8   b = cos(0); 
    9   printf("%d\n%d\n",a,b); 
10   return 0; 
11 } 

輸出:

0 
0 
0 
1 
+0

http://stackoverflow.com/questions/1635549/in-what-versions-of-c-is-a-block-inside-parenthesis-used-to-return-a-value-valid – DCoder

+0

也許gcc - 迂腐 - 牆壁是我的不錯選擇 – yuan

回答

3
({ a=5; b = a+1;}) 

是GNU擴展,expression statement。這不是標準C.

塊中的語句被執行,塊中最後一個表達式的值是表達式語句的值。

所以

c = ({ a=5; b = a+1;}); 

a爲5,則到ba+1(6),和c到該值。


關於更新,

b = ({int cos(i){return 0;};a = 0;cos(a);}); 

使用另一個GNU擴展附加地,nested functions。在表達式語句的複合語句中,定義了一個嵌套函數cos,它隱藏了在math.h中聲明的名稱cos,因此作爲複合語句中最後一個表達式的cos(a)調用了嵌套的本地定義。

在第8行中,嵌套功能當然是在範圍的,所以

b = cos(0); 

調用從math.h之一。

+1

謝謝,@Alex的鏈接。我的google-fu太慢了。 –

相關問題