0% found this document useful (0 votes)
33 views10 pages

01 General Structure

A C++ program generally consists of three main parts: preprocessor directives and header files, the main() function, and user-defined functions. It may include other optional sections like documentation, definitions, declarations. The main() function is where program execution begins and ends, containing declaration and executable parts. User-defined functions defined in the subprogram section are called from main(). Preprocessor directives like #include instruct the compiler, while header files contain necessary declarations.

Uploaded by

api-3769732
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views10 pages

01 General Structure

A C++ program generally consists of three main parts: preprocessor directives and header files, the main() function, and user-defined functions. It may include other optional sections like documentation, definitions, declarations. The main() function is where program execution begins and ends, containing declaration and executable parts. User-defined functions defined in the subprogram section are called from main(). Preprocessor directives like #include instruct the compiler, while header files contain necessary declarations.

Uploaded by

api-3769732
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 10

STRUCTURE OF A C++ PROGRAM

A C++ program generally consists of three different parts:


4. Preprocessor & header files directives
5. main() function
6. User-defined functions
STRUCTURE OF A C++ PROGRAM
The general form of a program is as follows:
Documentation Section
Link Section
Definition Section
Global Declaration Section
main() Function Statements
{
Declaration part
Executable part
}
Subprogram section Function 1 Function 2 . . . Function N
The documentation section consists of a set of comment lines giving
the name of the program, the author and other details, which the
programmer would like to use later. The link section provides
instructions to the compiler to link functions from the system
library. The definition section defines all symbolic constants.
There are some variables that are used in more than one function.
Such variables are called global variables and are declared in the
global declaration section that is outside of all the functions. This
section also declares all the user-defined functions.
Every program must have one main() function section. This section
contains two parts, declaration part and executable part. The
declaration part declares all the variables used in the executable
part. There is at least one statement in the executable part. These
two parts must appear between the opening brace and closing brace.
The program execution begins at the opening brace and ends at the
closing brace.
The closing brace of the main function section is the logical end of
the program. All statements and declaration and executable parts end
with a semicolon.
The subprogram section contains all the user-defined functions that
are called in the main function. User-defined functions are generally
placed immediately after the main function, although they may
appear in any order.
All programs must be written in lowercase.
THIS IS A STATEMENT WRITTEN IN UPPERCASE.
this is a statement written in lowercase.
All sections, except the main function section, may be absent when
they are not required.
PROGRAMMING STYLE

C++ is a free-form language. That is, the C++ compiler does not
care, where on the line we begin typing. While this may be a license
for bad programming, we should try to use this fact to our
advantage in developing readable programs. Although, several
alternative styles are possible, we should select one style and use it
with total consistency.
A simple program:

#include<iostream>
using namespace std;
void main()
{
cout<<“Hello, how are you ?” ;
}
Here observe that the documentation section, definition
section and global declaration section are absent. Also the
subprogram section is also absent. But this program has a
main function which is must for every program.
DIRECTIVES
The two line that begin the first example are directives. The first is a
preprocessor directive and the second is a using directive. They are
not part of basic C++ language, but they are necessary.

Preprocessor Directives
The first line of the example is:
#include<iostream>
Might look like a program statement, but it is not a program
statement. It is not part of a function body and does not end with a
semicolon, as program statements must. Instead, it starts with a
number sign #, pronounced as hash. It is called a preprocessor
directive. Recall that program statements are instructions to the
computer to do something, like adding two numbers.
A preprocessor directive, on the other hand, is an instruction to the
compiler. A part of the compiler called the preprocessor deals with
these directives before it begins the real compilation process.
The preprocessor directive #include<iostream> tells the compiler to
insert another file into your source file. In effect, the
#include<iostream> is replaced by the contents of the file indicated.
Using an #include<iostream> directive to insert another file into
your source file.

#include is only one of many preprocessor directives, all of which


can be identified by the initial # sign. The type of file usually
included by #include is called a header file.
HEADER FILES
In the first example, the preprocessor directive #include tells the
compiler to add the source file IOSTREAM to the source file before
compiling. IOSTREAM is an example of a header file. It is
concerned with basic input/output operations and contains
declarations that are needed by the cout identifier and the <<
operator. Without these declarations, the compiler will not
recognize cout and <<.
We will see more about header files later on.
The using Directive
A C++ program can be divided into different namespaces. A
namespace is a part of the program in which certain names are
recognized; outside of the namespace they are unknown. The directive
using namespace std;
says that all the program statements that follow are within the std
namespace. Various program components such as cout are declared
within this namespace.

You might also like