0
Posted September 26, 2012 by Spyros in C/C++ Programming
 
 

Understanding the Basic Structure of a Program With a Simple C++ Example

Steel Structure1
Steel Structure1

‘Complex’ – this is perhaps the most common perception and misconception about C++. Well, to be honest, it really isn’t. When learnt with patience and a complete comprehension, C++ may turn out to be the easiest programming languages of all. And, with the same spirit, today we will learn and understand thoroughly – The Basic Structure of a C++ Program. As most of us know, this is a very primitive level C++ tutorial that is equally significant. Without sufficient knowledge on this, it is quite difficult to progress to further levels.

Basic Structure

In simple words, a C++ program is nothing but a set of instructions and commands that get executed in series to process and display the intended result. So, to learn the basic structure of a C++ program, we will make use of a sample program that comprises of almost all the elements of a basic structure.

Example Program:

// This is a Tutorial to Understand the Basic Structure of a C++ program 
#include <iostream.h>

int main () 
{
  cout << "Understanding the Basic Structure of a C++ Program";
  return 0;
}

Below, we will begin from the commencement of the program.

The Comment Element

This is the primary non-executing part of a C++ program. It is attributed by the symbols “//” and is never executed or considered by the compiler. These comment lines are generally added to the coding to enhance the human readability and understandability. Also known as the header block, this element of a C++ program gives valuable information about the program to the user. For instance, if a file is added to this part with a file name say “*.cc””, the header block should have necessary information such as the author name, its purpose, the date and the platform with which it was created. Furthermore, any modifications or changes made to the program must also be included in the header block. In simple words, this part of the program structure should contain all the information and details that are needed for human comprehension and usability.

The Command Element (not officially named, but referred for the purpose of understanding)

The command elements in a C++ program usually begin with a hash sign (#). These act as instructions for the preprocessors and indicate the compiler to include a specific set of header files that are pre-loaded in C++ library. For instance, the command #include indicates the compiler that it should include the standard header file “iostream” and an instruction of #include instructs the compiler to include the header file – “conio”. These codes are not executable and generally comprises of various declarations present in the typical C++ library.

The Main function

This part of the program structure signifies the commencement of the main function. It is the stage where the C++ program actually gets executed. The command int main () is the first executable file or a command in a C++ program and is unaffected by its position in the program. This means that, regardless of its position in the program, say beginning, center or in the end, the command to get first executed is the main function. It is essential for every program to contain a main function, which is usually followed by a parenthesis ().

The Executable Part

This part of the C++ program is often referred to as the executable part, where most of the commands and instructions get processed and executed. It may be a “cin” function that allows the user to input his choice of variables or values or a “cout” functions (as in our example).
According to our example, the primary function of cout is to stream the values to the output unit and display the results. In our case, the message Understanding the Basic Structure of a C++ Program will get displayed on the monitor. One of the essential things to in this stage is that the instructions or commands of such types should always have a semicolon at the end. By adding, it indicates the compiler that the instruction is complete and the command may be executed. Failure of inclusion will result in the compiler in sending back some error reports.

The End

This is the final part of a C++ program and is usually denoted by return command. It indicates the termination or the ending of the program and is generally followed by a return code, which is in this case – 0. The return code with 0 is the simplest way to terminate a program that has no compiling errors and issues. Notice that the return command also ends with a semicolon.

So, this was the basic structure of a C++ program. If you notice, you can find that each part of the program had its own functions and duties. Starting from the comment line that gives the programmer some valuable set of instructions to the return statement, which helps in termination of the program, every segment had an instruction to carry out. So, a thorough understanding of each part of the program will be helpful in the coming exercises and programs. So, practice and master over C++ with ease! Hope this was helpful.

This guest post has been brought to you by Robin Mckenzie of Buycenturylink.com, a site that offers savings and current information on consumers broadband internet and cable. Centurylink or you can go to this link to check for services in your area.


Spyros