Hello, novice programmers. (There is not much here for the experts).
Before you start bugging somebody else, try
finding the bug yourself. ;)
I have listed below some of the common mistakes that C programmers make.
Hope this comes in handy.
Well, the important ones first :
- If you try writing programs the night before your assignments are
due, you are bound to have errors. (Just in case you do not agree, you
are not a novice programmer and you may please try racking your brains
elsewhere .)
- Try to enjoy writing your programs, be patient and relax. As an
alternative,
you can try banging your head against the wall. You will be doing that
anyways after you fail to find the bugs in your program. ;)
- Do not try to be too cryptic (at least before your program is up
and running). You wouldn't understand that yourself.
- Cut/Copy and Paste saves you a lot of time, but only if you are careful
enough. Otherwise, a small change that you should have made (in the pasted
text) but haven't
may cost you hours of debugging. Go through the pasted text carefully
and patiently. Do not assume the correctness of anything in the pasted
region.
Now for the actual C stuff - here's a C check list.
- Check all variable types (especially while reading using scanf() ).
- Check for missing & in scanf (warning issued by Kernighan and Ritchie).
- Check for overstepping (moving outside array bounds).
- Check for infinite loops.
- Check for variable initialization (big and common fault when using '+=').
See if you are initializing inside (outside) a loop when you are supposed
to do that outside (inside) the loop.
- Check if '=' has been used in place of '+=' or '=='.
(Another warning by K & R. Yet, quite a common mistake.)
- Check for division by zero.
- Check if <math.h> has been included when math functions like sqrt or sin, etc. are being used.
- It is not sufficient to include <math.h> when using
math functions. By
including <math.h>, all that the compiler gets to know is that
the particular math function exists. The actual function would be part of
the math library which needs to be specified by the user (the command
would look like "cc -o prog prog.c -lm").
The compiler links it when creating the executable.
- Check if every malloc or calloc has a corresponding free. On some systems,
several versions of malloc or calloc may be available. Read the manual
before using them. Do not forget #including <stdlib.h>.
- When in doubt, use parentheses. (Adapted from : "When in doubt, toss!!" :))
- If files are being opened for reading, make sure that they exist.
(Quite silly, but unbelievably common.) Compare the value returned by
fopen() with NULL.
- Make sure that you are compiling the right program.
(You won't like to admit it, but this one does happen.)
- Beware of integer division. 1/2 is
always 0. Use 1.0/2 to obtain
0.5.
- This is an interesting one.
When using pointers, do make
it a habit to keep a space before the '*'. Or else, you might
find yourself making statements like
a = 1/*b;.
(Source :
Mr. B. Uma Shankar)
As a matter of fact, it
might be a good idea to write your expressions with your variables
and operators separated by spaces.
- When using atan, acos, etc., see the
manual to check the range of the return value
(i.e. [-pi,pi) or [0,2pi)), (when Inf will be returned, etc).
- Similarly, for rand and random, check their ranges before using them.
Do not use INT_MAX and RAND_MAX blindly.
Make sure <stdlib.h> has been included.
If your objective is to generate random numbers in (0,1), you may
use drand48() (instead of scaling values returned by rand()).
- When trying to catch a bug using printf() statements,
make sure that the printf() statements have a "\n" at the
end. Otherwise, pinpointing the error becomes difficult.
(The error could have occurred somewhere quite far away.)
If you are using scanf() statements, for stopping in between, use
an fflush() before reading characters. You can also read an integer
using scanf("%*d").
- When using scanf(), beware of the following way of reading:
scanf("%d,%d", &i, &j);Well, looks harmless, doesn't it?
But, unless you put a comma between the two integers during the input,
it is going to store some garbage into j, and you know the rest
of the story. The reason is that scanf() expects all ordinary characters
(that is, those which do not specify the format) to be present in
the actual inupt. However, this is may be a boon in disguise, if you
want to ignore certain characters. Your data may be comma separated,
in which case the above works. There's more that you can do with
this. Experiment yourself. :)
- Don't read more than the given data. Some systems don't
warn the user.
- If your program is still not ok, use some debugging tool. They are
easy to use and bugs are easily located.
- NOTE :
Some systems tolerate errors for quite some time.
But one should NOT expect them to continue tolerating errors forever.
It does not make much sense to say
"but my system should have prompted me at
that point itself...".
Any other mistakes that should have been included? Do let me know. (I will be
adding some more myself.)
Send your suggestions to me at
bln@indiatimes.com
BACK to HOME