Suggested C Coding Standards
Naming Conventions
- Begin variable names with lowercase letters
- Begin function names with uppercase letters
- Use all uppercase for symbolic constants (#define)
- Prefix global variables with "g_" or other common prefix
- Use meaningful identifiers
- Be consistent!
- Separate "words" within identifiers with underscores or mixed upper
and lowercase.
- Examples:
2-"word" variable name: baudRate or baud_Rate
2-"word" function name: ProcessError or Process_Error
Use of Whitespace
- Use blank lines to separate major parts of a source file or function.
- Separate declarations from executable statements with a blank line.
- If possible, configure text editor to use spaces rather than tabs.
- Use tab stops of 3 or 4
- Preprocessor directives, function prototypes, and headers of function
definitions begin in column 1.
- All statements in the body of a function are indented one tab stop;
statements inside of a loop or part of an "if" structure are indented
further.
Use of Braces
- Always use braces to mark the beginning and end of a loop or "if"
structure, even when not required.
Comments
- Every source file should contain an opening comment describing the
contents of the file and other pertinent information (author's name,
creation date, history of changes, special notes or compilation
instructions).
- Each function should have a header comment which states the purpose of
the function and the details of its usage.
- Include additional comments to explain use of variables, to delineate
the major parts of functions, and to help the reader understand
anything that's not obvious.
C Programming - Indentation Styles
Choose one of the two styles and use it consistently
if (condition) if (condition) {
{ statement(s)
statement(s) }
}
if (condition) if (condition) {
{ statement(s)
statement(s) } else if (condition) {
} statement(s)
else if (condition) } else {
{ statement(s)
statement(s) }
}
else
{
statement(s)
}
for (loop control expressions) for (loop control expressions) {
{ statement(s)
statement(s) }
}
while (condition) while (condition) {
{ statement(s)
statement(s) }
}
do do {
{ statement(s)
statement(s) } while (condition);
}
while (condition);
switch (integer expression) switch (integer expression) {
{ case constant1:
case constant1: statement(s)
statement(s) case constant2:
case constant2: statement(s)
statement(s)
default:
default: statement(s)
statement(s) }
}