15 Common Errors in C and C++ Programming

By | August 20, 2014
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.
15 Common Errors in C and C++ Programming

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

Misuse of Semicolon

Another common mistake is to put a semicolon in a wrong
place. Consider the following code:
for(i =1; i<=10;
               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:

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:

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
/* 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.

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() >=

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)

Mismatching of Actual and Formal Parameter Type in Function

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)

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”,
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;
The above code will not find the correct sum of the elements
of array x. The for loop expression
should be corrected as follows:

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:
{              int a,*ptr;

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:
{              int m,*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:
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 Balagurusamy

Please share, if this article is useful for you!

Leave a Reply

Your email address will not be published. Required fields are marked *