Updated April 3, 2023
Introduction to Programming Errors in C
Errors, in general, are referred to as an action or fault or problem which is incorrect or makes the program behavior abnormal. In C programming language, the programming errors are bugs or faults that occur during runtime or compile time where the programs are not executed normally or can print any garbage values also. The process of removing or correcting these errors in C or any programming language is known as debugging. Therefore, for the programs to execute successfully they must be free from errors. There are warnings also generated by the compilers but they can be sometimes ignored as they rarely occur, while errors cannot be ignored as to obtain the desired outputs.
Types of Programming Errors in C with Examples
In C, there are different types of errors that can occur in the programs which can make programs run abnormally. Errors are occurred by beginners such as run time, compile-time errors, warnings, etc can be corrected in different ways.
Given below are the types of programming errors that occur in C programs:
1. Syntax Errors
These are the errors that occur during compiling the programs. These errors are compile-time errors. As these syntax errors are thrown by the compilers during the program execution hence these syntax errors are called compilation errors. In general, these errors are made while writing programs or the syntax writing rules are not followed while writing the programs. Such errors can be easily corrected as they are known. These types of errors are usually made by beginners who are learning the programming language.
Example:
Suppose we want to declare any variable with particular data type then the correct method or syntax for declaring variable is as below:
Syntax:
int a;
But if we do as
Int a;
Then the above declaration gives syntax error as to letter “I” for data type “int” is in the capital letter so.
Code:
#include <stdio.h>
intmain()
Int a = 10;
printf("The value of a is : %d", a);
return 0;
}
Output:
In the above program, we saw the data type was written wrong so it gave compilation failed due to the error and due to the occurrence of this error the program could not be executed. The syntax error can be like not mentioning data type before the variable, or not terminating the printf statement with a semicolon (;), not ending the program with flower brackets ( {} ), etc.
2. Runtime Error
This error has occurred during runtime which means that occurs during program execution that is after compilation of the program. This error occurs mainly when the program is still running and it will not be able to perform some particular operation in the main which may lead to memory leakage.
Example:
Code:
#include<stdio.h>
intmain()
{
int n = 9;
int div = 0;
int rem = n/0;
printf("Result of division = %d", rem);
}
Output:
The above program gives the divide by zero error during the program execution which can be handled by using exceptions.
3. Linker Error
This error has occurred when the executable file is not found or not created. This can be explained suppose if the main.c contains some function like func() whose executable file is func.c, this func() is defined in some other file such as sample.c and the object files generated are main.o and sample.o when the program is executing if the func() definition is not found in the sample.o then the linker will throw an error.
Example:
Code:
#include <stdio.h>
intMain()
{
int n=9;
printf("The value of n is : %d",n );
return 0;
}
Output:
In the above program, the error occurred because of writing “Main” instead of “main” which is the most common linker error.
4. Semantic Error
These errors have occurred when the program statements are not correctly written which will make the compiler difficult to understand such as the use of the uninitialized variable, type compatibility, errors in writing expressions, etc.
Example:
Code:
#include <stdio.h>
intmain()
{
intx,y,res;
x=2;
y=3;
res=1;
x+y=res;
return 0;
}
Output:
In the above program, the variable “res” cannot be used twice as the left operand.
5. Logical Error
This is an error that gives incorrect output due to illogical program execution but they seem to be error-free hence they are called logical errors.
Example:
Code:
#include <stdio.h>
intmain()
{
int sum=0;
int a=1;
for(inti=1;i<=20;i++);
{
sum=sum+a;
a++;
}
printf("The sum of the numbers is %d", sum);
return 0;
}
Output:
The above program is to print the output as the sum of numbers less than 20, but as we have specified semicolon after the for loop statement so it gives the wrong output where statements inside for loop are not executed. Hence the logical error occurs by printing the sum of 20 numbers as 1.
Conclusion
In this article, we have discussed the programming errors that are occurred in C programming languages. To conclude with this article is that there are different types of errors that occur, which can be occurred due to the writing the programs incorrectly, or by writing the wrong syntax, or by writing wrong expressions in programs, or by not following the rules of writing programs such as case sensitive is not followed, etc. In this article, we have discussed the main errors such as runtime errors, compile-time errors, syntax errors, semantic errors, and logical errors.
Recommended Articles
This is a guide to Programming Errors in C. Here we discuss the introduction to types of programming errors in C with 5 different errors and respective sample code. You may also have a look at the following articles to learn more –