Basic Structure of C++ Program

- November 17, 2017
Today, C++ is most powerful computer programming language that is widely used to develop system software and application software. Actually, C++ is the advanced version (edition) of C language.
     In this article we discuss about the basic structure of C++ program. Each part of this article is very important to clear the concept so read it completely.
C++ basic structure

What is Basic Structure of C++ Program

The format or layout according to which a program is written is called the structure of the program. 
Each programming language has its own program structure. The basic structure of C++ program is very simple that is consists on three main parts.
  • Preprocessor Directives
  • The main() Function
  • C++ Statements
      To understand the structure of C++ program, look at the given below source code that will display a message "My first program in C++" after execution.
#include <iostream.h>
#include <canio.h>
int main (void)
cout<<"My first program in C++";
return 0;

      Now we discuss about preprocessor directives, the main() function and C++ statements to clear the concept about basic structure of C++ program.

What is Preprocessor Directives in C++

The word directive means order or command or instruction. The instruction that are given to the C++ compiler to preprocess the information before starting the actual compilation process are called preprocessor directives. 
      A preprocessor directives is not a C++ program statement that is why it doesn't end with semicolon ";". The preprocessor directives are also known as compiler directives. When we run the program on the C++ compiler it takes action on preprocessor directives during translating the source code into object code. Each preprocessor directives strats with # sign. Such as #include, #define , #if,  #line and #line etc.
      In C++ program, there are a number of preprocessor directives that are used for specific function. For example #include directive is used to include a header file into the source code. Keep in mind, #include and #define is the most commonly used preprocessor directives in C++ source code.

What is the main() Function in C++

The main() function indicates the beginning (starting) of the C++ program. This function must be included in every program of C++. When a C++ is executed (run), the control goes directly to the main() function and starts executing its statements. To understand this concept look at the given below general syntax of main() function.
void main(void)
   //body of main() function
      In the above mentioned syntax of main() function:-
The keyword "void" is always used before main() function. Thie keyword indicates the data type of the value that is returned by the main() function. By definition,
A function may accept one or more parameters and return a single value. 
      The program is executed within an operating system (such as DOS). After the execution of the program, at the last stage of program execution the main() function can return a value to the operating system.  The "void" means that program will not return any value to the operating system after its execution. If we use "int" instead of "void" in this condition, program will return an intger value to the OS (operating system). The OS will use this value to determine whether the program is executed successfully or not.
Keep in mind that the use of 'void' or 'int' before the main() function and 'void' in brackets of the main() function are optional.
The "void" keyword is used within small brackets after the main() function indicates that program takes no parameters when program is executed from command line such as DOS prompt.
The statements written under the main() function represents the body of the main() function. The body of the main() function is always enclosed in curly brackets "{ }". Curly brackets are also called braces or delimiters. The left brace "{" mention the start (beginning) of the body of the function whereas the right "}" brace indicates the end of the body of the function.
Remember if a C++ program doesn't have main() function, it can't be executed however it can be compiled successfully.

What is C++ Statements

A fundamental unit of any programming language is called program statement. Program statements provide instructions to the computer to do something according to that instructions. 
      In C++ program, the statements are written under the main() function (or under any user defined function) between the curly "{   }" brackets.
"A statement or the set of statements written under the main() function is called body of the function."
      Every statement of C++ program terminate (end) with semicolon ";". The semicolon (;) at the end of C++ program is called statement terminator. Keep in mind that we can write multiple statements in one line but each statement ends with a semicolon.  If statement terminator ";" is missing at the end of any statement, then the following error message will be reported by C++ compiler during compilation process.
Statement missing;
      The C++ programming language is a case sensitive language. (As it keeps difference between lower case and upper case variables or identifies).Normally, the C++ statements are written in lowercase letters. However, variables or identifies can be written in both lowercase and uppercase letters.
      Keep in mind that peprocessor directives doesn't end with semicolon but C++ program statements must be end with semicolon. Because C++ statements provide the instructions to the computer to perform task according to the instructions while the preprocessor directives provide instructions to the compiler. Such as preprocessor directive #include provide the instruction to compiler to include header file.

Concept of C++ program

To understand better, look at the given below source code that is written in C++ and it displays a message "Welcome".
#include <iostream.h>
#include <canio.h> 
 clrscr (); 

In the above mentioned code:-

  1. First and second statements are preprocessor directives which are used to include the header files iostream.h and canio.h in the program.
  2. The third line contains main() function where execution of the program begin.
  3. There are two statements are written under the main() function between left and right brace. "{   }". Both statements ended with semicolon ";". In this program the body of main() function consists on only two statements. The first statement clears the monitor screen while second statement (ouput) displays a message "Welcome".