Why the name main for function main()

2019-02-06 22:39发布

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)

标签: java c main
18条回答
放荡不羁爱自由
2楼-- · 2019-02-06 22:53

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.)

查看更多
beautiful°
3楼-- · 2019-02-06 22:53

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...).

查看更多
时光不老,我们不散
4楼-- · 2019-02-06 22:58

Sometimes it's better not to change something just for the sake of changing it.

查看更多
何必那么认真
5楼-- · 2019-02-06 22:59

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, not main(). 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
查看更多
beautiful°
6楼-- · 2019-02-06 23:00

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.

查看更多
Emotional °昔
7楼-- · 2019-02-06 23:01

Quick answers:

  1. Why not?
  2. Why change it? To what?
  3. 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.... ;-)

查看更多
登录 后发表回答