1

Hii ,

We generally see that the program execution begins in the main method for the languages like C , C++ , Java (i am familiar with these). I want to know how the compiler knows the presence of MAIN method in the program .

What does the main method signify besides that it is the entry point for program execution ...How does these criteria differ for C , C++ ...

Provide any links which you think are helpful ...

Chubsdad
  • 24,777
  • 4
  • 73
  • 129
Flash
  • 2,901
  • 5
  • 38
  • 55
  • In C/C++ it will not link into an executable with out main because the runtime calls the function main() to start the program. In Java it is basically the same process but performed at start-up. – Martin York Sep 09 '10 at 18:44
  • Note that 'main' is only special by default; the linking loader can optionally specify any function name as the entry point. – mpez0 Sep 09 '10 at 19:42
  • Have in mind that in C++ constructors of global variables will be executed *before* `main()` – gpeche Sep 09 '10 at 20:41
  • I think there have been previous questions on this topic. Pretty good dupe [Why the name main for function main()](http://stackoverflow.com/questions/1688338/), and related [C program without main function ?](http://stackoverflow.com/questions/3666540/), [main() in C, C++, Java, C#](http://stackoverflow.com/questions/1539385/). Other interesting stuff in this vein: [Is it the program or the OS that is responsible for setting up the stack ](http://stackoverflow.com/questions/3265057/). – dmckee --- ex-moderator kitten Sep 11 '10 at 15:28

3 Answers3

4

Generally, the code that is executed at the beginning of every C or C++ program (included usually by default by compilers/linkers) does some initialization and then calls a function called main. If this function is not present, it will lead to an unresolved name when linking a program (in which all the names have to be resolved). If it is present, it will be called by the program initialization code.

The initialization code does some housekeeping (for example, converts the return value of the main function to the exit code of the program, etc.)

Diego Sevilla
  • 28,636
  • 4
  • 59
  • 87
1

Nothing. It's just a conventional name for the starting point of the program.

in C, main() is as normal a function as sin() or any other function. The only requirement in a hosted implementation is that it conforms to one of the prototypes

int main(void);
int main(int, char **);

Edit

You can even call main() yourself from your code :)

#include <stdio.h>
int main(int argc, char **argv) {
    printf("main() called with %d arguments.\n", argc);
    if (argc) {
        main(0, NULL);
    }
    return 0;
}
pmg
  • 106,608
  • 13
  • 126
  • 198
  • 1
    As you noted, that's true for C. In C++, `main` is a bit more special; it may not be called directly, must not be overloaded, etc. – jamesdlin Sep 09 '10 at 19:18
  • `main()` is also quite different from `sin()` (at least in C) in that after a return from the initial call to `main()` the `atexit()` registered functions are called. – Jens Aug 21 '11 at 21:06
0

On windows it all starts with the Portable Executable file format: http://en.wikipedia.org/wiki/Portable_Executable.

The entry address can be specified through the linker: http://msdn.microsoft.com/en-us/library/y0zzbyt4.aspx

A managed application has a reference to the static main method in its assembly metadata. Again this is a command line option of the compiler: http://msdn.microsoft.com/en-us/library/6s2x2bzy%28v=VS.71%29.aspx

Alex
  • 1,997
  • 1
  • 16
  • 31