Do i have to have a main function?

P

PencoOdStip

Do i have to have a main function?

If i don't have a function called main,i get erors.Why do i have to
have a function called main?
 
H

Howard

Do i have to have a main function?

If i don't have a function called main,i get erors.Why do i have to
have a function called main?

To keep from getting errors? :)

It's the function which gets executed when you run the program.

Whether you actually need a function called "main" or not depends on what
platform and type of executable or library you're compiling. For example,
it may be WinMain (or something like that, I forget) on Windows. And a
library may not even *have* an entry point, because it doesn't get executed
(at least not in the same way that a stand-alone application gets executed).
Instead, it may "export" any number of relative addresses in the library
where specific functions can be called.

Assuming you didn't have a main function, what would you do instead? How
would you expect the compiler to tell the operating system where to start
executing your code?

-Howard
 
O

osmium

Do i have to have a main function?

If i don't have a function called main,i get erors.Why do i have to
have a function called main?

Yes you do.

a) a bunch of guys got together many years ago, probably in some nice place
like Geneva, and decided you needed one, and that should be how to spell
it.

b) A sizable program has many separate source files. The operating system
has to know how to find which one of them gets control when the program
executes.
 
D

Default User

Do i have to have a main function?

If i don't have a function called main,i get erors.Why do i have to
have a function called main?

You don't, if you're not in a hosted environment. If you are, you need
one because the Standard says you do. Why don't you want one?




Brian
 
J

JohnQ

Do i have to have a main function?

If i don't have a function called main,i get erors.Why do i have to
have a function called main?

You don't have to. At least some compilers let you specify the entry point
(like Visual C++ for example). If you're not overriding the entry point
though, then you'll have to have a main. The entry point though is not
really main but some other function in the runtime library that calls main.
If you change the entry point, you can't use the C++ libraries because they
won't be initialized. Other things such as destruction of global objects
with constructors won't work unless you find a way to do so (not too hard).
I don't have an exhaustive list of the things you'll encounter on every
platform. Note that I have done this (created my own entry point) on the
WinTel platform and it was very instructional. I didn't figure it out as
much as I just did the research on the web on how to do it. I got into that
while trying to wittle down executable sizes to the minimum.

John
 
J

James Kanze

Yes you do.
a) a bunch of guys got together many years ago, probably in some nice place
like Geneva, and decided you needed one, and that should be how to spell
it.

It wasn't a bunch---more like two or three. And it was
someplace in New Jersey.
b) A sizable program has many separate source files. The
operating system has to know how to find which one of them
gets control when the program executes.

There are a number of ways of handling this. Kernighan, Thomson
and Richie just chose one.

And of course, in C++, you get control before main starts, in
static initializers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top