C++ Basic Program structure

Program Structure

The best way to learn a programming  language is by writing a program. That s why, here is our first program:

// 1st  program in C++

#include <iostream>

using namespace std;

int main ()


cout << “This is my first program!”;

return 0;


The above section shows the source code of our first program. Output of this program after compilation and execution will be as given below:


This is my first program!

The way how to edit and compile a program depends on the compiler you are using. It depends on whether it has a Development Interface or not and on its version.

No doubt it is one of the simplest programs that can be written in C++, but it contains all the fundamental components that are essential by every C++ program.

Let’s explore this code line by line:

// 1st  program in C++

This is a comment line. Comments do not have any effect on the behaviour of the program. There are two types of comments.

  1. Single line comments
  2. Multi line comments

Single line Comments always starts with two slash signs (//). The programmer can add short explanations or descriptions within the source code itself. In this case, the line is a brief description of what our program is.

#include Lines starting with a hash sign (#) square measure preprocessor directives. These square measure indications for the compiler’s preprocessor during this 1st program example the directive #include tells the preprocessor to incorporate the iostream commonplace file. This specific file (iostream) contains the declarations of the essential commonplace input-output library in C++, and it’s enclosed as a result of its practicality goes to be used later within the program.

using namespace std; All the weather of the quality C++ library square measure declared at intervals what’s known as a namespace, the namespace with the name std. thus so as to access its practicality we have a tendency to declare with this expression.

int main () This line is that the starting of the definition of the main function. This can be the entry purpose of C++ programs to begin their execution, severally of its location at intervals the ASCII text file. The directions contained at intervals this function’s definition can forever be the primary ones to be dead in any C++ program. Therefore all C++ programs have a main function.

Main function is followed by a combine of parentheses (()). that’s as a result of it’s a function declaration: In C++, what differentiates a function declaration from alternative sorts of expressions square measure these parentheses that follow its name. Optionally, these parentheses might enclose an inventory of parameters inside them. Right once these parentheses we will realize the body of the most function penned in braces ().

cout << “This is my first program!”; This line

could be a C++ statement. In fact, this statement performs the sole action that generates an apparent result in our initial program. cout represents the quality output stream in C++, and also the that means of the complete statement is to insert a sequence of characters (in this case the This is my first program!! is the sequence of characters) into the quality output stream (which typically is that the screen). cout is asserted within the iostream customary file inside the std namespace, therefore that is why we wanted to incorporate that specific file and to declare that we tend to were attending to use this specific namespace earlier in our code. Notice that the statement ends with a punctuation character (;). This character is employed to mark the top of the statement and if truth be told it should be enclosed at the top of all expression statements altogether C++ programs (one of the foremost common syntax errors is so to forget to incorporate some punctuation once a statement).

return 0; The return statement causes the most perform to end. return could also be followed by a  return code (in our example is followed by the return code 0). A come back code of zero for the most perform is usually taken because the program worked of course with none errors throughout its execution. This is often the foremost usual thanks to finish a C++ console program.

The program has been structured in numerous lines so as to be additional legible, but in C++, we tend to don’t have strict rules on the way to separate directions in numerous lines. for instance, instead of

int main () { cout << ” Hello World!”; return 0; } We could have written: int main () { cout << ” This is my first program!”; return 0; }

PDF books and articles are here.