what is the problem with static main()

S

suva

Hi,

What is the precise problem for which we cant declare main() as
static. I know there is no reason to do so but still I wish to know
the precise technical problem for which it is disallowed.
 
A

Alf P. Steinbach

* suva:
Hi,

What is the precise problem for which we cant declare main() as
static. I know there is no reason to do so but still I wish to know
the precise technical problem for which it is disallowed.

'main' is a function with special rules, so there would no technical
problem whatsoever with declaring it in any particular way, even using
special made-up keywords: the compiler must treat it specially anyway.

However, the standard imposes some requirements on declarations of
'main', to avoid a complete anarchy.

Also, there would be no advantage: you'd declare 'main' as 'static' only
to have that ignored by the compiler...

Cheers, & hth.,

- Alf
 
I

Ian Collins

suva said:
Hi,

What is the precise problem for which we cant declare main() as
static. I know there is no reason to do so but still I wish to know
the precise technical problem for which it is disallowed.

If main were static, what would be the program's entry point?
 
M

Matthias Buelow

suva said:
What is the precise problem for which we cant declare main() as
static. I know there is no reason to do so but still I wish to know
the precise technical problem for which it is disallowed.

A static main() would be like a house with a front door that could only
be opened from the inside.
 
J

James Kanze

'main' is a function with special rules, so there would no
technical problem whatsoever with declaring it in any
particular way, even using special made-up keywords: the
compiler must treat it specially anyway.
However, the standard imposes some requirements on
declarations of 'main', to avoid a complete anarchy.
Also, there would be no advantage: you'd declare 'main' as
'static' only to have that ignored by the compiler...

In this case, C compatibility may have played a role as well.
You can't declare main static in C. And in the case of C, this
has a strong technical justification; unlike in C++, the
compiler doesn't treat main specially (although it probably
could).

Not really a technical reason, but given that main can only be
called from outside the translation unit (and in fact, only from
outside your program), declaring it static (which for any other
function would say that it can only be called from within the
translation unit) would be misleading, to say the least.
 

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
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top