At the top of your C file, write a comment with your name, etc., in the following form:
This information is not strictly necessary, since your files are already identified by their names and the repository they reside in. Nevertheless, the redundancy is a helpful convenience for us when we are browsing and/or grading your work. It also gets you used to writing such a header at the top of every file./* Jane Doe, jdoe */ /* CS152, Winter 2016 */ /* hw 1 */
Comments, where they occur, should be precise and informative. Do not comment the obvious:
/* I'm adding b and c, and naming it "a"! */ <== not helpful int a = b + c; /* Summing number of birds and crocodiles */ <== much more helpful int a = b + c;
If a comment spans multiple lines, then you use the following format:
Be careful: C syntax does not support nested comments./* begin comment: slash star * middle lines start with space, star * the last line is a space, then end comment: star slash */
As a rule, do not write more than one statement on a line.
Your code should be no more than 80 columns wide. You can display the current column number in vim with the command
:set ruler
You will also want to issue the following two commands to vim to make the environment more pleasant to work in:
The first command turns on syntax coloring, and the second turns on line numbering. They're both very helpful.:syntax on :set number
You need to choose good variable names.
int a = b + c/* not good names */
int animals = birds + crocs;/* very good names */
Curly braces surrounding function bodies are placed on their own lines.
int my_example_function(int degrees) { }
Otherwise, you can choose one of two curly braces conventions. Whichever you choose, you must be consistent throughout your code. Choice 1: Open curly braces appear at the end of the first line on which they appear, as shown in the following example.
if (some_boolean_expression) { return 0; } else { return 7; }
Choice 2: Open curly braces appear on their own lines, and they are lined up with the beginning of the line with which they are associated.
if (some_boolean_expression) { return 0; } else { return 7; }
Indentation: You must indent each control structure, as shown in class.
Now and throughout the quarter, consult K&R (the required text) or class lecture notes for code layout standards.Remember you need to add and commit every file to your subversion repository. If, for any reason, you do not have access to your subversion repository today, please save your work (carefully) somewhere you can get to it later, and add and commit it as soon as you can. Please note that the CSIL Macintoshes are wiped clean every night, so saving files on them is not an option.