UMBC CS 104
UMBC CMSC 104 CSEE | 104 | current 104

CMSC 104 - C Coding Standards

General Comments

Every programming department has some set of standards or conventions that programmers are expected to follow. The purpose of these standards is to make programs readable and maintainable. After all, you may be the programmer who maintains your own code more than six months after having written the original. While no two programming departments' standards/conventions may be the same, it is important that all members of the department follow the same standards. Neatness counts! At UMBC, the following standards have been created and are followed in CMSC 104 and CMSC 201.

Part of every project grade is based upon how well these standards are followed.

It is your responsiblity to understand these standards. If you have any questions, ask your instructor, your TA, or the Computer Science Help Center for assistance.

Naming Conventions

Use of Whitespace

The prudent use of whitespace (blank lines as well as space) goes a long way to making your program readable.

Use of Braces

Comments

Comments are the programmer's 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.
  1. The file name
  2. Your name
  3. The date the file was created
  4. Your section number
  5. Your UMBC e-mail address
  6. A description of what the code is and what it does
For example, /***************************************** ** File: proj1.c ** Author: Bob Smith ** Date: 6/22/99 ** Section: 0304 ** E-mail: bsmith22@gl.umbc.edu ** ** This file contains the main driver program for Project 1. ** This program reads the file specified as the first command line ** argument, counts the number of words, spaces, and characters, and ** displays the results in the format specified in the project description. ** ***********************************************/

Function Header Comments

EACH FUNCTION must have a header comment that includes the following:
  1. The function name
  2. A description of what the function does
  3. A description of the function's inputs
  4. A description of the function's outputs
  5. Side effect(s) of the function (if any -- this should be rare)
For example, /************************************************ ** CircleArea -- ** This function calculates the area of a circle ** with the specified radius. ** Inputs: the circle's radius as a parameter ** Output: the circle's area as the return value **************************************************/

In-Line Comments

In-line comments are used to clarify what your code does, NOT how it does it. Well-structured code will be broken into logical sections that perform a simple task. Each of these sections of code (typically starting with an 'if' statement, a loop, or a 'switch') should be documented. A particularly important line of code should also be commented. Do not comment every line of code. Trivial comments (/** increment x **/) are worse than no comments at all.

An in-line comment appears above the code to which it applies and is indented to the same level as the code. For example:

/* check every element of the array */ for (i = 0; i < ARRAYSIZE; i++) { /* if it's odd, add one ** if even, do nothing */ if (array[i] % 2 == 1) { array[i]++; } }

Comments for #defines and Variables

#defines should be commented on the same line on which they occur. Such comments should be aligned with the comments for other #defines. Once again, neatness counts!

For example,

#define NUM_STUDENTS 35 /* number of students in the class */ #define NUM_SECTIONS 4 /* number of sections of CMSC 104 */ #define NUM_TAS 5 /* number of graduate student TAs */ Variables MUST be declared one per line, each with its own comment: int total; /* total number of students */ int highest; /* highest final exam score */ FILE *ifp; /* input file pointer */ double average; /* class' final exam average */



Last Modified: Friday, 18-Apr-2003 14:05:30 EDT