C Coding Standards
Naming Conventions
Use meaningful variable names !
For example, if your program needs a variable to represent the radius of a circle, call it 'radius', NOT 'r' and NOT 'rad'.
The use of single letter variables is forbidden, except as simple 'for' loop indices.
The use of obvious, common, meaningful abbreviations is permitted. For example, 'number' can be abbreviated as 'num' as in 'numStudents'.
- Begin variable names with lowercase letters
- Begin function names with uppercase letters
- Use all uppercase for symbolic constants (#define)
- Use all uppercase for typedefs
- Do not use global variables
- Be consistent!
- Separate "words" within identifiers with underscores or mixed upper and lowercase. Examples:
2-"word" variable name: grandTotal or grand_total
2-"word" function name: ProcessError or Process_Error
Use of Whitespace
The prudent use of whitespace (blank lines as well as space) goes a long way to making your program readable.
- Use blank lines to separate major parts of a source file or function.
- Separate declarations from executable statements with a blank line.
- DO NOT use tabs
- Use 3 or 4 spaces for each level of indentation
- Preprocessor directives, function prototypes, and headers of function definitions begin in column 1.
- All statements in the body of a function are indented 3 or 4 spaces. Statements inside of a loop or part of an "if" structure are indented further.
- Use spaces around all operators. For example, write x = y + 5;, NOT x=y+5;
Use of Braces
Always use braces to mark the beginning and end of a loop or "if" structure, even when not required.
Comments
Comments are the programmers main source of documentation. Comments for files, functions and code are described below.
File Header Comments
EVERY source file (.c and .h files) should contain an opening comment describing the contents of the file and other pertinent information. This "file header comment" MUST include the following information.
- The file name
- Your name
- The date the file was created
- Your section number
Function Header Comments
EACH FUNCTION must have a header comment includes the following:
- function name
- a description of what the function does
- a description of the function's inputs
- a description of the function's outputs