CMSC 104 - C Coding Standards
Naming Conventions
- Begin variable names with lowercase letters
- Begin function names with uppercase letters
- Use all uppercase for symbolic constants (#define)
- Do not use global variables
- 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.