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:
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.
Next, the program is compiled. The C++ compiler translates the C++ code into machine language code.
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.
Then comes the loading phase. Before any program is executed, it is loaded into memory, and this task is performed by the loader.
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.
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 <iostream>
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
The first line of the program is:
#include <iostream>
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.
Knowing how a C++ program works, stage by stage, is not just academic background – it directly determines where you should look when something goes wrong. An error reported during the editing stage, such as a typo in a keyword, is caught by your editor or IDE before compilation ever begins. An error during compilation, such as a missing semicolon or a mismatched type, is reported by the compiler with a specific line number, since the compiler is the stage responsible for translating your C++ source into machine code. A linking error, by contrast, usually means the compiler understood your code perfectly but could not find the actual implementation of a function you called – often because a required library was not properly included. Recognizing which of the five stages produced a given error message is the fastest way to know whether the problem is in your own code, in how you are including outside files, or in how the program is being run.
The natural next topic to learn is covered in our article on Comments in C++: Single-Line & Multi-Line with Examples. Readers wanting the formal language specification can consult isocpp.org.
No Comments