C Coding Standards

  1. Multiline comment at top with program name, author's name, description of program.
    	    /*
    	      cat -- concatenates files
    	      Author: Alfred E. Newman
                  Description: Concatenates one or more files read from standard
    	                   input or files named in command line arguments. If
    	                   arguments are present, reads from stdin. If a file is
    	                   specified as "-", reads from stdin.
    	    */
    	
  2. Proper indentation, 2 or 4 spaces.
  3. Single spaces around operators.
  4. Opening curly brace goes on the line introducing the block.
                for (int i = 0; i < n; i++) {
                  a[i]++;
                }
  5. Functions other than main preceded by comments with the purpose of the function and description of each argument.
    	    /*
    	      square(x) -- squares x
    	      Arguments:
    	        x:  the number to double
    	      Returns:      the square of x
    	      Description:  Computes x * x.
    	    */
    	    double square(double x) {
    	      return x * x;
    	    }
  6. Commas in between arguments followed by a single space.
  7. Comment things that are tricky. Do not comment things that are obvious.
  8. Use meaningful names. However, names like i, j, and n are fine if there's not a better choice.
  9. Separate words in names with underscores.
  10. Constant names are all caps.
  11. Separate sections of the program with single blank lines. Do not use extra blank lines.
  12. Check for presence of command line arguments. Print standard form usage message if arguments are missing.
  13. Print standard for error messages using errno and strerror.