CS2023
C Language Coding Standard
It is a requirement of CS2023 that
the following guidelines be observed for all coding assignments. This list
may be increased as the term progresses so be sure that you are aware of
any new items. In general you should follow the style guidelines in C
for Java Programmers, but the following guidelines must be followed.
-
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.
-
File Documentation: the first
item in a source file should be a comment block identifying the name of
the file, its author and what functionality the code provides. Each
individual function within the file should also have a comment block
naming the function and the functionality it provides.
-
Preprocessor Information: list
the header files that are needed, followed by the preprocessor macros.
Preprocessor macro names should use capital letters only.
-
Type definitions: programmer-defined
data types, using the C keyword typedef.
-
Function Prototypes: prototypes
for all programmer-defined functions should be presented. The arguments
should be specified with both data type and name (e.g. float xvalue).
-
Main Function: the main program
body. Ordering of the functions themselves is at the discretion of the
programmer, however a top-down approach is suggested. This means that the
first functions defined should be those that are called from the main program
and that functions calling no other function are listed last.
-
Functions: functions should
be listed last
-
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:
-
Variable Declaration: all variables
to be used in the function will be listed immediately following the function's
opening brace. Comments are only required for global variables (which should
normally not be used, except in specific cicumstances), constant
definitions, fields in structures. Instead of commenting each variable,
use meaningful names. Names made up of multiple words should either have
the initial letter capitalized, except for the first word (e.g. frontOfTheQueue),
or be separated with underline characters (e.g. front_of_the_queue).
-
Function Code
-
Function return: if the function
returns a value (that is, does not return void) the return statement should
appear as the last statement of the function.
-
Statement
Block Format: code blocks should be set off by indenting using
a tab. In the Emacs C environment this will be done automatically, which
also helps spot syntax errors as you write. Statements that exceed the
width of the screen (or printed page) should be broken into more than one
line so that the continuation line is indented from the parent line.
-
The
use of side effects 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.
CS2023
Home Page
Maintained
by Eric Aubanel, and derived from Rick
Wightman's C Language Coding Standard for CS1003 and CS1013.
Sept.
2, 2003