0% found this document useful (0 votes)
100 views2 pages

4 - Understanding Common Errors in C++

There are three main types of errors that can occur when writing programs: syntax errors, logical errors, and runtime errors. Syntax errors occur during compilation and are caused by incorrect syntax. Logical errors involve faulty logic but do not crash the program. Runtime errors happen during execution and terminate the program abruptly. The document then provides examples of syntax errors in a sample code and defines runtime and logical errors further.

Uploaded by

Sarat Chandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views2 pages

4 - Understanding Common Errors in C++

There are three main types of errors that can occur when writing programs: syntax errors, logical errors, and runtime errors. Syntax errors occur during compilation and are caused by incorrect syntax. Logical errors involve faulty logic but do not crash the program. Runtime errors happen during execution and terminate the program abruptly. The document then provides examples of syntax errors in a sample code and defines runtime and logical errors further.

Uploaded by

Sarat Chandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

While writing programs in any programming language it is common to encounter

errors.

Generally speaking errors can be classified into the below three types:

Syntax Errors - Incorrect usage of syntax result in syntax errors. Since these
errors are shown during compilation, they are also called compile-time errors.

Logical Errors - Incorrect logic in the program which does not let the program to
produce correct output is called a logical error. These are found out during
testing. These do not abruptly terminate or crash the program.

Runtime Errors - The errors which occurs during the program execution and terminate
the program abruptly are called runtime-errors.
The runtime and logical errors are also known as bugs which prevent the program
from producing the correct output.

Let us consider the below code which contains two syntax errors:

#include <iostream>
int main() {
int a = 10, b = 20, c // statement-1
a + b = c; // statement-2
cout << "Result = " << c;
}

In the above code the line marked as statement-1 will trigger a compilation error,
as the statement is not terminated by a semicolon (;).

Similarly, the statement-2 also triggers a compilation error, as the assignment is


done incorrectly (where the RHS and LHS have been swapped).

Some of the most commonly occurring compile-time errors are:


Missing semicolon ( ; ) at the end of statement.
Missing any one of the matching separators in {, }, [, ], (, ), etc.
Incorrect spelling of a keyword in C++. Note that C++ is a case-sensitive language.
Using a variable without declaration it before its first use.

//---------------------------------------------------------------------------------
---------------------------------------------------------------//

Understanding Runtime Errors in C++

A Runtime Error is an error that occurs during the execution of a C++ program which
terminates the program execution abruptly.

A few examples of some runtime errors are dividing a number by zero, trying to open
a file which is not created, lack of free memory space, illegal memory access, etc.

//---------------------------------------------------------------------------------
---------------------------------------------------------------//

Understanding Logical Errors in C++

A logical error is a bug in the program that causes it to operate incorrectly and
does not terminate it abruptly.

Logical errors will not be detected by the compiler, these are usually found during
testing the program.

You might also like