This is not a formal standards document. Rather, it contains style guidelines for C programs to make them easier (for me) to read and debug them.

Programs written for this course must satisfy the following guidelines. For examples of such programs, see the programs in USP.

  1. include files:
    1. Include files should only contain macro and type definitions, typedefs, and function prototypes.
    2. All system include files should come first, starting with those at the top level in alphabetical order, followed by those in subdirectories.
    3. These are followed by user include files in alphabetical order.
    4. These are followed by defined constants
    5. These are followed by prototypes
    6. On some systems that do not follow the POSIX standard, the ordering of the include files may have to be modified.
  2. main:
         int main(void) {
         int main(int argc, char *argv[]) {
  3. functions:
    1. Do not start a new line for the opening brace for functions.
    2. Local variables in alphabetical order
    3. If a call to a function can produce an error, be sure that you test to see if the error occurred and take the appropriate action.
    4. If a function makes an assumption about a parameter, this should be described in a comment before the code for the function.
    5. Never call exit from a function other than main.
    6. If you write a function whose purpose is not obvious, has restricted parameters, or has side effects, you must describe what this function does.
    7. Functions should not do any I/O unless I/O is required by the function specification.
    8. Functions should not have side effects unless absolutely necessary.
    9. All function side effects must be documented.
  4. Indent 3 spaces for each level and do not use tab characters.
  5. Comments right justified.
  6. No lines longer than 80 characters.
  7. Blocks: starting brace does not start a new line.
    Indent three spaces. Do not use tab characters in your file.
    Ending brace lined up with statement containing the matching brace.
  8. No program should ever have a possibility of a buffer overflow.
  9. Main programs that take parameters should check for the correct number of parameters and print a usage message if incorrect.
  10. Use functions (internal and external) liberally to modularize your programs.
  11. Use separate compilation.
  12. Use the static modifier to hide private functions and global variables whenever possible.
  13. Use variables with external linkage only when necessary.
  14. Use make.
  15. Do not embed the numeric values of arbitrary constants in your code.
    Use #define to define constants and reference these symbolic values instead.
    Be particularly careful to do this when defining the size of a buffer.
  16. At most one statement per line.
  17. At most one assignment per line.
  18. Do not return a negative value from main.
  19. Do not use 0 when you mean NULL.
  20. Do not use 0, 1, or 2 when you mean STDIN_FILNO, STDOUT_FILENO, or STDERR_FILENO.
  21. Never use goto under any circumstances.