Common
'C' Programming Errors
Getting your programs to Compile:
-
End all statements in a semi-colon,
except for your branching if and while statements.
-
Make sure you have all the necessary
header files. e.g. may need math.h or stdlib.h.
-
Make sure all the braces { } and
() match appropriately.
-
Make sure all your variable names
are spelt correctly.
Unfortunately 'C' doesn't give
compiler warnings or errors to code that obviously not what the programmer
intended. So even when your program compile you may have "weird" bugs.
-
Saying
" if (a = b) {" instead of "if
(a == b) {" // understand
what happens in the first case
-
Having a semi-colon at the end
of a for loop "for (i=0; i<10; i++);"
// this loop spins and just changes i to 10.
-
Not having { } around code that
should execute together. Avoid this by always using { }, even for
one line of code.
if (a == b)
a = 10;
else
a = 11;
// this line executive is a not equal to b.
b = 12;
// this line will always be executed
Ways to avoid errors
-
Plan out program logic before you
start coding.
-
Pay attention to your variable
names make them meaningful.
-
Pay attention to white space, especially
proper indentation.
-
Compile your program often, fixing
syntax and logic errors as you write your program.
-
Don't copy code from your neighbours,
not only may you get zero for cheating, but you won't understand what you
are trying to do. This will hurt you a great deal on tests and exams.
-
Use printf statements for debugging
as you write code before you know you will need them.
-
Test code as you go. Try to test
the different logic paths, think of test cases before you write code.
-
Learn how to use the debugger.
Computer Studies
Home
ICS3M Home
Top