- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Errors in C/C++
In C or C++, we face different kinds of errors. These errors can be categorized into five different types. These are like below −
- Syntax Error
- Run-Time Error
- Linker Error
- Logical Error
- Semantic Error
Let us see these errors one by one −
Syntax error
This kind of errors are occurred, when it violates the rule of C++ writing techniques or syntaxes. This kind of errors are generally indicated by the compiler before compilation. Sometimes these are known as compile time error.
In this example, we will see how to get syntax error if we do not put semicolon after one line.
Example
#include<stdio.h>
main() {
printf("Hello World")
}Output
Error] expected ';' before '}' token
Runtime error
This kind of errors are occurred, when the program is executing. As this is not compilation error, so the compilation will be successfully done. We can check this error if we try to divide a number with 0.
Example
#include<stdio.h>
main() {
int x = 52;
int y = 0;
printf("Div : %f", x/y);
}Output
Program crashes during runtime.
Linker error
This kind of errors are occurred, when the program is compiled successfully, and trying to link the different object file with the main object file. When this error is occurred, the executable is not generated, For example some wrong function prototyping, incorrect header file etc. If the main() is written as Main(), this will generate linked error.
Example
#include<stdio.h>
main() {
int x = 52;
int y = 0;
printf("Div : %f", x/y);
}Output
C:\crossdev\src\mingw-w64-v3-git\mingw-w64-crt\crt\crt0_c.cundefined reference to `WinMain'
Logical error
Sometimes, we may not get the desired output. If the syntax and other things are correct, then also, we may not get correct output due to some logical issues. These are called the logical error. Sometimes, we put a semicolon after a loop, that is syntactically correct, but will create one blank loop. In that case, it will show desired output.
Example
#include<stdio.h>
main() {
int i;
for(i = 0; i<5; i++); {
printf("Hello World");
}
}Output
Here we want the line will be printed five times. But only one time it will be printed for the block of code.
Semantic error
This kind of error occurs when it is syntactically correct but has no meaning. This is like grammatical mistakes. If some expression is given at the left side of assignment operator, this may generate semantic error.
Example
#include<stdio.h>
main() {
int x, y, z;
x = 10;
y = 20;
x + y = z;
}Output
[Error] lvalue required as left operand of assignment
- Related Articles
- Difference between Compile Time Errors and Runtime Errors in C Program
- How to handle errors in middleware C# Asp.net Core?
- What are undefined reference/unresolved external symbol errors in C++?
- Errors in Assessment
- PHP Errors in PHP7
- Custom Errors in JavaScript
- Handling Errors in TypeScript
- Database Handling Errors in Python
- Rounding off errors in Java
- What is the difference between compile time errors and run time errors in Java?
- What are syntax errors in JavaScript?
- What are runtime errors in JavaScript?
- Examples of runtime errors in Python?
- Errors within Perl Modules
- PHP Types of Errors