Errors in C++
In this tutorial, we will learn about the errors in C++. There are many types of errors that can occur during the compilation or the execution of the program. Let’s understand these in detail.
Types of Error in C++
Errors in C++ can be classified in the following categories.
- Compile-time errors
- Run time errors
- Linker errors
- Logical errors
- Semantic errors
Compile-time error in C++
Errors generated at the time of compilation of the program are called compile-time errors. These errors can occur due to wrong syntax i.e. missing semi-colon, missing or extra parenthesis, initializing or printing a variable without its declaration, not following the right syntax for predefined keywords, etc. These errors are shown in the error log along with the lines they have occurred within.
An example of a compile-time error.
#include <iostream> using namespace std; int main() { //here i is not declared i=9; return 0; }
Output:
[Error] 'i' was not declared in this scope
Run time errors in C++
Run time errors are generated at the time of execution of the program. These programs are compiled successfully but produce errors when they are executed. The most common run time error is division error i.e. when we divide a number by zero. See the given example for a better understanding.
#include <iostream> using namespace std; int main() { int a=9,b=0; //division by zero a=a/b; cout<<"value of a after division by zero is:"<<a; return 0; }
Output:
Floating point exception
The above program is compiled successfully but produces unwanted results on execution. Therefore, this is a run time error.
Linker errors in C++
Linker errors occur in the linking stage of a program. If a C++ program gives a linker error that means some necessary function or library can not be found and hence executable of the program cannot be generated. See the example code here.
#include<iostream> using namespace std; void Function(); int main() { Function(); return 0; } void function() { //do something here }
Here, as you can see we have misspelled the function name while defining it. Now when a call to Function() is made, it looks for its definition in the program but since there is no function defined with name Function(), the error occurs.
Logical errors in C++
A program is said to have a logical error when it compiles and executes successfully but does not produce the desired output. Consider the below program.
//C++ program to print twice of sum of two integers #include<iostream> using namespace std; int main() { int a=1,b=2,c; int result=a+b*2; cout<<result; return 0; }
The above program intends to print twice the value that is obtained after adding a and b. So the output of this program should be 6. But it gives the following output.
5
If we see closely enough, we can notice that there is an error in our program. While calculating the result we haven’t used parenthesis for a+b. That is why we do not get the required result.
Semantic error
Semantic errors occur when there are statements in the program that do not have proper meaning. See the code given here for a better understanding.
#include<iostream> using namespace std; int main() { int a=1,b=1,c=1; a&b=c; return 0; }
Output:
Error
The above code gives the error message “lvalue required” because in the statement a&b=c we are trying to assign the value of c in a&b, which is meaningless.
Also read:
Leave a Reply