GNU C與ANSI C的不同

來源:互聯網
上載者:User

標籤:

引用:http://tsroad.lofter.com/post/376316_57ac519

1.GNU C可定義0長度數組,目的是為了定義可變長結構體。 
           

struct var_struct
{
   int len;
   char data[0];
};

把此結構體的資料緊接著放後面,就可以用data[index]來訪問了。 

2.case可以範圍匹配,case x...y 

switch(n)
{
   case 0...9 c=1;break;
   case 10...11 c=2;break;
   ...
}


3.GNU C把大括弧中的語句看成一個運算式,可以出現在任何錶達式能出現的地方 

#define int_min(x,y) \
 {int __x=(x);int __y=(y);__x<__y?__x:__y;}


4.typeof關鍵字,可以得到變數的類型 

int x;
typeof(x) y;


5.支援可變參的宏 

#define dprint(fmt,arg...) \
printk(fmt,##arg)


6.當前函數名,使用宏__FUNCTION__ 

7.在初始化結構體或數組時,通過索引和結構體名,允許初始化值以任意順序出現。 

int s[5]={[0 ... 3]=3,[4]=1};

注意[0空格...空格3] 

struct x
{
   int a;
   char b;
};
struct x y=
{
   .b=‘h‘,
   .a=10,
};



8.允許聲明函數,變數,類型的特殊屬性,只要在後面加上__atribute__((屬性)) 

#define ATTRI_NORETURN __attribute__((noreturn))

void exit() ATTRI_NORETURN
{
  ...
}


9.GNU C提供了大量的內建函數 

10.用do ... while(0)來定義宏,這樣做是為了方便在宏後面加;號 

#define XXX do{free(p);p=NULL;}while(0)


11.經常用goto語句進行錯誤處理。 

以上內容來自《Linux裝置驅動開發詳解》。

 

GNU C與ANSI C的不同

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.