why does the execution of a c++ program start with main()???

Answers were Sorted based on User's Feedback



why does the execution of a c++ program start with main()???..

Answer / avtar singh sidhu

The compiler was designed to recognize main as the entry
point in a program. It started that way with C, and C
compilers were just built that way. It's the same way with
C++ and Java just because of habit, consistency, standards,
etc.

Is This Answer Correct ?    28 Yes 10 No

why does the execution of a c++ program start with main()???..

Answer / brian

Using static class execution can actually begin before main()!

E.g.

class MyStatic
{
public:
MyStatic() { cout << "MyStatic Class called." << endl; };
};

static MyStatic ms;

void main()
{
cout << "main called." << endl;
}

Output : =
MyStatic Class called.
main called.

Is This Answer Correct ?    13 Yes 3 No

why does the execution of a c++ program start with main()???..

Answer / ajaykumar(me-tu)

I agree with above answer(given by Sujith). It is not
necessary the execution of program always start with main
function. Using pragma startup directive we execute any
function before executing main function. Here we give one
example in c language.

#include<stdio.h>

void print()
{
printf("\nThis is print function");
}
#pragma startup print
void main()
{
printf("\nThis is main function");
}

Output:
This is print function
This is main function

Is This Answer Correct ?    9 Yes 4 No

why does the execution of a c++ program start with main()???..

Answer / siva

yes you are right Mr. Brian

In VC++ (MFC application) also excution starts with
globally created applcation object.

Is This Answer Correct ?    3 Yes 0 No

why does the execution of a c++ program start with main()???..

Answer / sujith

Typically all programs that is written using C or C++ have
a global header called C Runtime headers. This is inserted
to the beginning of programs. So when you start the program
the function that is called always is, _CRTMain(). This
function will call the main function. Why every function to
start with main? Well, it is not true that all functions
should start with main. There are ways to call a user
defined function before main using #pragma starup etc.
Still mostly the entry point for all functions are main.
The reason why it is given like that is, it is essential
that _CRTMain has to give control to the user defined
functions through some way. Main is that function, and why
it is named as main is because that is the "main" function
which _CRTMain calls.

Is This Answer Correct ?    4 Yes 2 No

why does the execution of a c++ program start with main()???..

Answer / alok ranjan

I agree with above answer(given by Sujith).
that are ture.

Is This Answer Correct ?    2 Yes 0 No

why does the execution of a c++ program start with main()???..

Answer / rajesh paul

Actually this is a very obvious question from our curiosity...
Even I was also very much puzzled at one time with this question and after a long time of discussion with my frnds and some books I got it clear. The explanation is as follows--




consider the code segment given by Brian--


class MyStatic
{
public:
MyStatic() { cout << "MyStatic Class called." << endl; };
};

static MyStatic ms;

void main()
{
cout << "main called." << endl;
}

Output : =
MyStatic Class called.
main called.
---------------X---------------

This happens because at run-time the static members are loaded into the RAM automatically along with the main() function. Hence as the class 'MyStatic' is statically instantiated before main(), the constructor function is called before main() as because the object is static i.e. already loaded into RAM hence the constructor is executed. Thereafter the main() is executed.


So from this discussion we come to the conclusion that "It is OS that instructs the system loader to load the main() into RAM automatically at run-time but no other function is loaded into RAM until and unless the main() calls them". That's what I want to say...........................

Is This Answer Correct ?    1 Yes 0 No

why does the execution of a c++ program start with main()???..

Answer / yajnesh

I think the above answer by avtar singh sidhu is right .
But i would like to add some more information to the above
The os is designed in such way that the main function is
always the starting point of execution so the process continues

Is This Answer Correct ?    13 Yes 13 No

why does the execution of a c++ program start with main()???..

Answer / achal

It is not necessary that program start from main(). This is
all decided by the embedded softawre designer. Have you
heard of attribute programming? Actually most tools take
main() as first function. But don't feel it occurs always.

Is This Answer Correct ?    3 Yes 3 No

why does the execution of a c++ program start with main()???..

Answer / brian

Answers 1-4 are Wrong !!!

Is This Answer Correct ?    2 Yes 4 No

Post New Answer

More STL Interview Questions

What are the components of stl?

0 Answers  


How do you convert stl to steps?

0 Answers  


Difference between Structure and Class in C++?

5 Answers   Caritor,


how to making game in c++ ?

0 Answers  


What is the stl, standard template library?

0 Answers  






What is stl stand for?

0 Answers  


Describe the My Computer and My Documents folders; identify the elements that are present in every Window.

0 Answers  


Who wrote stl?

0 Answers  


Write a program in C++ to concatenate two strings into third string using pointers

5 Answers  


what are you now programming Languages C+

1 Answers   HCL,


what is compiler?

4 Answers   NASA,


Write a C/C++ program that connects to a MySQL server and checks if the InnoDB plug-in is installed on it. If so, your program should print the total number of disk writes by MySQL.

0 Answers  


Categories