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)
问题:
回答1:
There are a lot of silly and not very respectful answers here to a legitimate question.
C didn't come from nowhere. Its immediate ancestor is B, written by Ken Thompson. Here is a link to the B manual. The basic structure of a B program is
main(); exit();
main() is provided by the programmer and exit() is supplied by the library. This seems to be the first appearance of main() as the predecessor of B, BCPL, has no such concept. I guess you would have to ask Ken Thompson why it was main and not something else.
回答2:
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 from man ld
:
-e entry --entry=entry Use entry as the explicit symbol for beginning execution of your program, rather than the default entry point. If there is no sym- bol named entry, the linker will try to parse entry as a number, and use that as the entry address (the number will be interpreted in base 10; you may use a leading 0x for base 16, or a leading 0 for base 8).
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:
-e
gives a replacement for_start
, notmain()
. You have to know how the system run-time passes arguments to a program and duplicate some of the functionality of crt[01in].o and crt{begin,end}.o to call main.
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's main
.
$ cat junk.c int junk() { return 8; } $ gcc -nostdlib -e _junk junk.c -o junk && (./junk; echo $?) 8
回答3:
Because C did it, C++ retained it to be compatible, and Java did it to ease the transition from C++. Back in the early days of Java, employers often hired people who had C++ experience because it was so similar. Not like today where they want the new guy to have more Java experience than Gosling.
And lets not forgot that PL/1 used "procedure options main" for the same purpose. (Man, that's refreshing some memory cells that haven't been touched in a while!)
回答4:
How would you name the main function of a program?
回答5:
Or, to be more obtuse, Why do we drive on the side of the road we do?
Answer: We had to choose something.
回答6:
It's not always main().
Java Applets use init() and start() for the external caller to hook into.
Servlets are started via init() and service() methods.
(service will dispatch to the more familiar doGet and doPost methods)
Granted, these exceptions do rely on some container other than the OS to invoke the methods.
回答7:
More interesting questions would be...
You got so close to some questions that would almost be interesting...
- is
main
part of the language or the library? - how does my program get started?
- does anything run before
main()
? - what was the first language to use
main()
? - can I write a program that uses no libraries? Does it need a
main()
? - what happens when I return from main()?
- to impress girls, how could I change the name from
main()
to something more cool?
回答8:
Quick answers:
- Why not?
- Why change it? To what?
- Because it's one of the symptoms that C, C++ and Java all share a common ancestry (specifically, that C has heavily influenced the other two). You won't see main() in Scheme or Prolog, for instance.
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.... ;-)
回答9:
Because it is main function. The term main function has been used at least since the 1960s. In PL/I, the function which started the execution had the following header:
FOO: PROCEDURE OPTIONS(MAIN);
where FOO
is the function name.
回答10:
The language designers had to choose "some" name and main() sounds like the Main function, since that is where the execution starts :)
回答11:
Probably because it's the main function that has to run. C++ inherited the name from C and Java inherited it from C++ (programmers don't like change).
回答12:
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.
回答13:
Well, it either had to have a fixed name, or you would have to give the programmer a way to specify the name.
If the programmer could pick the name, then there would have to be an extra statement or feature of some kind in the language just to handle that. And what would be gained? Arguably we'd be worse off: Then when you wanted to find this function, you'd first have to look for the thing that says what it's called, then you'd have to look for the function itself, so there would be two steps instead of one.
Given that it will have a fixed name, somebody had to pick what that name would be. One could think of many candidates: "start", "run", whatever. I doubt there was any overriding reason why "main" was chosen. Somebody had to pick something, that was as good a choice as any.
回答14:
Sometimes it's better not to change something just for the sake of changing it.
回答15:
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 implying int
when the return type is missing but also implying so to say main()
when the function name is missing.)
回答16:
Because it is as good as any other. The other way to go is to use an unnamed block, as in Pascal and its derivatives, or in most of the scripting languages, where the "main function" (if allowed to call it so) is just the beginning of the file. Then, you have to care from where you get the program arguments (some library or global variable) and you can ask why they have chosen args
instead of arguments
or argv
or param
, etc.
Sincerely, I never thought somebody would care about that almost irrelevant conventionalism xD
回答17:
If you develop an embedded system, you may see other names.
ECos uses
cyg_user_start().
(or main(), depending on the setup).
A linux kernel module uses a function marked with __init (though that's not the same thing, since modules are event-driven, typically).
回答18:
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...).