Paparazzi UAS  v5.0.5_stable-7-g4b8bbb7
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
C Style Guide

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

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 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);
    ...
    }

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;
    }

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.