This is the style guide for COMP2521.
In a C program file items must be arranged in the following sequence:
In a C header (.h) file items must be arranged in the following sequence:
A header comment should include:
// Student name and unsw email address // Tutorial class and tutor name // Date // What this file is for (one line summary). // Possible longer explanation
what |
convention |
example |
functions |
camelCase, lowercase first letter, no spaces or _ |
isCookie (nextItem) |
variables |
oldCookieCount = 7; |
|
anonymous variables (eg some loop counters) |
single lowercase letter |
i++; |
new typedefed types |
camelCase, lowercase first letter unless abstract, uppercase first letter if ADT. |
typedef struct positionRep { |
constants |
UPPER_CASE, words joined with _ |
#define MAX_COOKIES 5 |
void eatCookie (int age) { printf ("Yummy"); }
place opening brace compactly (after condition rather than on following line), eg
while (cookies == 0) { ....; }
use compact braces around else eg
if (cookies == 0) { ....; ....; } else { ....; }
do not further indent chained else if and else statements eg
if (cookies == 0) { ....; ....; } else if (cookies < 10) { ....; } else if (cookies < 50) { ....; } else { ....; ....; }
Declare one per line if initialised. Only use comma separated lists of variables in a declaration when they are related instances (eg int row, col;)
one per line, not i=0; j=0;
i = j = 0; //NOT OK
if ((c = getchar()) != EOF) //OK
write struct tags (eg "entryRep" in "struct entryRep" below) in lowercase.
typedef struct entryRep { char *name; int listing; } entry;
typedef struct nodeRep *Node; struct nodeRep { int value; Node next; };
blocks of comments:
style is discouraged. // at the start of each line is preferred.
gcc -Wall -Werror -O -std=c99
You may use
Parts of the initial version 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 expanded and revised based on the experiences of using it in our courses. It has benefited from the suggestions and comments of numerous 1911, 1921, 1917, 1927 and 2911 teaching staff and tutors. Thank you to all who have contributed.