The main function is the point by where all C++ programs start their execution.It
does not matter whether there are other functions with other names defined
before
or after it, the instructions contained within this function’s definition will always
be the first ones to be executed in any C++ program.For that same reason, it is
essential
that all C++ programs have a main function
Body of the main() function
After the parentheses of the main function,the body of main function starts which
is enclosed in braces{ }.When this function is executed, the instructions written
within it perform their task. Inside the body of main function, C++ statements are
written.
C++ Statements
A C++ statement is a simple or compound expression that can actually produce
some effect.It is actually an instruction to the computer for taking an action.
For example cout << “Hello world!”; is a C++ statement. The following are some
examples of statements.
cout<< “Hello World!;
In fact,this statement performs the only action of displaying the result
“Hello World!” on the screen.
getch();
This is a standard library function defined in the header file <conio.h> and is used
to wait for the user to input a character from the keyboard.
return 0(zero);
The return statement causes the main function to return control to the operating
system.Return may be followed by a return code.The code 0 returned by the
teturn statement tells the operating system that the program terminates normally.
The program has been structured in different lines in order to be more readable, but in C++ ,we do not have strict rules on how to separate instructions in different lines. For example,the following program can also be written in a single line.
int main()
{
cout<<”Hello World!”;
return 0;
}
Its single line version will be:
int main() {cout<< “Hello World!; return 0;}