In this article I am listing out the most common errors and mistakes that programmers do. These must be kept in mind while writing the code.
Also Read: 8 Best Rules for Good Programming Style
Missing semicolons
Every statement must end with a semicolon. A missing semicolon may cause considerable confusion to the compiler and result in misleading error messages. Consider the following statements.
a = x+y
b = m/n;
The compiler will treat the second line as a part of the first one and treat b as a variable name.
Misuse of Semicolon
Another common mistake is to put a semicolon in a wrong place. Consider the following code:
for(i =1; i<=10; i++);
sum = sum + i;
The code is supposed to sum all the integers from 1 to 10. But what actually happens is that only the last value of i is added to the sum.
Missing Braces
It is common to forget a closing brace when coding a deeply nested loop. The number of opening braces should match with the closing ones. However, if we put a matching brace in a wrong place, the compiler won’t notice the mistake and the program will produce unexpected result. Consider following example:
for(i=1;i<=10;i++)
sum1=sum1+i;
sum2=sum2+i*i;
printf(“%d%dn”,sum1,sum2);
In this code the for loop treat only the first statement sum1=sum1+i; as its body and therefore the statement sum2=sum2+i*i; is evaluated only once when the loop is exited. The correct code is:
for(i=1;i<=10;i++)
{
sum1=sum1+i;
sum2=sum2+i*i;
}
printf(“%d%dn”,sum1,sum2);
Missing Quotes
Every string must be enclosed in double quotes, while a single character constant in single quotes. If we miss them out, the string or the character will be interpreted as a variable name. For Example:
if(response==YES) //correct code is if(response==”YES”)
Grade=A; //correct code is Grade=’A’
Improper Comment Characters
Every comment should start with /* and end with */. Anything between them is ignored by the compiler. If we miss out the closing */, then the compiler searches for a closing */ further down in the program, treating all the lines as comments. In case, it fails to find to find a closing */, we may get an error message. Consider following lines:
. . . . . . . .
/* comment line 1
Statement1;
Statement2;
/* comment line 2 */
Statement 3;
. . . . . . . .
Undeclared Variables
Every variable must be declared for its type, before it is used. During the development of a large program, it is quite possible to use a variable to hold intermediate results and to forget to declare it.
Also Read: 5 Tips to Become a Better Programmer
Forgetting the Precedence of Operators
Expressions are evaluated according to the precedence of operators. It is common among beginners to forget this. Consider the statement:
if(value = product() >= 100)
tax=0.05*value;
The call product() returns the product of two numbers, which is compared to 100. If it is equal to or greater than 100, the relational test is true and 1 is assigned to value, otherwise 0 is assigned. In either case, the only values value can take is 1 or 0. This is not we actually want.
The statement was actually expected to assign the value returned by product() to value and then compare value with 100. If value was equal to or greater than 100, tax is computed.
The error is due to the higher precedence of the relational operator compared to the assignment operator. We can force the assignment to occur first by using parentheses as follows:
if((value = product()) >= 100)
tax=0.05*value;
Mismatching of Actual and Formal Parameter Type in Function
Calls
When a function with parameters is called, we should ensure that the type of values passed, match with the type expected by the called function. Otherwise, erroneous results may occur. If necessary, we may use the type cast to change the type locally.
Example: y = cos((double) x);
Missing & operator in scanf Parameters
All non-pointer variables in a scanf call should be preceded by an & operator. If the variable code an integer, then the statement scanf(“%d”, code); is wrong. The correct one is scanf(“%d”, &code); Remember that the compiler will not detect the error and you may get a crazy output.
Crossing the Bounds of an Array
Array indices start from 0. A common mistake is to start the index from 1. For example:
int x[10],sum,i;
for(i=1;i<=10;i++)
sum=sum+x[i];
The above code will not find the correct sum of the elements of array x. The for loop expression should be corrected as follows:
for(i=0;i<10;i++)
Forgetting a Space for Null character in as String
All character arrays are terminated with a null character and therefore their size should be declared to hold one character more than the actual string size.
Using Uninitialized Pointers
An uninitialized pointer points to garbage. The following program is wrong:
main()
{ int a,*ptr;
a=25;
*ptr=a+5;
}
The pointer ptr has not been initialized.
Missing Indirection and Address Operators
Another common error is to forget to use the operators * and & in certain places. Consider the following program:
main()
{ int m,*p1;
m=25;
p1=m;
printf(“%dn”,*p1);
}
This will print some unknown value because the pointer assignment p1=m; is wrong. It should be p1=&m;
Consider the following expression: y=p1+10;
Perhaps, y was expected to be assigned the value at location p1 plus 10. But it does not happen. y will contain some unknown address value. The above expression should be rewritten as: y=*p1+10;
Missing Parentheses in Pointer Expressions
The following two statements are not the same:
X=*p1+1;
X=*(p1+1);
The first statement would assign the value at location p1 plus 1 to x, while the second would assign the value at location p1+1.
Omitting Parentheses around Arguments in Macro Definitions
This would cause incorrect evaluation of expression when the macro definition is substituted.
Example: #define f(x) x*x+1
The call: y=f(a+b);
Will be evaluated as: y=a+b * a+b+1;
Some other mistakes that we commonly make are:
- Wrong indexing and termination of loops.
- Unending loops (infinite loops).
- Use of incorrect relational test.
- Failure to consider all possible conditions of a variable.
- Trying to divide by zero.
- Mismatching of data specification and variables in scanf and printf statements.
- Forgetting truncation and rounding off errors.
Source: Programming In ANSI C By E BalagurusamyPlease share, if this article is useful for you!
You have a bug in your fors. 1<10 instead of i<10.
I have corrected it, thanks for mentioning.
I want to print first m prime numbers
Can you find error in this program.
#include
main()
{int n=1,i,j=3,sum=0,m;
printf(“Enter the number of(ist) prime numbers to be printed:”);
scanf(“%d”,&m);
while(n<=m)
{for(i=1;i<=j;i++)
{if(j%i==0)
sum=sum+1;
}
if(sum==2)
{printf("%d",j);
n=n+1;
}
j=j+1;
}
}
Am a computer science student at Starford ,I love programming
Am a computer science student at DMI
I love programming the program above is running with no error
Header file error…