NCERT Computer: Chapter 7 – Introduction to C + + Part 10

Download PDF of This Page (Size: 188K)

12 Structure of C++ Program

The structure of a C++ program is given below:

Image of Structure of C++ Program-1

Image of Structure of C++ Program-1

Image of Structure of C++ Program-1

A C++ program starts with function called main ( ).

The body of the function is enclosed between curly braces.

These braces are equivalent to Pascal’s BEGIN and END keywords.

The program statements are written within the braces. Each statement must end by a semicolon (statement terminator).

A C++ program may contain as many functions as required.

However, when the program is loaded in the memory, the control is handed over to function main ( ) and it is the first function to be executed.

Let us now write our first program:

// This is may first program in C++

/* this program will illustrate different components of a simple program in C++*/

# include<iostream.h>

void main ( )

{

cout<<”This is may first program in C++”;

cout <<< “\n......................................”;

}

Structure of C++ Program-2

Structure of C++ Program-2

Structure of C++ Program-2

(i) Comments

First three lines of the above program are comments and are ignored by the compiler.

Comments are included in a program to make it more readable. If a comment is short and can be accommodated in a single line, then it is started with double slash sequence in the first line of the program. However, if there are multiple lines in a comment, it is enclosed between the two symbols /* and */

Everything between /* and */ is ignored by the complier.

(ii) Include

The lines in the above program that start with symbol ‘#’ are called directives and are instructions to the compiler.

The word include with ‘#’ tells the compiler to include the file iostream.h into the file of the above program.

File iostream.h is a header file needed for input/output requirements of the program. Therefore, this file has been included at the top of the program.

(iii) void main ( )

The word main is a function name. The brackets ( ) with main tells that main ( ) is a function.

The word void before main ( ) indicates that no value is being returned by the function main ( ).

Every C++ program consists of one or more functions.

The control is handed over to function main ( ) and it is the first function to be executed.

(iv) The curly brackets and body of the function main ( )

A C ++ program starts with function called main ( ).

The body of the function is enclosed between curly braces.

These braces are equivalent to Pascal’s BEGIN and END keywords.

The program statements are written within the brackets. Each statement must end by a semicolon, without which an error message is generated.

Example

Write a C ++ program that reads two values x and y, exchanges their contents and prints the output

Sign In