In this tutorial, we are going to learn about the structure of a simple C++ program.

Structure of a CPP program:

When the language was standardized by the ANSI committee, they decided to move all of the functions in the runtime library into the std (standard) namespace where features of the C++ Standard Library are declared.

If they move all the functions into the std namespace, none of the old programs would work anymore, So, a new set of header files was introduced that use the same names but lack the .h extension. These new header files have all their functionality inside the std namespace.

The below given C++ code prints “Online Tutorials Point” to the console.

#include <iostream>
int main() {
    std::cout << "Online Tutorials Point\n";
    return 0;
}

Nowadays a C++program can be written as:

#include <iostream>
using namespace std;
int main() 
{ 
    cout << "Online Tutorials Point\n"; 
    return 0; 
}

The first line of the above program #include <iostream> has three parts:

  1. The # character is called the preprocessor directive.
  2. include is a command.
  3. iostream is the name of the header file which we are trying to include

The header file iostream contains a set of templatized I/O classes that support both narrow and wide characters.

The iostream uses the objects cin, cout, cerr, and clog for sending data to and from the standard streams input, `output`, error, and log  respectively.

The second line of the program using namespace std contains three parts:

  1. using means you are going to use it.
  2. the namespace is a container for a set of elements in which each element has a name unique to that set
  3. std is a namespace where features of the C++ Standard Library, such as string or vector are declared.

The standard iostream library lives in the std namespace. This can be fixed by either the application of a using-declaration or prefixing the relevant identifiers with std::.

The third line of the program int main() contains two parts:

  1. In our program’s context, int is the return type and which means the function main() returns integer data value.
  2. main() is a function (also called a subroutine) which is called by the operating system. main() is always the starting point of execution in any C++ program.

The cout is a standard output stream that is used to print the text specified within double quotes to the standard output device ( i.e, Console/Monitor).

The executable statements in the main() function must be enclosed within an open brace { and a close brace }.

Output:

Understanding a C++ program execution:

The instructions (called source code) written in the C++ programming language are saved to a file with a .cpp extension. For eg: Sample.cpp. This is called a source file.

There is a special program called a compiler, which is used to compile the source file. Once compiled, an object file with extension .obj is created.

There is yet another program called a linker, which combines one or more object files to create a single executable file. The executable file can have any extension. For example, in Windows, they usually have a .exe extension. This file is executed to get the desired output.

For writing the code and compiling it, we use the Integrated Development Environment (simply IDE). An IDE is a software for building applications that combines common developer tools into a single Graphical User Interface (simply GUI). For example, NetBeans, Eclipse, IntelliJ, etc…..

Dev-C++ is a free full-featured integrated development environment distributed under the GNU General Public License for programming in C and C++. It uses the MinGW compiler system to create Windows as well as Console based C/C++ applications.

 References:

Happy Learning 🙂