UNB/ CS/ David Bremner/ teaching/ old/ cs2023/ CS2023 C Language Coding Standard

It is a requirement of CS2023 that the following guidelines be observed for all coding assignments.

Source File Organization

The ordering presented here should be used.
Not all of the section types will necessarily be used in all programs, in which case don't put them in.
Each of the sections should be clearly identified within the program source file using comments and blank lines.

Note that the brief and detailed descriptions must be separated by an empty line (containing only a "*").

/** @brief getline: get line from file (file handle inFile)       
 *  @param  line The buffer where the line will be stored      
 *  @param  max The size of the buffer     
 *  @return The number of characters read   */

A detailed description and other comments (bugs, etc..) may also be included (see Doxygen documentation).

Function Format The main function must be of type int and must

return appropriate return codes. Functions must have meaningful names, leaving little question about the purpose they serve within the program. Functions should have at least the following three sections:

Indentation

Each block or level of scope should be set off by indenting using two to four spaces (choose a value and stick to it). In the Emacs C mode this will be done automatically, which also helps spot syntax errors as you write. Statements that exceed 72 characters wide should be broken into more than one line so that the continuation line is indented from the parent line. Example:

int log2(int n){
  int current=n;
  int count=0;
  int useless=n;
  while(current>0){
    current /= 2;
    count++;
    if (useless){
      useless = current/current+useless*count+3*count+useless*current
                   +2*useless;          
    }
  }
  return count;
}

The use of side effects

The use of expressions with side effects (except for some idiomatic cases we will discuss in class) is discouraged since it distracts from readability and comprehension.

The gets() Library Function

Use of the standard i/o function gets() is absolutely prohibited since introduces an an unavoidable security hole and/or bug into every program it is a part of.