C++ tutorial, C++ programs, C++ Exercises, C++ Example Programs

Monday, July 18, 2011

Basic Structure of a C++ Program

So, what is the format of a C++ program? We discussed in previous C++ lessons about some basic programming concepts like program, programming language, program development process, algorithms, flowcharts and so. Here we will discuss basic  structure of a C++ program. The basic structure of a C++ program consists of the following two main parts:

1. Preprocessor Directives

2. main Function

Basic C++ Program Strucrure

Preprocessor Directive number 1
Preprocessor Directive number 2
.
.
.
Preprocessor Directive number n
void main()
{
    C++ statement number 1;
    C++ statement number 2;
      .
      .
      .
    C++ statement number n;

1. Preprocessor Directives

Preprocessor directives are C++ commands given to Compiler. Preprocessor is a part of C++ compiler. Preprocessor acts upon these commands. Preprocessor directives modify source program before compilation.Preprocessor directives start with hash sign #. Preprocessor directives does not end with a semi colon. For example #include is a preprocessor directive. It is used to include header files into our source program. #include<iostream.h> means to include a header file iostream.h  into our source program. If we use cout object or cin object in our program, then we have to include iostream.h header file in our source program, because it contains necessary information about cin and cout object.

2. main Function

A C++ program is divided into small and manageable modules of code called functions. The main function is the starting and compulsory function. The control starts execution of C++ program from the main function. The basic structure of a main function is as follows:
basic-structure-of-C-plus-Plus-Program-in-detail-Explanation-examples
basic-structure-of-C-plus-Plus-Program-in-detail-Explanation-examples

void main()
{
    C++ statement number 1;
    C++ statement number 2;
      .
      .
      .
    C++ statement number n;
}
First line void main() indicates the start of main function. It is called declaration or header of main function. void means this main function will not return any value.
Second line { indicates start of body of main function. Body of main function consists of a set of one or more C++ statements according to the logic and requirement of the program.
C++ statements are also called instructions or commands. Each C++ statment is used to perform a specific task. For example, the follwing statement is used to display "Hello C++" on screen:
cout<<"Hello C++";
Next Lines indicates the presense of a set of one or more C++ statements.  
Here is a basic first program of C++.
Last Line }  shows the end of main function as well as the end of the program.
Let us see an example program according to the basic structure discussed above:

1. #include<iostream.h>

2. void main()

3. {

4. cout<<"Hello C++ Programming!";

5. }
OUTPUT of this program
Hello C++ Programming!

How does this program work?

How does this program work?
  1. [#include<iostream.h>] is used to include header file named iostream.h in source code (First.cpp).
  2. [void main()] indicates Start of main function
void means the main function will not return any value.
  1. { Opening Brace indicates the start of definition of main function.
  2. [cout<<"Hello C++ Programming!";] is used to display “Hello C++ Programming!” on monitor screen.
  3. } Closing Brace indicates the end of main function definition

Share:

1 comment:

  1. STC Technologies provide assured placements and that is the kind of the training provided here that everyone is ready to directly enter the corporate world and take heads on. STC Technologies

    ReplyDelete

We Love To Hear From You!

EasyCPPprogramming.blogspotcom

Labels