Why the function name main() is retained in many languages like C, C++, Java? Why not any other names for that function? Is there any common structure for all these 3 main() (in C, C++, Java)
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- Multiple sockets for clients to connect to
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
Unfortunately, I’m not able (yet) to directly comment, so I’ll have to give a full answer without knowing an answer to your question at all.
But, however, I’d like to point out to all the people saying ‘what other than main() should it have become anyway?’ that indeed there is no need for a named function at all. It could well have been
{}
with the convention that code inside these anonymous brackets is the main function and that’s it. (So it’s not only implyingint
when the return type is missing but also implying so to saymain()
when the function name is missing.)because C, C++ and Java needed a way to know where the main function is...
however, many other languages allow it to be named the way you like and have a way to tell the compiler which function is the entry-point (compiler option, pragma, special statement...).
Sometimes it's better not to change something just for the sake of changing it.
Note also that while the name
main
is a convention of sorts, you can name your entry function whatever you want, so long as you tell the linker what the entry point actually is. See this snippet fromman ld
:Also, FWIW,
ld
's first choice of entry point is (sometimes) a function actually called_start
(but I think it's really a platform-dependent value).And see this mailing post which adds a little more explanation to
ld
's-e
option:I can't find where it's documented in the gcc man page, but you can also pass
-e
to gcc to specify the entry point; however, it ends up being a fairly complicated task when you work around the magic of C'smain
.You've got to name it something. And I can't think of any better name, since that's where the main program flow starts.
There is no common structure, except maybe the ability to take arguments. Nor should there be a common structure, since the whole point of a program is to do whatever the programmer wants. I.e., anything.
Quick answers:
Personally, I think the answer to questions 2a and 2b are the most important. If you really want to break every C/C++/Java program in the world in order to repair what you feel are flawed aesthetics of a single function name, I would have to ask you if you have your priorities in order.... ;-)