7

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)

Nethra
  • 111
  • 1
  • 2

16 Answers16

33

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.

VLL
  • 9,634
  • 1
  • 29
  • 54
Tim Allman
  • 1,501
  • 11
  • 8
26

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
Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398
  • Great answer and it incresed my knowledge too! – Xolve Nov 06 '09 at 18:13
  • 2
    gcc didn't exist when C was written. – Tim Allman Nov 09 '09 at 12:15
  • For program development you're best to stick with the program entry point named "main". Because thats the convention. Anything else will confuse the next guy as to where it all begins. – hookenz Nov 08 '10 at 00:48
  • 1
    @Matt H: I don't understand. I thought "confuse the next guy" was the goal. At least, that appears to be the intent of most of the code I have to work on. – Jay Sep 27 '11 at 16:21
10

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

Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
  • 1
    That would rise a new question: why did C use name "main"? =) – BalusC Nov 06 '09 at 16:20
  • 2
    I suspect that boiled down to 'they had to use something' and 'main sounds good'. – Michael Kohne Nov 06 '09 at 16:24
  • 2
    @BalusC: That's going to be a difficult question to answer (aside from "that's what it was in B"). "Why" questions on arbitrary decisions that aren't important in themselves (e.g., in the US it's not important that we drive on the right, only that we all drive on one side) are seldom fruitful. – David Thornley Nov 06 '09 at 17:44
  • 1
    @David: there is some evidence that the side of the road you drive on does have a slight effect on safety. This is because of the dominant eye of the people driving. http://en.wikipedia.org/wiki/Right-_and_left-hand_traffic#Safety_factors – rmeador Nov 06 '09 at 18:01
8

Or, to be more obtuse, Why do we drive on the side of the road we do?

Answer: We had to choose something.

Chris Cudmore
  • 29,793
  • 12
  • 57
  • 94
  • 2
    actually I think that had to do with drawing swords and most people being right handed... Everyone used to walk/ride on the left, but then Napoleon changed it to make it more difficult to fight in the streets. At least that's the story I heard... I'm sure Snopes has an opinion. – Brian Postow Nov 06 '09 at 16:27
  • 1
    @Brian: When Sweden shifted from left to right back in 1967, the most popular argument for doing the switch was that it would be safer for the passenger to get out curb side (even when we were driving on the left, most vehicles (except for busses) had the steering wheel on left hand side). – Fredrik Nov 06 '09 at 22:48
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.

Chris Cudmore
  • 29,793
  • 12
  • 57
  • 94
3

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

Bob Cross
  • 22,116
  • 12
  • 58
  • 95
  • There are many good answers to the "To what?" question, some better some worse: `program()`, `start()`, `run()`... – R. Martinho Fernandes Nov 06 '09 at 15:43
  • @Martinho, no, those aren't better choices. "program" means a collection of source code. "start" and "run" are keywords that are used by Thread. Regardless, the fact that you could think of a new word doesn't answer the previous question: "why change it?" The answer would also have to be balanced against the reality that even if you could change "main" in Java (for example), you would break every Java program in the world. Are the aesthetics of a single name really worth it? – Bob Cross Nov 06 '09 at 18:05
  • @Bob you're arguing with hindsight. What's discussed is 'why did main() get to be named that way?', and it goes back at least 40 years, when there was no Java and no Thread. – Adriano Varoli Piazza Dec 17 '09 at 12:33
3

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.

FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91
Dan N
  • 31
  • 3
  • 3
    but if we renamed the main function FOO, then what would we call all of our temp little functions that aren't going to do anything useful??? – Brian Postow Nov 06 '09 at 22:38
2

The language designers had to choose "some" name and main() sounds like the Main function, since that is where the execution starts :)

2

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

TLiebe
  • 7,913
  • 1
  • 23
  • 28
2

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.

James Cronen
  • 5,715
  • 2
  • 32
  • 52
  • Actually, *you* can think of a better (highly subjective!) name: "where the main program flow starts" -> `program()`? `start()`? – R. Martinho Fernandes Nov 06 '09 at 15:42
  • 1
    It's actually not only a start, but the whole _main_ body of the program. You can add functions outside `main()`, but the _main_ flow of the program (at least in C and C++) is inside it. – Adriano Varoli Piazza Dec 17 '09 at 12:35
2

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.

Jay
  • 26,876
  • 10
  • 61
  • 112
0

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

Chris Judge
  • 1,952
  • 1
  • 13
  • 10
0

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

fortran
  • 74,053
  • 25
  • 135
  • 175
0

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

Tim Schaeffer
  • 2,616
  • 1
  • 16
  • 20
0

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

Debilski
  • 66,976
  • 12
  • 110
  • 133
-1

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

Adrien Plisson
  • 22,486
  • 6
  • 42
  • 73