12/10/2011

ERROR IN C PROGRMMING

,
Comman errors in C programming
Here's a C check list of programming errors.
• Check all variable types (especially while reading using scanf() ).
• Check for missing & in scanf .
• 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 '=='.
• Check for division by zero.
• Check if has been included when math functions like sqrt or sin, etc. are being used.
• It is not sufficient to include when using math functions. By including , all that the compiler gets to know is that the particular math function exists.
• Check if every malloc or calloc has a corresponding free. If files are being opened for reading, make sure that they exist.
• 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;.
• Similarly, for rand and random, check their ranges before using them. Do not use INT_MAX and RAND_MAX blindly. 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. 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(),be aware of the following way of reading:scanf("%d,%d", &i, &j);
• 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.

0 comments to “ERROR IN C PROGRMMING”

Post a Comment

comment plzz...

 

trick keys Copyright © 2011 -- Template created by O Pregador -- Powered by Blogger