Coding Guidelines for C (as used for our projects): 1. All your variable/function names should be small case. Multiple words should be separated by underscore 2. Always use braces with if/while/for loops even if the body is one line 3. Use the following style of braces: if (cond) { body; } For do-while statments, use do { body; } while (cond); 4. Indent by two whitespace characters. Do not use tabs anywhere for indentation. In vi, you can ensure this by putting the following statements in .vimrc: :set cindent shiftwidth=2 :filetype indent on :set tabstop=2 :set expandtab :set foldmethod=marker 5. Each line should never exceed 89 characters 6. For function declarations, put the return type in one line and the rest of the declaration in another. If the arguments don't fit nicely on one line, split it like this: int lots_of_args (int an_integer, long a_long, short a_short, double a_double, float a_float) 7. Do not use implicit operator precedence. Use brackets to disambuguate for better readability. For example, don't use: if (a && b || c && d) { Instead use if ( (a && b) || (c && d)) { (Notice that by splitting operators of different precedence across different lines, we are improving readability) 8. Except the above condition, use the minimum possible brackets. For example, don't write: if ((a > 4) && (a < 6)) Instead write: if (a > 4 && b < 6) 9. Every program should start with a comment saying briefly what it is for. Example: `fmt - filter for simple filling of text'. This comment should be at the top of the source file containing the `main' function of the program. 10.Minimize the scope of variables. For example, use stack variables instead of heap variables wherever possible. Inside a function body, declare variables local to a scope if they are only used in that scope. Minimize the use of global variables. If a global variable is used only in a single file, use 'static' keyword for it. 11.All global and static variables should have a comment before them explaining what they do. 12.Comments should be in a separate line before variable/function declarations. Use the following style for comments: /* This variable is a demo of how to comment. */ int demo_var; 13.Put a comment on each function saying what the function does, what sorts of arguments it gets, and what the possible values of arguments mean and are used for. It is not necessary to duplicate in words the meaning of the C argument declarations. Here is an example: /* Attempts to resize OLD_BLOCK to NEW_SIZE bytes, possibly moving it in the process. If successful, returns the new block; on failure, returns a null pointer. A call with null OLD_BLOCK is equivalent to malloc(NEW_SIZE). A call with zero NEW_SIZE is equivalent to free(OLD_BLOCK). */ void * realloc (void *old_block, size_t new_size) 14. Use meaningful names for functions and files. Do not use names like record1.h record2.h etc. Instead use record_defs.h for example. 15. Minimize the use of function definitions in header files. Minimize the use of macros (use static inline functions instead if possible). Enclose macros inside a do { } while (0) construct. Indent the body of a macro just like you would indent a function.