Common Mistakes in the Mock Exam
(note - there may be updates to this document)
- #define is a command to the C preprocessor and thus does not need a semi-colon after it. Try using the -E flag to only run the preprocessor - it should make this type of mistake easier to see.
- Normal while statements have the following structure
while ( /* condition */ ) { /* content */ }however if it is a do/while loop an extra semi-colon is needed:do { /* content */ } while ( /* condition */ ) ; - Using -Wall when compiling will help improve your coding style and in some cases catch mistakes that would have otherwise have to be found via testing. -ansi and -pedantic are good choices as well.
- Making sure indentation and code layout is correct is important, especially as the program you write get longer. Consider using a text editor which automatically indents code for you.
- Note that if ( /* condition */ ) /* expression */ ; (i.e. without brackets but only one line in the body of the if statement) is valid C, but should be avoided as it can cause confusion / problem with maintenance.
- You should #include <stdio.h> before using printf or scanf, if you wish your code to be standards compliant.
- Global variables should be avoided. They make code harder to follow, harder to maintain and are rarely justified.
- It is better to have functions return an integer on success / failure and then perform any error handling in the code that calls them, than to perform error handling (i.e. printing messages) in the function.
Lastly - it was clear that some people simply had not been practicing. As well as the lectures and tutorials, you are expected to be putting in at least 4 hours practice a week. If you do, you will not find the course difficult and you will be able to program in C. If you do not then you will find it difficult and you won't be able to program in C.