Skip to content Skip to sidebar Skip to footer

Errors & Warnings in C++

In the journey of the C++ programming, we would see tons of errors and warnings, which will restrict our program from successful execution and will lead to headache(s) as well; It just gives us an idea of whether we should let our system die by throwing our screen on the wall or use our brand new baseball bat because this is a frigging nightmare when bugs occur. However, you don’t need to do anything on your screen; understanding errors and warnings is not much complex.

What are Errors?

Error is an illegal operation performed by the user, resulting in malfunctioning the program’s working, finally leading to an unsuccessful execution. Programming errors weren’t calculated until the program was finally compiled or executed in the era of old IDEs. But now, thanks to our advanced IDEs, we can fix most of our errors quickly, even before the execution IDEs encounter errors already. The errors may prevent the program from compiling or running. Consequently, it would be best to eliminate the errors before you compile and run it.

Programming errors weren’t calculated until the program was finally compiled or executed in the era of old IDEs. But now, thanks to our advanced IDEs, we can fix most of our errors quickly, even before the execution IDEs encounter errors already. The errors may prevent the program from compiling or running. Consequently, it would be best to eliminate the errors before you compile and run it.

Types of Errors

The most standard errors can be broadly classified as follows.

  1. Syntax error
  2. Run-time error
  3. Linker error
  4. Logical error
  5. Semantic error

Let’s Go through each type one by one:-

Syntax error

Syntax error means something is wrong with the error the Syntax (Click here to read more about syntax). The syntax uses semicolons, parentheses, braces, and many other characters. When a programmer forgets or misuses one of these characters, the C++ will display an error message on the screen called syntax error.

Example
#include <iostream>
	using namespace std;
	int main()
	{
    	cout << "Hello world" // missed semicolon
    	return 0;
	}
Output
    Syntax_Error.cpp:5:26: error: expected ';' before 'return'
             cout << "Hello world" // missed semicolon
                                		 ^
                                 		 ;

Run-time error

A runtime error occurs when a program is syntactically correct but contains an issue detected only during program execution. One of the most common run-time errors is division by zero, also known as Division error. These errors are hard to find as the compiler doesn’t point to the errors directly.

Example
#include <iostream>
using namespace std;
int main()
{   
    int a=5;
    int result = a/0;
    cout << "Result : " << result;
    return 0;
}
Output
Run_time_Error.cpp:6:19: warning: division by zero
[-Wdiv-by-zero]
     int result = a/0;

Linked error

This error occurs when the program is compiling and trying to link the different object files with the main object file—for example, some wrong function prototyping, incorrect header file, Etc.

Example
// linker error
#include <iostream>
using namespace std;
 
void Main() // Here Main() should be main()
{
    int a = 10;
    cout << " "<< a;
}
Output
(.text.startup+0x2e): undefined reference to `WinMain'

Logical Error

When the program’s output is not desirable even after a specific input, that indicates the program is facing a logical error by the programmer. In logical error, the program is error-free; it either gives the wrong output or no output. ¯\_(ツ)_/¯

Example
#include <iostream>
using namespace std;
// logical error
int main()
{
    int i = 0;
    // logical error : i=3 in the confition of loop
    for (i = 0; i = 3; i++)
    {
        cout << "loop ";
        continue;
    }
    return 0;
}
Output
Loop Loop Loop ....(n times) //Therefore stuck in an infinite loop.

Semantic Error

A semantic error is a program that does not do what you intended but does a different task. This is the kind of error that happens when the programmer makes assumptions about the code without providing sufficient documentation about what those assumptions are.

Example
#include <iostream>
using namespace std;
int main()
{
    int a, b, c;
    a + b = c; // semantic error
    return 0;
}
Output

Semantic_Error.cpp:6:13: error: lvalue required as left operand of assignment

     a + b = c; // semantic error

Now we know all about the errors that can occur in the program and become a headache and how we can eliminate those errors, but there is still one thing left Warning, so without wasting much time, Let’s understand Warning.

What is a Warning?

Compiler warnings are messages produced by a compiler regarding program code fragments to be considered by the developer, as they may contain errors. Unlike compilation errors, warnings don’t interrupt the compilation process. A warning could result because of the use of obsolete features; or the use of nonstandard features of C++.

Should we ignore Compiler Warnings?

The Warning should not be overlooked. You’d better rectify every possible error before starting software testing. You may waste much time and effort to find an error in the debugger, although the compiler gives you an explicit warning about it.

And for the Best Programming practice, it’s always best to eliminate all the bugs in the code before pursuing further.

Until now, we understood Warning and errors, but both seem familiar so much that they are the bugs of the program and the obstacle to the successful compilation program, despite both are not identical. So What is the difference between an Error and a Warning? Have a look at this :  

Difference between Error and Warning

ERRORS

VS

WARNINGS

  1. A compiler error means your code has a syntax error that causes the compiler to give up.
  2. An error occurs when your source is not a legal program.
  3. No program output will generate.
  4. An error cannot be ignored and have to fix first before being re-run.
  5. Example: syntax error, logical error, semantic error
  1. A compiler warning means the compiler suspects your program might crash or behave strangely, but the syntax is correct.
  2. A warning occurs when your program is legal
  3. Program output may or may not generate.
  4. A warning can be ignored before re-run.
  5. Example: Header file missing, Pointer’s pointing mistake.

Follow up

Voila! You learned all about the bugs. Now you can debug your code and make malware for your best friend’s devices😁 and have fun.

Don’t forget to subscribe to us on FacebookInstagram & or Twitter. Also, don’t miss our newsletter subscription. We provide exclusive blogs, tips, tricks, and advice to program efficiently and market smartly. We also promote some of our partners with excellent experiences to share with you. Until next time 🙂

Leave a comment

Sign Up to Our Newsletter

Be the first to know the latest updates