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

前端 未结 9 1460
心在旅途
心在旅途 2020-12-11 06:20

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

相关标签:
9条回答
  • 2020-12-11 06:50

    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.

    0 讨论(0)
  • 2020-12-11 06:54

    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.

    0 讨论(0)
  • 2020-12-11 06:57

    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.

    0 讨论(0)
提交回复
热议问题