main() in C, C++, Java, C#

旧时模样 提交于 2019-12-28 06:49:33

问题


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


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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




回答5:


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)




回答6:


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.




回答7:


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.




回答8:


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.




回答9:


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.



来源:https://stackoverflow.com/questions/1539385/main-in-c-c-java-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!