zoukankan      html  css  js  c++  java
  • c语言代码风格

    简要:主要介绍了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 = 0; i < 10; i++) // an example
                ;
            if (flag == true) {
                p = p->next;
                do_someting();
                break;
            } else
                do_something_else();
            p = p->next;
        }
        //...
        final_thing();
        return 0;
    }

    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 = 0; i < 10; i++) // an example
                            ;
                    if (flag == true) {
                            p = p->next;
                            do_someting();
                            break;
                    } else
                            do_something_else();
                    p = p->next;
            }
            //...
            final_thing();
            return 0;
    }

    2、Allman style(BSD style)

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

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

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

  • 相关阅读:
    centos7.6安装Oracle11g过程记录(下)
    centos7.6 安装解压缩软件
    centos7.6 安装及配置ftp服务
    MySQL8.0的主从配置过程记录
    解决 /dev/mapper/centos-root 空间不足的问题
    ASP判断当前页面上是否有参数ID传递过来
    通过ASP禁止指定IP和只允许指定IP访问网站的代码
    asp自动补全html标签自动闭合(正则表达式)
    asp中utf8不会出现乱码的写法
    通过安全字符串过滤非法字符
  • 原文地址:https://www.cnblogs.com/gongpixin/p/6608063.html
Copyright © 2011-2022 走看看