Linux下代码括号“{”和“}”的使用原则如下
(1)对于结构体、if/for/while/switch语句,“{”不另起一行,例如:
struct var_data{
int len;
char data[0];
};
if (a == b){
a = c;
d = a;
}
for (i = 0; i < 10; i++){
a = c;
d = a;
}(3)if和else混用的情况下,else语句不另起一行,例如:
if (a == y){
...
}else{
...
}switch{
case 'G':
case 'g':
mem <<= 30;
break;
case 'M':
case 'm':
mem <<= 20;
break;
case 'K':
case 'k':
mem <<= 10;
break;
default:
break;
}GNU C的扩展
1 零长度和变量长度数组
truct var_data {
int len;
char data[0];
}int main (int argc, char *argv[])
{
int i, n = argc;
double x[n];
for (i = 0; i < n; i++)
x[i] = i;
return 0;
}switch (ch) {
case '0'... '9': c -= '0';
break;
case 'a'... 'f': c -= 'a' - 10;
break;
case 'A'... 'F': c -= 'A' - 10;
break;
} GNU C把括号中的复合语句看做是一个表达式,称为语句表达式。
#define min_t(type, x, y) ({type __x = (x); type __y = (y); __x < __y ? __x: __y})typeof(x)可以获得x的类型,因此上面的定义可以变成:
#define min(x, y) ({ const typeof(x) _x = (x);const typeof(y) _y = (y);_x < _y ? x: y;})#define pr_debug(fmt, arg...) printf(fmt, ##arg)
#define SAFE_FREE(p) do{free(p); p = NULL;}while(0)
if (NULL != p)
SAFE_FREE(p);
else
...2)假设没有后面的else分支,则不论if的条件是否成立,p = NULL;都会执行。
原文地址:http://blog.csdn.net/it_liuwei/article/details/39346905