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 23:04

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?
查看更多
我想做一个坏孩纸
3楼-- · 2019-02-06 23:05

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.

查看更多
萌系小妹纸
4楼-- · 2019-02-06 23:05

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.

查看更多
该账号已被封号
5楼-- · 2019-02-06 23:07

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

查看更多
Deceive 欺骗
6楼-- · 2019-02-06 23:07

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

查看更多
你好瞎i
7楼-- · 2019-02-06 23:10

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

查看更多
登录 后发表回答