Linux內核編碼風格在內核源代碼的Documentation/CodingStyle目錄下(新版本內核在Documentation/process/coding-style.rst)。函數
變量命名採用下劃線分割單詞,如:oop
int min_value; void send_data(void);
switch和case對其,即case前不縮進,如:this
switch (suffix) { case 'G': case 'g': mem <<= 30; break; case 'M': case 'm': mem <<= 20; break; case 'K': case 'k': mem <<= 10; /* fall through */ default: break; }
不要把多個語句放一行,如:編碼
if (condition) do_this; // 不推薦 if (condition) do_this; // 推薦
一行不要超過80個字符,字符串實在太長,請這樣:code
void fun(int a, int b, int c) { if (condition) printk(KERN_WARNING "Warning this is a long printk with " "3 parameters a: %u b: %u " "c: %u \n", a, b, c); else next_statement; }
代碼使用K&R風格,非函數的花括號不另起一行,函數花括號另起一行,例如blog
if (a == b) { a = c; d = a; } int func(int x) { ; // statements }
do...while語句中的while和if...else語句中的else,跟「}」一行,如:字符串
do { body of do-loop } while (condition); if (x == y) { ... } else if (x > y) { ... } else { .... }
只有一條語句時不加「{ }」,但若是其餘分支有多條語句,請給每一個分支加「{}」,如:it
if (condition) action(); if (condition) do_this(); else do_that(); if (condition) { do_this(); do_that(); } else { otherwise(); } while (condition) { if (test) do_something(); }
if, switch, case, for, do, while
後加一個空格函數長度不要超過48行(除非函數中的switch有不少case);函數局部變量不要超過10個;函數與函數之間要空一行io