DEV Community

Cover image for Types of errors in C++ & infographic summary
🇬OoGlE
🇬OoGlE

Posted on

Types of errors in C++ & infographic summary

Hello everyone:)

i wish to have a nice Day.

have a great Day.gif

today I explain the errors in C++

''When a user performs an illegal operation, the program malfunctions as a result. Programming errors are often undetected until the program is compiled or executed. The errors may prevent the program from compiling or running. Consequently, you should eliminate the errors before you compile and run it.''

Common errors can be grouped into the following categories

1. syntax Errors

2. Run-time Error

3. Linker Errors

4. Logical Errors

5. Semantic errors

type of error c++.png

1. syntax Errors

when you violate the rules of writing C/C++ syntax are known as syntax errors.

A compiler error indicates that something needs to be fixed before the code can be compiled. The compiler detects all of these errors, so they are known as compile-time errors.

The most frequent syntax errors are:

  • Missing Parenthesis (})
  • Printing the value of a variable without declaring it
  • Missing semicolon like this:

see the below code

#include <iostream>
using namespace std;

void main()
{
    int x = 10;
    int y = 15;

    cout << " "<< (x, y) // semicolon missed
}

Enter fullscreen mode Exit fullscreen mode

Error

error: expected ';' before '}' token
Enter fullscreen mode Exit fullscreen mode

2. Run-time Errors

Errors which occur during program execution(run-time) after successful compilation are called run-time errors.

Division by zero, also known as a division error, is one of the most common run-time errors.
The compiler does not directly point to the line where the error occurs for these types of errors.
Runtime errors are commonly called referred to as “bugs” and are often found during the debugging process before the software is released.

See the following example for more information.

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

void main()
{
    int n = 9, div = 0;

    // wrong logic number is divided by 0,
    // so this program abnormally terminates
    div = n/0;

   cout << "result = "<< div;
}
Enter fullscreen mode Exit fullscreen mode

note:

" These types of error are hard to find as the compiler does not point to the line at which the error occurs. "

### Error

warning: division by zero [-Wdiv-by-zero]
     div = n/0;
Enter fullscreen mode Exit fullscreen mode

In the given example, there is Division by zero error. This is an example of run-time error i.e errors occurring while running the program.

3. Linker Errors

These error occurs when after compilation we link the different object files with main’s object using Ctrl+F9 key(RUN)

When an executable of the program cannot be generated, these errors are generated.
Incorrect function prototyping, incorrect header files may be to blame.

Writing Main() instead of main() is one of the most common linker errors.

See the following example for more information.

#include <bits/stdc++.h>
using namespace std;

void Main() // Here Main() should be main()
{
    int a = 10;
    cout << " "<< a;
}
Enter fullscreen mode Exit fullscreen mode

4. Logical Errors

When certain input values are given to a program during compilation and execution, desired output is not obtained.

Logic errors are errors that appear to be error-free but provide incorrect output. They are among the most common programming errors made by beginners. Errors like these depend solely on the logic of the programmer and are easily detected if we follow the path of execution and find out why the program follows that path.

like this meme

image.png

#include <bits/stdc++.h>
using namespace std;
// C++ program to illustrate
// logical error
int main()
{
    int i = 0;

    // logical error : a semicolon after loop
    for(i = 0; i < 3; i++);
    {
    cout << "loop ";
        continue;
    }
    return 0;
}


// This code is contributed by shivanisinghss2110.

Enter fullscreen mode Exit fullscreen mode

Error

No output
Enter fullscreen mode Exit fullscreen mode

5. Semantic errors

A semantic error occurs when the statements written in the program do not make sense to the compiler.

#include <bits/stdc++.h>
using namespace std;
void main()
{
  int a, b, c;
  a + b = c; //semantic error
}
Enter fullscreen mode Exit fullscreen mode

Error

error: lvalue required as left operand of assignment
 a + b = c; //semantic error
Enter fullscreen mode Exit fullscreen mode

this summary for you :)

Copy of Yellow Gray and Black Hand Drawn Process Infograhic.png
Wishing you all the best 😊

Discussion (0)