

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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++?
- Custom Errors in JavaScript
- PHP Errors in PHP7
- Database Handling Errors in Python
- Errors within Perl Modules
- PHP Types of Errors
- 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?
- Are Python Exceptions runtime errors?
- Handling errors in SAP GUI Scripting code
- How to handle Geolocation errors in HTML5?
- How to catch syntax errors in JavaScript?