C++ Programming

C++ Programming

C++ Compiler


Introduction

In computing, a compiler is a computer program that translates computer code written in one programming language into machine level language. In C++, a compiler is a computer program that translates a C++ program into machine level language so that the computer can read and execute the program. The C++ program is called the source code, and the resulting compiled code produced by the compiler is usually called an object file. C++ is regarded as a middle-level language, as it comprises a combination of both high-level and low-level language features. It is a superset of C, and that virtually any legal C program is a legal C++ program.

The basic function of a C++ compiler is to analyze the correctness of the program. To compile a C++ program, go to the menu bar and click Compile. If there’s an error, the compiler will give the describe the error. So if a program has any errors, we cannot run it until the error is resolved.

In order to fix an error we need to know what kind of error it is. There are basically three types of errors that you must contend with when writing computer programs:

  • Syntax Error
  • Run-time Error
  • Logical Error

Syntax Error

Syntax Error is an error caused when the proper syntax of a language is not followed. Let’s say we have the following code:

int x=10;
cout<<x

Can you find the error in any of these lines. For those who found it, bravo you’re learning!! In the second line, the statement doesn’t end with a semicolon. Thus an example for syntax error is misplaced semicolon.





Run-time Error

A runtime error is a program error that occurs during the execution of the program. Run-time errors are commonly called referred to as bugs. Let’s say we have the following code:

int x,y;
x=y/0;

If you notice in the second line the value of x is unknown. Technically this statement is correct, but when you execute/compile the program it is considered as an error. Thus an example for run-time error is an unknown mathematical value.

Logical Error

A logical error is an error caused due to incorrect logic applied in the program. It’s a type of run-time error that may simply produce the wrong output or may cause a program to crash while running. Example: Infinite Loop

For those who are curious about what an infinite loop is, well we’ll study about it in detail in the further modules. Describing it right now may be quite skeptical and can lead to confusion.

Previously we have discussed only two kinds of tokens. Now let’s discuss the remaining few…..





Literals

Literals are often referred to as constants. These are data items that never change their value during a program run.     

Ex: int a=10;

Here the number 10 is a literal.

C++ has the following literals:

Types of literals

Punctuators

A punctuator is a token that has syntactic and semantic meaning to the compiler, but the exact significance depends on the context. The following characters are used as punctuators (also known as separator) in C++ :

  • Brackets [ ] – Opening and closing brackets indicate single and multidimensional array subscripts
  • Braces { } – Opening and closing braces indicate the start and end of a compound statement
  • Parentheses ( ) – Opening and closing parentheses indicate function calls and function parameters
  • Comma , – It is used as a separator in a function argument list
  • Semicolon ; – It is used as a statement terminator. Every executable statement is terminated be a semicolon
  • Colon : – It indicates a labeled statement

Operators

In C++, operators are used in a program to perform specific tasks or operations on data. Operators are tokens that trigger some computation when applied to variables and other objects in an expression. A quick sneak peek, there’s a full lesson coming up on operators so stay tuned.

The Weekly Program

Now let’s create a program to perform basic arithmetic operations.

#include<iostream.h>
#include<conio.h>
void main()

{
clrscr();
int x,y,z;
cout<<"Enter the value of x: ";
cin>>x;
cout<<"Enter the value of y: ";
cin>>y;
cout<<"Enter the value of z: ";
cin>>z;
cout<<"\nSum of three numbers: "<<x+y+z;
cout<<"\nSubtraction of three numbers: "<<x-y-z;
cout<<"\nMultiplication of three numbers: "<<(x*y*z);
getch();
}

The program is quite self-explanatory except for the last few lines.

cout<<“\nSum of three numbers: “<<x+y+z;   

The statement involving multiple output operators ( << ) is said to be a Cascading Output Operator Statement. Thus the above statement is an example.

The following statement can also be written as :

cout<<"\nSum of three numbers: ";
cout<<x+y+z;

When we use double quotes with the output operator ( << ) it will display just as it is in the output screen. When we only use the output operator, the output screen will be the result of the statement after the “<< ” operator that is the result of “x+y+z”.

cout<<“\nSubtraction of three numbers: “<<x-y-z;

The following statement can also be written as :

cout<<"\nSubraction of three numbers: ";
cout<<x-y-z;

cout<<“\nMultiplication of three numbers: “<<(x*y*z);

The following statement can also be written as :

cout<<"\nMultiplication of three numbers: ";
cout<<(x*y*z);





Execution of the Program

Now Compile the program. If the compiler shows errors then check with my program and resolve it. Then Run the program. Here’s the output:

Arithmetic Operations Output

Note : Make sure enter small values, otherwise the multiplication might be wrong

Pro Tip : If your turbo C++ screen is minimized, to make it fullscreen here’s a shortcut key Alt +  Enter

Now it’s time to do it yourself and feel free to mess around with the program to understand in-depth what each statement does. Don’t forget to save your program.

That’s it for today, see you next week!!!