I do know about the main() method used in C/C++ & Java but since the main() is user defined (as the code inside the main() is defined by the user, it can't be a predefined method) & in C/C++ the main is not stored in a header file, then where is it exactly located so that the compiler first searches for it in a program? And is main a keyword or something else? I heard somewhere that it is an attribute used as a keyword but I am not sure. Can someone please help me with this? (I would like to know about this in case of both Java & C/C++)
-
5Which language are you asking about? The answer is (probably, more or less) the same for C and C++, but very different for Java. – Mike Seymour Mar 24 '14 at 16:01
-
2It is not the _compiler_ which "searches for it" in the case of Java but the JVM. With C and C++ it is nearly similar, except that it is not the compiler which will look for it but the linker. You can compile object files in C/C++ without having a main() in them. – fge Mar 24 '14 at 16:04
-
Similar to [this question](http://stackoverflow.com/q/3679522/1270168), but I'm not sure it's a duplicate. – jerry Mar 24 '14 at 16:06
-
2In C/C++, there is typically a "startup" subroutine (which could be written in C, assembly, or something else) that does some setup work and then calls `main`. The setup work may involve calling subroutines that initialize global variables and call constructors for global variables, and possibly setting up the `argc` and `argv` parameters. – ajb Mar 24 '14 at 16:11
-
1In Java, `main` is just a method, only made special by the fact that, when you do `java SomeClass`, the JVM inspects `SomeClass` to find an appropriately described `main` method to invoke. If it's not found then the `java` command will report an error. The compiler has no say in the matter. – Hot Licks Mar 24 '14 at 16:16
-
1IIRC, there have been versions of C where the C compiler "recognized" `main` and specially packaged it. This is not in the architecture, though, it's just an implementation detail. – Hot Licks Mar 24 '14 at 16:18
-
@MikeSeymour- The question is in reference to both the languages i.e., C/C++ as well as Java – shw Mar 28 '14 at 19:00
4 Answers
main
is not predefined, but it is predeclared. In C, your code is linked against a small runtime library that constitutes the true starting point of your program. It is this small library that does a minimal amount of stack setup, then calls a function called main
--it's hardcoded to do so. Your code runs because you supply the definition of main
.
The Java runtime does something similar: the bootstrap code in the virtual machine will invoke the main
method of whatever class is provided as input.

- 497,756
- 71
- 530
- 681
-
1Just to be extra pedantic: In C, `main` is defined just like a function, but it is not allowed to call it. The runtime might have some funky way of invoking it. – vonbrand Mar 24 '14 at 17:11
-
How common is that? I have vague recollections of calling `main` recursively, although I'm sure it's certainly non-standard if it worked at all. – chepner Mar 24 '14 at 17:43
-
1In "typical" operating systems (Unixy, at least) the runtime does a more or less normal call to `main()`; but it is allowed to do something completely different. I remember a case (*long* time ago, some no-Unix system, might have been DOS or Windows) where calling `main()` failed spectacularly. And yes, I remember some cheater showing me a recursive program with *no* functions except `main()` once. – vonbrand Mar 24 '14 at 19:18
-
-
`libc`. It's not declared so much as it's a symbol that is expected to exist in the final fully linked object file. – chepner Mar 25 '14 at 17:41
Main is not a keyword in Java. When you try to execute a java code using "java" command, the runtime will load the public class that you are trying to execute and then call the main method defined in the class. The runtime knows that "main" is the method to look for as it is designed that way. The language specification also mandates that there should be a method named "main", which should be public and static and accept an array of strings as parameter with the return type as void. Since it is public and static, the runtime can call the method without having to instantiate the Class.

- 2,222
- 1
- 14
- 19
C++11
Just a quote from the standard:
3.6.1 Main function
A program shall contain a global function called main, which is the designated start of the program. It is implementation-defined whether a program in a freestanding environment is required to define a main function. [ Note: In a freestanding environment, start-up and termination is implementation-defined; start- up contains the execution of constructors for objects of namespace scope with static storage duration; termination contains the execution of destructors for objects with static storage duration. — end note ]
An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main:
int main() { /* ... / } and int main(int argc, char argv[]) { /* ... */ }
In the latter form argc shall be the number of arguments passed to the program from the environ- ment in which the program is run. If argc is nonzero these arguments shall be supplied in argv[0] through argv[argc-1] as pointers to the initial characters of null-terminated multibyte strings (ntmbs s) (17.5.2.1.4.2) and argv[0] shall be the pointer to the initial character of a ntmbs that represents the name used to invoke the program or "". The value of argc shall be non-negative. The value of argv[argc] shall be 0. [ Note: It is recommended that any further (optional) parameters be added after argv. — end note ]
The function main shall not be used within a program. The linkage (3.5) of main is implementation-defined. A program that defines main as deleted or that declares main to be inline, static, or constexpr is ill- formed. The name main is not otherwise reserved. [ Example: member functions, classes, and enumerations can be called main, as can entities in other namespaces. — end example ]
Terminating the program without leaving the current block (e.g., by calling the function std::exit(int) (18.5)) does not destroy any objects with automatic storage duration (12.4). If std::exit is called to end a program during the destruction of an object with static or thread storage duration, the program has undefined behavior.
A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling std::exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;
Yes. Main is a keyword in java and in C. An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of definitions of main.