The following is a list of some of the most frequently occurring bugs in C programs; it may help you in debugging your code. Using a strict compiler (like gcc) may trap some of them but C is very forgiving and consequently it is easy to write perfectly legal syntax which has unintended effects.
Forgetting a ; at the end of a statement or failing to close single or double quotes. This can send the compiler into hysterics
Forgetting to close a comment with */
and inadvertently commenting out a large chunk of your program
Failing to match opening { or ( with the the
corresponding closing } or ); this risk can be
minimised by suitable indentation.
Forgetting to initialize variables - not all compilers will initialise
them to an appropriate value.
Forgetting to initialise the counter on a while loop
Forgetting to reinitialize variables after exiting from a loop
using = (assignment) instead of == to test
for equality
A condition is false if it evaluates to zero and true if it evaluates to any other value
if (a=b) is legal C but will not usually be what you
intend; the test is true iff the value of b is non-zero
[*] Off by One errors e.g.
forgetting 0 is the index origin in C
array[1] is the 2nd element of array
for (i=0; i<= MAXNUMBER; i++) will cause 1 too many iterations
Putting a condition in a for or while loop which is always satified. This means the loop will never terminate, almost certainly causing the program to crash because it runs out of stack space or exceeds its memory space.
Putting a semicolon ; where you shouldn't and getting a null statement block
for the processing in an if clause or for loop.
as a rule of thumb, you don't put a ; if it will be immediately followed by a {
Assuming operators are overloaded in C as they are in some other languages e.g.
char* a, b; if (a = = b) ... is legal C but compares
the address of the two strings;
if you want to
compare their contents you need a string function like strcmp()
[*] Confusing a char*
array with char* string
A string is a variable length array terminated with a null
'\0' whereas a character array has a defined length;
if you use a string operation on a character array (e.g. in printf()
or a string copy), the function will keep on going until it
finds a \0 in memory somewhere or generates a memory
violation and crashes.
[*] Forgetting to put a '\0' terminator on strings you generate and causing the same problem as above
Confusing single 'x' i.e. a character occupying 1 byte and double "x" quotes i.e. a string of length 1 occpying 2 bytes
Confusing the logical operators for AND and OR in compound conditions i.e. putting
&& when you need || and vice versa.
This seems particularly common when NOT is involved in the condition.
Typing / instead of \ and wondering why you didn't get
the appropriate control character in your print output
Unintentionally, escaping a quote by writing ... \" instead of .... \n"
Assuming an incorrect order of evaluation of an expression; it is usually safer to
use parentheses, ( ) to force the order in which you
wish the expression evaluated than to rely on your memory of the order of operator
precedence.
In particular writing code which depends on the order of side-effects e.g.
a [n]=n++; different compilers may produce different results but fortunately
this is an error which tends to come with over confidence in the language, so offenders
deserve everything they get.
missing a break in a switch statement which causes processing to
flow through into the next case.
Similar oddball effects can occur if, by skimping on bracketing
the clauses, you mismatch the else statements in a nested
if-else structure.
[*] Mismatching the parameter
types when calling a function
This can cause the return address to be corrupted on the stack
common cases of this are
[*] Declaring a dynamic
structures like a string by a pointer variable (e.g. char
*) and forgetting to malloc() any space for it
char* X reserves 4 bytes for an address ; you can't start
putting the content X in it
Generally you will want to declare a variable as a pointer in your
function, when the structure to which it points is being created
by another function e.g. an API call.
[*] may cause a memory or stack error
Have fun - for all its flaws , C is a very flexible, very powerful and very elegant language and is the language generated as intermediate code by the compilers for most other languages. You are just cutting out the middle-man
Ian Vlaeminke 1996