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 |
What is the disadvantage of templates ?
I am doing my BS.c MATHS CAN I ABLE TO JOIN IN NIIT?
Define stl.
In what scenario does the Logical file and Physical file being used?
What does stl mean in slang?
5. Write c++ function that would intake a string and return the number of occurrences of a given character in that sring Ex:- if the word is “Colombo” and count the occurrences of the letter “o” the function would return 3
what's the difference between function overloading and function overiding?
how to making game in c++ ?
#define CUBE(x) (x*x*x) main() { int a,b=3; a=cube(b++); printf("%d %d",a,b); } What should be the value of a and b? My calc a=4 but syst a=6 how pls tell me if you know it?
Q1. A. What is unary operator? List out the different operators involved in the unary operator. B. What is an adjust field format flag? Q2. A. Distinguish between a # include and #define. B. Can a list of string be stored within a two dimensional array? Q3. A.Explain how a pointer to function can be declared in C++? B.List the merits and demerits of declaring a nested class in C++? Q4. A. What are the syntactic rules to be avoid ambiguity in multiple inheritence? B. Explain the operation of overloading of an assignment operator. Q5. A. Explain how the virtual base class is different from the conventional base classes of the opps. B. Explain how an exception handler is defined and invoked in a Program. Q6. A. What is a binary file? List the merits and demerits of the binary file usagein C++. B. Write short notes on Text Manipulation Routines. C. Write bites in Turbo c++ Header (“Include”) Files.
0 Answers GE, Infosys, Microsoft, NIM,
When did c++ add stl?
Define the terms: field, record, table and database