Skip to content

Latest commit

 

History

History
96 lines (78 loc) · 2.62 KB

code-style.org

File metadata and controls

96 lines (78 loc) · 2.62 KB

C/C++ Code Style

Rules

  • Use 4 spaces for indentation. Don’t use tabs.
  • Use 100-character column width.
  • Use snake_case for the names (not camelCase).
  • All macro names must be in UPPERCASE.
  • Use 1TBS (the one true brace style).
    int main(int argc, char *argv[])
    {
        if (argc > 2) {
            printf("too many args\n");
        } else {
            printf("too little args\n");
        }
    
        return 0;
    }
        

    Note brace position. The braces in control structures are required even for a single statement.

  • The pointer star must be aligned to the variable name:
    int *x;
        
  • Labels must be indented one level less than the normal indentation (except for case labels):
    int main(int argc, char *argv[])
    {
        switch (argc) {
            case 3:
                printf("hi\n");
    
            default:
                goto fail;
        }
        return 0;
    
    fail:
        return 1;
    }
        
  • Prefer double-indent instead of alignment in conditions and argument lists:
    int my_function(int param1, int param2,
            int param3, int param4)
    {
        if (param1 > 3 && param2 > 3 &&
                param3 > 3 && param4 > 3) {
            return param1 + param2 + param3 +
                param4;
        } else {
            return 0;
        }
    }
        
  • Don’t allow trailing spaces or lines.

Editor configurations

Emacs

(require 'whitespace)
(setq-default whitespace-style '(face
                                 tab-mark
                                 empty
                                 trailing
                                 lines-tail))
(global-whitespace-mode t)

(c-add-style "kaa"
             '("k&r"
               (whitespace-line-column . 100)
               (indent-tabs-mode . nil)
               (c-basic-offset . 4)
               (c-label-minimum-indentation . 0)
               (c-offsets-alist . ((case-label . +)
                                   (arglist-intro . ++)
                                   (arglist-cont-nonempty . ++)
                                   (inextern-lang . 0)))))

(setq c-default-style "kaa")