Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
C Style Guide

This page contains guidelines for writing new C source code for the Paparazzi project.

If you are using Eclipse, you can use Eclipse code style located in the repository (paparazzi_code_profile_eclipse.xml)

Formatting Guide

  • remove any trailing white space at the end of lines.
  • use 2 spaces for indentation; do NOT use tabs.
  • use Unix line endings ('\n'); do NOT use DOS endings ('\r\n')
  • limit code length to 100 chars per line
  • limit adjacent empty lines to at most two (2).
  • remove any trailing empty lines at the end of source files
  • do not "comment out" code from the tree; instead, one should either:
    1. remove it entirely (git can retrieve the old version), or
    2. use an #if/#endif block.

Naming Rules

  • most identifiers must use lower-case letters (and digits) only.
    • macros should use upper-case letters (and digits) only.

Type Guidelines

  • use the types from <inttypes.h>:
    • int8_t, int16_t, int32_t, or int64_t: signed types of specified size
    • uint8_t, uint16_t, uint32_t, or uint64_t: unsigned types of specified size

Functions

  • static inline functions should be prefered over macros in most cases:
    /* do NOT define macro-like functions like this... */
    #define CUBE(x) ((x) * (x) * (x))
    /* instead, define the same expression using a C99 inline function */
    static inline int cube(int x) { return x * x * x; }
  • Functions should be declared static unless required by other modules
    • define static functions before first usage to avoid forward declarations.
  • Functions should have no space between its name and its parameter list:
    ...
    int32_t y = f(x1, x2 - x1);
    ...
    }
    int int32_t
    Typedef defining 32 bit int type.
    Definition: vl53l1_types.h:83
    uint16_t f
    Camera baseline, in meters (i.e. horizontal distance between the two cameras of the stereo setup)
    Definition: wedgebug.c:204

Switch statements

  • specify a default case
  • prefer an enum over defines for the different states
    enum state {
    STATE_FOO = 1,
    STATE_BAR = 2
    };
    switch (state) {
    case STATE_FOO:
    foo();
    break;
    case STATE_BAR:
    bar();
    break;
    default:
    break;
    }
    struct State state
    Definition: state.c:36
    uint16_t foo
    Definition: main_demo5.c:58

If statements

  • return statement on the new line even for ""simple if"" case
    if (TRUE) {
    return 1;
    }
    if (x < other.x) {
    return -1;
    }
    else if (x > other.x) {
    return 1;
    }
    else {
    return 0;
    }
    #define TRUE
    Definition: std.h:4

Preprocessor directives

  • For conditional compilation use #if instead of #ifdef. Someone might write code like:
    #ifdef USE_FOO
    bar();
    #endif
    Someone else might compile the code with turned-off foo info like:
     gcc stuff.c -DUSE_FOO=0
    
    So use #if not #ifdef to turn a feature on or off. This works fine, and does the right thing, even if USE_FOO is not defined at all (!)
    #if USE_FOO
    bar();
    #endif
  • If you really need to test whether a symbol is defined or not, test it with the defined construct.