tags:

views:

527

answers:

9

Is main() (or Main()) in C, C++, Java or C#, a user-defined function or a built-in function?

A: 

In C/C++, it a standard so its built in and reconized.

Java, not sure, no experience

C# - Its a part of a class so its defined by you.

All of these are defined by you -- you tell it what to do.

Daniel A. White
+3  A: 

It's a required user defined function (the entry point for executables)...

Justin Niessner
+14  A: 

It's a user-defined function that is necessary for a program to execute. When you go to run your program in the compiled language, the main function is what is executed. For instance, in Java, if you have a function of the signature public static void main(String ... args) in a class then that class can be executed, as the JVM will execute the contents of that main method.

Example in Java:

public class Test {
  public static void main(String ... args) { 
    System.out.println("Hello World");
  }
}

...

javac Test.java

...

java Test

Results in "Hello World" being printed to the console.

geowa4
It's not required if you tell the linker to use another function as the entry point.
Ryan Kearney
@Ryan - some of us (*cough* Java *cough*) don't enjoy the sweet privilege of having a linker.
ChssPly76
@Ryan - Some of us (*cough* me *cough*) don't like fighting with a linker if we don't have to ;)
aperkins
@Ryan - In Java there is no linker, and your main method is required to have the signature `public static void main(String[] args)` and there is no way you can change that.
Jesper
+3  A: 

It is not "built-in" in any language, in a sense that there is no standard implemented-for you main() avialable.

For C/C++/Java, it is a function with a special property, namely, the function that will be called at the start of your program after all the static setup is done. E.g. entire C program's execution path is:

  1. Do some initialization code

  2. Call main()

  3. Exit.

As such, it has a standard declaration (# of parameters passed from command line + array of "strings" - however the language implements that - which are the actual arguments from command line)

DVK
Just FYI - "Main" is pretty much Built-in in VB.NET. You actually have to work in order to expose it and put your own implementation in place...
Reed Copsey
VB has main()? (Honest surprise - my last exposure to any Basic at all was back in the days of Sinclair Z80 PC ownership...
DVK
A: 

It's a user defined function that is called by the language's runtime library. For example, a C runtime library will grab the command line arguments and sometimes environment variables from the operating system and pass them into to your main() function.

Different language runtimes usually perform the same operation in one form or another, and will throw some sort of error if the function it tries to call doesn't exist.

Bill Casarin
+4  A: 

I'm not sure what you mean by built-in vs. user defined. Almost no language actually gives your user-defined function the privilege of being the true entry-point into the program. C++, any .NET language, and Java all have hidden (built-in) entry point methods that in turn call your user-defined Main method (or whatever the entrypoint method for that language is called -- in .NET it can be named anything, although C# and VB.NET force it to be called Main).

So yes, virtually every language has a concept of a method that is automatically called, and this method is a user-defined method and usually mandatory. But virtually every language also has a built-in entry point method that actually sets up the framework and/or memory management for the process before invoking your user-defined "entry-point" function.

Andrew Arnott
Nice answer. I would just add that the name 'main' is not truly important, as most decent linkers will allow you to specify your entry point manually. (e.g. /ENTRY for VS)
Aurélien Vallée
A: 

It's declaration is built-in. It's definition is user supplied, or in some cases supplied by an application framework that has some other entry point, or in the case of most event-driven GUI frameworks, no single user-defined entry point.

Clifford
+4  A: 

Quote from the C Standard (emphasis is mine):

5.1.2.1 Freestanding environment

  1. In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined. Any library facilities available to a freestanding program, other than the minimal set required by clause 4, are implementation-defined.

main(), in a freestanding environment, is very much a user-defined function.

pmg
A: 

In Java main(String[] args) is the entry point for applications by convention (to make C++ programmers comfortable). For applets or servlets the invocation of code happens differently. Note that a jar may contain any or none of these entry points and that each class may contain a main so a given jar can be invoked in many different ways as an applcation if so desired.

Thorbjørn Ravn Andersen