Common Errors in C Programming

By | September 4, 2017

Let us see what are the most common errors in programming and how to resolve them easily with simple tweaks in no time.

These errors are applicable to almost all the programming languages but we’ve explained here using C programming language. There are some specific types of errors in programming that you must know.

C Programming Common Errors with Solutions

1. Improper Comment Characters

There are two methods of commenting allowed in C programming. The first way to comment is to create single line comments with must start with two slashes //.

The other way is to create comments that can spread over multiple lines in a program. It starts with a /* and must be terminated by */ symbol.

We usually tend to forget to terminate the comment and it causes an error in the C program. Please see an example of commenting in C programming.

2. Undeclared Variables and Functions

Every user-defined function that is called must be defined with the function prototype above the main function before it is declared which is one of most common mistakes in C programming.

In the function prototype, we have to declare the number of arguments and their datatype. We usually forget to pass appropriate variables within the function parameters. You must check the C function pointer as programmers usually make mistakes here.

Another mistake is the undeclared variables. here, we have used b but we haven’t defined and initialized the variable which will generate a compile time error.

3. Missing Braces and Indentations

It is very common to forget closing braces when coding with nested loops. It is a compile time error usually but sometimes also turn out to be a run-time error.

With looping statements such as for loop and while loop, the braces must be defined correctly to define the scope and visibility of the for loop.

In the following code, there are two statements after the for loop which creates ambiguity as to which statement should be calculated with the for loop.

If both of them needs to be included within the for loop, then braces must be defined. Otherwise, a simple indentation can signal the compiler that only the first statement after the for loop needs to be included.

4. Improper use of Semicolon

Every C programming statement must end with a semicolon. It is very common to forget a semicolon and it may cause misleading errors and could be time consuming to debug the code.

The improper use of semicolons can also cause considerable compilation issues.

5. Missing and Improper Quotations

It is very likely to happen that a programmer may use single quotes (‘ ‘) instead of double quotes (” “) or vice versa.

 

A character in a string is defined with single quotations whereas a string is defined with double quotes and it is very much important to keep track of it.

7. Array Bounds Issue

The indexes in C programming starts with 0. Sometimes, we do not pay attention towards the looping condition and we get a run time error which can create confusion.

A common mistake here is to start the looping with an index 1 which may give a wrong output if the condition is not used properly. This is another very common programming error in many of the programming languages.

8. Missing Operators in scanf and printf functions

In a scanf() function, all the non pointer variables must be preceded by an Ampersand & operator. It is a very common mistake in C programming, and can cause confusions as it is not always detected by the compiler.

 

The & operator is reference operator and helps to scan the user input and assign it to a placeholder or a variable.

9. Operator Precedence

Mathematical expressions are an integral part of C programming language. We normally tend to do mistakes in our mathematical formulas by applying wrong sequence of operators.

The operator precedence defines the way how an expression will be evaluated by the C compiler and must be followed. This is similar to the BODMAS rule.

This creates ambiguity in the the resultant value of the expression but it can be easily prevented by using parantheses.

10. Improper Use of Increment and Decrement Operators

The increment and decrement are one of the most commonly used operators in C programming and these operators produce a lot of errors if not used properly.

The increment/decrement operators are logical operators and hence will never be traced by the compiler as it comes under logical error and can produce wrong output.

It is important to understand the use of both pre and post increment/decrement operators before using them in your expressions.

11. Infinite Loops

The infinite loop or indefinite loop creates one of the most common errors in C programming language. These kinds of errors generally occur in loops.

Every loop has to repeat steps until a certain condition is met. It is important for the condition to satisfy else the loop will keep on till infinity.

To prevent infinite loop condition, there should be an increment or decrement operator which will help to satisfy the looping condition.

12. Datatype mismatch

The datatype mismatch error occurs during function prototype definitions, passing arguments to a function, variable declarations and much more.

These kinds of errors also occur while passing parameters to a user-defined function as shown above.

13. Divide by Zero

This is probably the most common errors in C programming by newbie programmers.

When you divide any number by zero, it will result in a compile time error and will not let you run the C program.

Another very common error in C programming is the Lvalue Required Error.

These were some of the most common programming mistakes in C programming and is also applicable to C++ programming language.

If you have any doubts or have any common errors in C programming that you have faced in your programming journey, let us discuss in the comment section below.

3 thoughts on “Common Errors in C Programming

  1. Jai Shah

    Thank you so much for the solutions to these common programming errors. Datatype error was the one I used to get very frequently.

    Reply

Let's Discuss