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). Example (style of /* */ placement is optional.):
/*****************************************************/
/* Program Header Block */
/* Filename: homework5.c */
/* Name: Ima Student */
/* SSAN: 123-45-6789 (could be XXX-XX-6789)*/
/* Date: 6 April 1998 */
/* Course/section: CMSC-104/0101 */
/* Description: (Your pseudocode goes here.) */
/* Notes: (As needed.) */
/*****************************************************/
- 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. These comments can be placed
at the beginning of a block, set of instructions or on the
line with each C statement.
Monday, 6-Apr-98 9:03:12 EDT