Commom Errors made in the C and C++ programs

Common Programming Errors in C

This Article lists down the common C programming errors

1.      Using Undeclared variables
Int main()
  Printf(“%d”,i); /* Error : I is not know to the program */

2.     Using = instead of  = =.

In C =  operator is used for assignment and = = operator for comparison . A compiler does not give a error when = is used for comparison. For example :

 int j = 10;
if (j = 5) 
    Printf(“Condition is True”);
   printf(“Condition is False”);

and the output is
Condition is True

In the above example j is not equal to 5 but still it prints the true result Because statement j=5 assigns 5 to j and if statement becomes
   If (5)
Now 5 is a nonzero value so it means true and it enters the if block.

3.     Forget to end the comments.
Comment start : /*
Comment end:  */

4.     Forget to put an ampersand (&) in scanf()

Scanf() always requires the  address of the variable to store value in it. Always use a & (ampersand) operator with the variable name to pass the address of the variable to scanf().

   Scanf(“%d”,&i);      /* & with i is required to pass the address of I to scanf() */

5.     Array Boundaries

The first element in an array in C is at index 0 (not index 1), and the last index isn-1 (not n), where n is the number of elements in the array.
  Students often write loops like

 for (i = 1; i <= n; i++)
                 a[i] = ...;
when they should write
              for (i = 0; i < n; i++)
                     a[i] = ...;
This can lead to unpredictable behavior of the program.

6.     “break “ is missing from switch statement.

7.     Forget to declare functions.
If functions returns other values than the 'int' type, they have to be declared before they are called. Example:
Long i;
 i = getval();
In this case 'i' is assigned an 'int' instead of a 'long'. In such cases the function needs to have a prototype like this
Long  getval(void);

8.     Using memory after freeing it using free() function.
It we continue to use a memory location which is deallocated using free() function it can give us unexpected results.

9.     Not Allocating memory to pointers .

The statement
     Char *str;
      Does not associate any memory with str. Now if we use it without allocating any memory
       Strcpy (str, “Welcome”)                                                        

the memory references will be some random location

 Correct Way to do this is :

Int main()
   Char str[10];

    Strcpy (str, “Welcome”)   /* str points to char array */
     Return 0;

10. Array pointer not passed to a called function: If a called function is to store values in an array for later use by the calling function, it should be passed a pointer to an array defined in the calling function.

11Comparing strings with ==

Never use the == operator to compare the value of strings! Strings are char arrays. The name of a char array acts like a pointer to the string. Consider the following code:
char str1[] = "hello";
char str2[] = "hello";

if ( str1 == str2 )
  printf("Condition True");
  printf("Condition False");
This program prints “Condition False”. Because the == operator is comparing thepointer values of str1 and str2, not the data pointed to by them. The correct way to compare string values is to use the strcmp() library function. For ex:
if ( strcmp(str1,str2) == 0 )
  printf("Condition True");
  printf("Condition False");
This program prints “Condition True”.

12. Strings are not terminated with NULL characters
C assumes that every string has a null character to mark the end of meaningful data in the string. If this value is missing, many C string functions will keep processing data past the end of the meaningful data and often past the end of the character array itself until it happens to find a zero byte in memory!