Here we discuss few common programming errors in c which we often commit. For better understanding we have listed the common programming errors according to topics.



Back to content
  1. Not putting semicolon after C statements like printf() or scanf().
  2. Not including required header files.
  3. Parenthesis not matching in printf() or scanf().
  4. Not including double quotes in printf().
  5. Not putting comma between identifiers.
  6. Including space in identifiers.
  7. using wrong conversion character in scanf() or printf().
  8. Not including address of operator ( & ) in scanf().
  9. Using address of operator ( & ) in printf().
  10. Not giving precedence to expression wherever required like a + b / 2 is different from ( a + b ) / 2.


Back to content
  1. Inserting semicolons at the end of expression in for loop or while loop or if or switch. 
         For example:
         for ( a = 4; a <= 10; a++);
              printf ( "%d", a );

    2.   Not inserting semicolon at the end of expression of while in do-while loop.
    3.   Not including curly braces to include if or loop statements.
    4.   Using an assignment operator in if expression instead of equality operator.


Back to content
  • Forget to put a semicolon at the end of prototype or function declaration.
  • Specifying function parameters of the same type as double x,y instead of double x, double y results in a compilation error.
    • Put a semicolon at the end of function header while defining the function.
             For example:
             float division(float a, int b);  // error 
                  return a/b;
    • Type mismatch error due to difference in the types in function declaration and function definition. The types of parameter may differ.
    • Type mismatch error due to difference in the order of parameters in function declaration and function definition.
    • Type mismatch error due to difference in the number of actual arguments and the number of formal arguments.
    • Defining a function inside another function is a syntax error.
    • Defining a local variable within a function with the same name as formal argument name.
             For example:
             float division(float a, int b)
                      int a; //error defining the same variable
    • Not returning any value when the function return type is not valid.
            For example:
            float division(float a, int b)
                  return ;
    • Declaring an array without specifying any value as size of array.
    • Declaring an array taking variable n as size of array. C does not allow a variable length array.
    • Initializing more values than the specified size.
    • Accessing array elements beyond the range limits. 
    • Array elements are to be used from 0 to maxsize-1. 
    • C does not report any error if user tries to access elements beyond this range but some garbage value is printed.
    • Declaring array of type void.


    Back to content
    • Not allocating sufficient space in a character array to store the null character that terminates a string.
    • Printing a "string" that does not contain terminating null character.
    • Processing a single character as a string. A string is a pointer - probably a respectably large integer. However, a character is a small integer ( ASCII values range 0-255). On many systems this causes an error, because low memory address are reserved for special purposes such as operating system interrupt handlers. So "access violation" occurs.
    • Passing a character as an argument to a function when a string is expected (and vice versa) is a compilation error.
    •  Not including the <string.h> header when using string functions from the string-handling library.
    • Not appending a terminating null character to the first argument of a strncpy when the third argument is less than or equal to the length of the string in the second argument.


    Back to content
    • The asterisk(*) notation used to declare pointer variables does not distribute to all variable names in a declaration. Each pointer must be declared with the * prefixed to the name. Eg: int *x,*y;
    • Dereferencing a pointer that has not been properly initialized or that has not been assigned to point to a specific location in memory is an error. This could cause a fatal execution time error, or it could accidently modify important data and allow the program to run to completion with incorrect result.
    • Being unaware that a  function is expecting pointers as arguments for pass-by-reference and passing arguments by value. Some compilers take the values assuming they're pointers and dereference the values as pointers. At run time, memory-access violation or segmentation faults are often generated. Other compilers catch the mismatch in types between arguments and parameters and generate error messages.
    • Using pointer arithmetic on a pointer that does not refer to an element in an array.
    • Subtracting or comparing two pointers that do not refer to elements in the same array.
    • Running off either end of an array when using pointer arithmetic.
    • Assigning pointer of one type to a pointer of another type if neither is of type void * is a syntax error.
    • Dereferencing a void * pointer is a syntax error.
    • Attempting to modify an array name with pointer arithmetic is a compilation error. 


    Back to content
    • Forgetting the semicolon that terminates a structure definition is a syntax error.
    • Assigning a structure of one type to a structure of different type is a compilation error.
    • Inserting space between the - and > components of the structure pointer operator is a syntax error.
    • Attempting to refer to a member of a structure by using only the member's name is a syntax error.
    • Not using parentheses when referring to a structure member that uses a pointer and structure member operator is a syntax error.
    • Assuming that structures, like arrays, are automatically passed by reference and trying to modify the caller's structure values in the called function is a logic error.


    Back to content
    • Opening an existing file for writing ( "w") when in fact, the user wants to preserve the file, discards the contents of file without warning.
    • Forgetting to open a file before attempting to reference it in a program is a logic error.
    • opening a nonexistent file for reading is an error.
    • Opening a file for reading or writing without having been granted the appropriate access rights to the file ( this is operating-system dependent) is an error.
    • Opening a file for writing when no space is available is a runtime error.
    • Opening a file in write mode ("w") when it should be opened in update mode ("r+") causes the contents of the file to be discarded.

    Share this

    Related Posts

    Next Post »