why does the execution of a c++ program start with main()???
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
Answer / alok ranjan
I agree with above answer(given by Sujith).
that are ture.
| Is This Answer Correct ? | 2 Yes | 0 No |
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 |
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 |
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 |
Write a program in C/C++ to implement reader- writer problem
how to overload << and >> operator in c++
differentiate between private, public and protected data members of the class using example.
What is C++ could you enplane me please?
How the STL's are implemented, What the difference between templates and STL?
Define the terms: field, record, table and database
If P is the population on the first day of the year, B is the birth rate, and D is the death rate, the estimated population at the end of the year is given by the formula: The population growth rate is given by the formula: B – D Write a program that prompts the user to enter the starting population, birth and death rates, and n, the number of years. The program should then calculate and print the estimated population after n years. Your program must have at least the following functions: 1. growthRate: This function takes its parameters the birth and death rates, and it returns the population growth rate. 2. estimatedPopulation: This function takes its parameters the current population, population growth rate, and n, the number of years. It returns the estimated population after n years Your program should not accept a negative birth rate, negative death rate, or a population less than 2. please answer my question ....
write a program that input four digit no and finds it is palindrome or not
What is stl stand for?
Which data structure gives efficient search? A. B-tree B. binary tree C. array D. linked list
21 Answers ABC, Sun Microsystems,
What is stl language?
what is a template?
2 Answers Amazon, BITS, IBS, Wipro,