main() in C, C++, Java, C#
Is main() (or Main()) in C, C++, Java or C#, a开发者_如何学编程 user-defined function or a built-in function?
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.
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.
Quote from the C Standard (emphasis is mine):
5.1.2.1 Freestanding environment
- 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.
It's a required user defined function (the entry point for executables)...
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:
Do some initialization code
Call
main()
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)
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.
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.
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.
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.
精彩评论