next up previous
Next: Compiling Up: intro_C Previous: intro_C

Basic Rules

C can be as structured or as unstructured as you like, but it's best to follow a few simple rules:

  1. Stick to one statement per line.
  2. Indent appropriately to show nested logic.
  3. Use descriptive variable and function names.
  4. Break large tasks into smaller tasks using functions.
  5. Use comments (delimited by /* and */) effectively.
  6. Avoid excessive use of global variables (in fact, avoid them altogether if possible!).
  7. Always use assert()s or some other kind of error checking.
  8. Reduce argument lists by using suitable structs where appropriate.
  9. Define obscure numerical constants at the beginning of a source file with #define macros or const statements; this makes the code easier to understand and change later on.
  10. Consider using enums to give descriptive names to simple integer flag values.

Note that in C all variables and functions must be defined before they are used. This is a good thing.

Massimo Ricotti 2009-01-26