Coding conventions are rules about code style and formatting used to establish consistency in an organization's code base. Remember, you're not just writing code for the machine to read, other humans may want to read your code too! For ECE 220, you are expected to follow these conventions so that we can easily read your code.
Code can be compiled with the gcc
compiler with the following invocation:
gcc -g -std=c99 -Wall -Werror -o output_executable -l library1 [-l library2] ... source_file1.c [source_file2.c] ...
Do not omit the -std=c99 -Wall -Werror
flags.
Typical values for library are c (C library), and m (math library)
source_file.c
is a source file containing your implementation, written in C
output_executable
is the name of the program produced if compilation is successful
* This is only an example on how to compile your file. You should follow the compilation command listed in each MP write-up.
named_like_this
: Most variables, functions, and types
NAMED_LIKE_THIS
: Enumerated values and macros
Only use LF newlines (Unix newlines). Do not use CR+LF newlines (Windows newlines). Windows newlines can cause problems for some of the programs you will be using in ECE 120. If you use one of the recommended text editors, you will not have to worry about this.
Run cat -v <filename>
to check your files for windows newlines:
[kacampb2@linux3 ~]98$ cat -v windows.txt
I'm Windows (TM)^M
I like to be different from everyone else.^M
If you see ^M
at the end of each line, your file has windows newlines. Remove them using the dos2unix
utility:
[kacampb2@linux3 ~]99$ dos2unix windows.txt
Different text editors expand tabs to different amounts of spaces, which can result in formatting being wrong when someone else looks at your code. For simplicity, do not use any tabs in ECE 120 code.
Run cat -T <filename> to check your files for tabs:linux3:~$ cat -T tabby.txt
{
^I4 spaces and a tab on this line
^IOne tab on this line
8 spaces on this line (just keeping things interesting)
}
If you see ^I
at the beginning of lines (or anywhere), those represent tab characters. Remove them with the tab2space
utility in ~ece120/bin
:
linux3:~$ ~ece120/bin/tab2space
Usage: /class/ece120/bin/tab2space [-s <spaces_per_tab>] file1 [file2 ...]
linux3:~$ ~ece120/bin/tab2space -s4 tabby.txt
This includes if/else
clauses and for/do/while
loops. This avoids ambiguity about what statements are part of an if
statement or while
loop.
See above section on tabs.
All functions, structs, loops, and branches are indented with 4 spaces as shown in the examples. Most text editors can be configured to insert 4 spaces when the tab key is pressed.
In general you should not find yourself approaching this limit. If you are, first look at the level of indentation. If many of the characters on the long line are indentation, you should probably break up your code into multiple functions. If it's just a really long line, then use extra variables to break it up or wrap it, observing the wrapping style below:
Again, no tabs are used; all spaces.
float result = beta(epsilon, theta, delta)
+ alpha(gamma, theta, delta, epsilon);
int read_input_file(char * file, int data_rows, int data_cols,
bool strict, int max_entries);
There are multiple styles for initializers you can use.
char * answers[3] = { "yes", "no", "maybe" };
int primes[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 0 };
int filter[3][3] = { { 0, 1, 0 },
{ -1, 0, -1 },
{ 0, 1, 0 } };
char * fox_in_socks[4] =
{
"Sue sews socks of fox in socks now.",
"Slow Joe Crow sews Knox in box now.",
"Sue sews rose on Slow Joe Crow's clothes.",
"Fox sews hose on Slow Joe Crow's nose."
};
Always use the typedef
directive when defining structs and enums. This makes using the type simple.
typedef struct
{
float origin_x;
float origin_y;
float radius;
} circle;
typedef enum
{
APPLE,
ORANGE,
BANANA,
} fruit;
while (2 + 2 == 4)
{
printf("This statement is not false.\n");
}
do
{
printf("Enter a positive number:");
scanf("%d", &number);
} while (number <= 0);
for (bottles = 99; bottles > 0; bottles--)
{
printf("%d bottles of beer on the wall...\n", bottles);
}
if (input < 0)
{
printf("Negative\n");
}
else if (input == 0)
{
printf("Zero\n");
}
else
{
printf("Positive\n");
}
switch (fruit)
{
case APPLE:
printf("Apple\n");
break;
case ORANGE:
printf("Orange\n");
break;
case BANANA:
printf("Banana\n");
break;
default:
printf("Unknown\n");
break;
}
Functions should be documented, defined, and called as shown:
bool is_prime(int number)
/*! Determine if the given number is prime.
Return true if it is; false if it isn't.
Only works for positive integers up to 2.
Results for larger inputs are undefined.
*/
{
return number == 2;
}
int main(int argc, char * argv[])
{
int number = atoi(argv[1]);
bool prime = is_prime(number);
return prime ? 0 : 1;
}
Comments should be indented the same as the code they describe. Put very short comments on the same line as the code. Longer comments should go on a separate line. Comments are notes to help humans understand your code, so please don't just restate what the code already says. Use them to describe your algorithm at a high level and explain the meaning of your code
while (working)
{
/* Collect a history of input so we can refer to it later */
if (get_input(&input[index]))
{
break; /* Input failure */
}
index++;
}