简要:主要介绍了K&R风格和Allman(BSD)风格之间的缩进大小和大括号位置等区别

关于其它的代码风格,详见:Indent style - Wikipedia

1、K&R style

When following K&R, each function has its opening brace at the next line on the same indent level as its header, the statements within the braces are indented, and the closing brace at the end is on the same indent level as the header of the function at a line of its own. The blocks inside a function, however, have their opening braces at the same line as their respective control statements; closing braces remain in a line of their own, unless followed by a keyword else or while.

int main(int argc, char *argv[])
{
int i; // //...
while (p != NULL) {
for (i = ; i < ; i++) // an example
;
if (flag == true) {
p = p->next;
do_someting();
break;
} else
do_something_else();
p = p->next;
}
//...
final_thing();
return ;
}

1.1、1TBS(OTBS) style,全称:the one true brace style

与K&R style的区别是:单语句的括号不省略。

    //...
if (flag == true) {
do_someting();
} else {
do_something_else();
}

1.2、Linux kernel style

与K&R style的区别是:缩进为8格。

int main(int argc, char *argv[])
{
int i; // //...
while (p != NULL) {
for (i = ; i < ; i++) // an example
;
if (flag == true) {
p = p->next;
do_someting();
break;
} else
do_something_else();
p = p->next;
}
//...
final_thing();
return ;
}

2、Allman style(BSD style)

与K&R style的区别是:大括号单独占一行。

int main(int argc, char *argv[])
{
int i; // //...
while (p != NULL)
{
for (i = ; i < ; i++) // an example
;
if (flag == true)
{
p = p->next;
do_someting();
break;
}
else
do_something_else();
p = p->next;
}
//...
final_thing();
return ;
}

以后首选K&R风格,其次Allman(BSD)风格,养成一个好习惯。

最新文章

  1. 3.raid基础应用
  2. E:“图片视频”的列表页(taxonomy-cat_media.php)
  3. 【转】使用SQL Tuning Advisor STA优化SQL
  4. 【PHP发展史】PHP5.2 到 PHP5.6 中新增的功能详解
  5. 浅谈大型web系统架构
  6. Pureftpd
  7. HttpClient 教程 (A)
  8. C#多线程下载一个文件
  9. 作为平台的Windows PowerShell(一)
  10. Frank自动化测试
  11. scrollTop 值为 0
  12. [LeetCode] 203. Remove Linked List Elements 解题思路
  13. asp.net使用unescape读取js escape编码过的字符串
  14. 【java线程池】
  15. eclipse、myeclipse写类时,自动生成注释
  16. 几个常见的Mysql索引问题
  17. 实现textview竖排文字效果
  18. 《算法》第五章部分程序 part 6
  19. POJ3087:Shuffle&#39;m Up(模拟)
  20. 创建/删除Cookie数据

热门文章

  1. Spring读书笔记-----Spring核心机制:依赖注入
  2. java面试的那些事
  3. ios cocoapods获取不到最新库的原因主要有两个:
  4. xcode ERROR ITMS
  5. 下周要搞大事情(ASP.NET Core &amp; WebForms)!
  6. 九度OJ 1343:城际公路网 (最小生成树)
  7. mac安装iterm2
  8. Netty聊天室(2):从0开始实战100w级流量应用
  9. php自定义函数: amr转mp3格式
  10. vue表单输入的绑定