Computing 1 Style Guide
This document sets out the style which is to be followed in all submitted work in the course COMP1917 Computing 1.
- C Program Structure
- Names
- Layout
- if, while
- Declarations
- Functions
- Structs
- Comments
- Compiler Options
- Thanks
C Program Structure
- header comment
- #included files
- #defines
- local struct typedefs
- local prototypes
- global variables (only if absolutely necessary)
- main function (if present)
- local functions
- header comment
- #included files
- #defines
- struct typedefs
- prototypes
- (extern) global variables (only if absolutely necessary)
A header comment should include:
// Name, Student Number.
// Date.
// What this file is for (one line summary).
// Possible longer explanation, copyrights, etc.
Names
- all names to be meaningful
- functions and variables like this:
varName
or var_name
- constants in all uppercase, e.g.
LINE_LENGTH
- single letter integers like
i,j,k
may be used for loop counters and array indices.
Layout
if, while
Declarations
- variables are declared at the top of the function.
- declare one variable per line if initialized; only use comma seperated
lists of variables in a declaration when they are related instances
(eg int row, col;)
- in a block of related definintions vertically align the names, types and initializations
- variables should be initialized close to where they are first used.
- all structs and enums defined with typedefs
- prototypes and typedefs for functions and types which are only used
locally at head of file
- prototypes and typedefs for functions and types which are used in
multiple files should be placed in a meaningfully named .h file, which is
included in the file in which the function is defined and also in each
file which uses the function or type.
Functions
- put the return type on same line as function name
- should be kept short, and broken into subfunctions as required
- each function should have a single clearly defined purpose,
this purpose should be reflected in its name.
main
is to be the first function in the file
main
should be preceded by prototypes of all
other functions in the file (including those returning int
)
- must give (sensible) parameter names in prototypes;
if a function takes no parameters, include
( void )
in the prototype
Structs
Compiler Options
- unless explicitly stated otherwise all code should be compiled with
gcc -Wall
- for debugging, you may like to use
dcc
instead of gcc
dcc -Wall
- if the code takes a long time to run, re-compile it using
the optimization option
gcc -Wall -O
(or -O2
or -O3
if you need even more optimization)
Thanks
Initially much of this document was based on
"Appendix C: C Programming Standards" of
Programming Techniques using the language C,
Government of South Australia, Pearson Education Australia 2005.
Subsequently it has been refined and
revised based on the suggestions by the COMP1911
and COMP1917 teaching staff.
Thanks to all who have contributed.