What is the role of compiler and types of error in C++?

The compiler is used to find the error in the written code and it translates high-level language to machine-level language which is easily understandable by computers. We use the different types compilers for every programming language example C, C++, Java, etc.

What is the role of compiler and types of  error in C++?

Role of Compiler and Types of Errors

The compiler is computer software that translates from a high-level programming language to a machine-level language. It checks the correctness of the program code. If the meaning of our program code is correct, then a compiler can not detect errors but a compiler can certainly detect errors in the form of a program. 

The program errors are of the following types:

1.Syntax Errors

Syntax errors occur when rules of programming language are not properly used or misused. It means that syntax errors are caused when the grammatical rules of C++ are violated.

For example in the given code segment,

main(){
    int a,b: //line 1
    cin>>a>>b; // line 2
    cout<<a+b. // line 3
    return 0 //line 4
}

We can clearly observe that there are some errors in our code and these are known as syntax errors. If you are not able to find out what are the errors in our code? So, before reading further just give it a try for finding errors on your own.

Errors in the above-given codes are:

    1. In line 1 syntax error occurs because the statement is terminated by " : " rather than " ; ".
    2. In line  3 syntax error occurs because the statement is terminated by " . " rather than " . ".
    3. In line 4  statement semicolon (;) is missing which also gives an error.

2. Semantics Errors

Semantic errors occur when statements are not meaningful. Semantics refers to the set of rules which give the meaning of a statement. For example the statement 'Human have two legs'  is syntactically and semantically correct as it has some meaning but the statement 'Legs have two human'  is syntactically correct but semantically incorrect. Similarly, we use semantics rules of a programming language on violation of these rules it results in semantical errors.

For example X*Y=Z; will result in a semantical error as an expression can not come on the left side of an assignment statement.

3. Type Errors

In C++ data has an associated data type. For example, 10 is an integer, 'a' is a character constant, and 'hi' is of type string. If a function is given the wrong type of data, the type error is shown by the compiler. For example, if the function requires an integer argument but we provide string then it is a type error.

4. Runtime Errors

A run time error is that occurs during the execution of the program. It is caused due to some illegal operation taking place or the unavailability of desired or required conditions for the execution of the program. For example, if a program is trying to open a file that does not exist or could not be opened, it results in an execution error. Sometimes if enough memory is not available or an expression is trying to divide a number by zero are run-time errors.

5. Logical Errors

A logical error is an error that causes the program to produce incorrect or undesired output. For example, if we are trying to print the table of a number 5 and if we say

i =1;
while(i>10) // logical error
{
     cout<

In this example, the loop would not be executed even once as the condition ( i>10 ) is not satisfied at all. Therefore, no output will be produced. Such type of error is a logical error. 

A compiler reports an error by displaying the error message on the console/terminal. An error message contains a line number and a brief description of the error. After correction of all errors, the program should be recompiled and retested.

Now, We will stop here in this tutorial and will study data handling in C++ in the next tutorial. If you have any query drop your comment in the comment section.

like
0
dislike
0
love
0
funny
0
angry
0
sad
0
wow
0