How a C++ Program Works: 5 Phases + First Program Example

Before writing first C++ program or understanding how a C++ program works, let us understand phases from which C++ program passes. C++ programs passes through five stages before displaying the output. These five stages are as follows:

  • Edit, Compile, Link, Load, and Execute.

Editing

The first stage is editing a file. This work is done in an editor program, where you type your program and fix necessary errors. After that, the program is stored on the hard disk for later use. Your C++ program file must have the extension .cpp.

Compilation

Next, the program is compiled. The C++ compiler translates the C++ code into machine language code.

Linking

After this, the program goes through a linking phase, where it is linked with the standard library or any class or function you might have used from elsewhere.

Loading

Then comes the loading phase. Before any program is executed, it is loaded into memory, and this task is performed by the loader.

Execution

Finally, your program is executed. If there are no errors, it will execute; otherwise, the linker or compiler will give an error, and you should correct it before executing the program again.

Now, we will write our first C++ Program:

What is a program?

A program is a set of instructions that can be executed.

C++ provides a clear and organized design for computer programs, meaning that the structure of a program written in it is very organized. As we mentioned earlier, the C++ compiler, after processing (compiling) the source file, converts it into an executable file that can be run on your computer like other programs. The source files included are actually text files, and their extension is .cpp, while the extension for executable files is .exe.

Below, we have written a very basic example of C++ programming that uses key features of C++. Let’s look at a simple example of a C++ program.

#include 
using namespace std; 
int main()
{
    cout << "Welcome to C++ Program";
    return 0;
}

Let’s now look at the output. When you compile this program, the output will be:

Welcome to C++ Program

Explanation:

The first line of the program is:

#include 

To output anything to the screen, this line is necessary. #include is a Preprocessor Directive. It tells the program to include the iostream external file. Iostream has objects cout and cin, which are used for input and output operations.

The next line is int main(). This is called the main function, and it is essential to include in every C++ program. It tells the C++ compiler where the program starts. Next line is, using using namespace std. std here stands for standard. std is a namespace where identifiers of standard C++ Library are defined. The brackets {} represents the start and end of the body of the main function. Additionally, we have the line:

cout << "Welcome to C++ Program";  

uses cout to send the string “Welcome to C++ Program” to the output. You can replace this with any other text you wish to print. cout is the standard output stream used to display any line on the computer screen.. You will notice that the desired text is enclosed in double quotes " ". This indicates that whatever text you want to display should be written within these quotes, and it will appear on the screen exactly as you have written it. After each statement, it is necessary to add a semicolon ; as this marks the end of the statement. Without the semicolon, the compiler would treat the line as an incomplete statement.

The >> symbol after cout is known as the output operator or insertion operator. It transfers your written content to cout. With this you have learned about some key features of C++ that are used in almost every program.

No Comments

Leave a Reply