Divide each module up into a public part (what's needed to use the module) and a private part (what's needed to get the job done). The public part goes into a .h file while the private part goes into a .c file.
Use only the 95 standard ASCII characters in your programs. Avoid exotic characters. (Foreign characters may be used if you are writing comments in a foreign language.)
Good variable names are created by using one word or by putting two or three words together, separated by "_". For example:
Don't use variable names that differ by only one or two characters. Make every name obviously different from every other name.
When creating a two word variable name where the words can be put in any order, always put the more important word first.
Standard prefixes and suffixes are _ptr , _p , _file , _fd , and n_ .
Short names such as x , y , and i are acceptable when their meaning is clear and when a longer name would not add information or clarity.
Use argc for the number of command line arguments and argv for the argument list. Do not use these names for anything else.
When you can't put a descriptive comment at the end of a variable declaration, put it on a separate line above. Use blank lines to separate the declaration/comment pair from the rest of the code.
The exponent in a floating-point number must be a lowercase e . This is always followed by a sign.
Start hexadecimal numbers with Ox . (Lowercase x only.)
Put spaces before and after each arithmetic operator, just like you put spaces between words when you write.
In a statement that consists of two or more lines, every line except the first must be indented an extra level to indicate that it is a continuation of the first line.
When writing multi-line statements, put the arithmetic and logical operators at the end of each line.
When breaking up a line, the preferred split point is where the parenthetic nesting is lowest.
Split long for statements along statement boundaries.
Always split a for statement into three lines.
Write switch statements on a single line.
When splitting a conditional clause ( ? : ), write it on three lines: the condition line, the true-value line, and the false-value line. Indent the second and third line an extra level.
In C expressions, you can assume that * , / , and % come before + and - . Put parentheses around everything else.
When using K&R parameters, put the type declarations for the parameters in the same order as the occur in the function header.
Avoid variable length parameter lists. They are difficult to program and can easily cause trouble.
In an if chain, treat the words else if as one keyword.
When looping forever, use while (1) instead of for( ;; ) .
Avoid using do/while . Use while and break instead.
Use the comma operator inside a for statement only to put together two statements. Never use it to combine three statements.
Use one printf per line of output.
Start goto labels in the first column.
End every case in a switch with a break or the comment /* Fall Through */
Always include a default case in every switch , even if it consists of nothing but a null statement.
#define constants are declared like variables. Always put a comment describes the constant after each declaration.
If the value of a constant is anything other than a single number, enclose it in parentheses.
The use of const is preferred over #define for specifying constants.
When possible, use typedef instead of #define .
If a macro contains more than one statement, use a do/while structure to enclose the macro. (Don't forget to leave out the semicolon of the statement).
When creating multi-line macros, align the backslash continuation characters ( \ ) in a column.
#include directives come just after the heading comments. Put system includes first, followed by local includes.
Do not use absolute paths in #include directives. Let the -I compile opt
Comment #else and #endif directives with the symbol used in the initial #ifdef or #endif directive.
Define (or undefine) conditional compilation control symbols in the code rather than using the -D option to the compiler.
Put #define and #undef statements for compilation control symbols at the beginning of the program.
Do not comment out code. Use conditional compilation ( #ifdef UNDEF ) to get rid of unwanted code.
Use #ifdef QQQ to temporarily eliminate code during debugging.