Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 449
user366312 Avatar asked Oct 08 '09 17:10

user366312


People also ask

What is main () in C?

Every C program has a primary (main) function that must be named main. If your code adheres to the Unicode programming model, you can use the wide-character version of main, wmain. The main function serves as the starting point for program execution.

How does a main () function in Java differ from main () in C & C++?

Difference between C/C++ and Java main method As evident from the above method signatures, the main method in Java does not return a value since it has return type void while the main() method in C/C++ returns an integer since it has return type int . Also, the main method in C/C++ is non-static, unlike Java.

Is Main () valid in C?

No. It's non-standard. The standard prototype of main is int main() with the optional command line arguments argc and argv . The int returned by main() is a way for a program to return a value to the system that invokes it.

Is main () and int main () same?

The void main() indicates that the main() function will not return any value, but the int main() indicates that the main() can return integer type data.


5 Answers

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.

like image 192
geowa4 Avatar answered Oct 24 '22 06:10

geowa4


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.

like image 21
Andrew Arnott Avatar answered Oct 24 '22 06:10

Andrew Arnott


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.

like image 43
pmg Avatar answered Oct 24 '22 04:10

pmg


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

like image 45
Justin Niessner Avatar answered Oct 24 '22 06:10

Justin Niessner


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)

like image 34
DVK Avatar answered Oct 24 '22 05:10

DVK