Michael D. Lowis

C Programming Guidelines

This is a suggested set of guidelines to follow when writing C code. It is based on my own experience as well as the influential "The Power of 10: Rules for Developing Safety-Critical Code".

  1. All control flow constructs must have braces
  2. Braces of control flow constructs go on their own lines
  3. 1 Avoid complex flow constructs, such as goto, setjmp/longjmp, and recursion.
  4. 1 All loops must have fixed bounds to avoid runaway code.
  5. 1 Avoid heap memory allocation after initialization.
  6. Minimize cyclomatic complexity and length of function bodies.
  7. Utilize design by contract on all public APIs and internal functions that are mission critical
  8. Restrict the scope of data to the smallest possible.
  9. Do not utilize function-scoped static variables.
  10. Check the return value of all non-void functions, or cast to void to indicate the return value is unused.
  11. Use the preprocessor sparingly.
  12. Limit pointer use to a single dereference, and do not use function pointers.
  13. Compile with all possible warnings active and warnings treated as errors.
  14. Use a single return statement per function.

  1. http://web.eecs.umich.edu/~imarkov/10rules.pdf