views:

470

answers:

12

What are the entry points in different languages?

Like in C, main() is the entry point.

A: 

Most languages I know use main(). Pascal is the sole exception, it uses a function with the name of the program itself. I didn't understand your question, what do you mean by "definition for main()" ? As far as I know, the definition for 'main()' is 'starting point of the program'.

Leahn Novash
+1  A: 

In C, main is not really a keyword. It's a predefined function name that is invoked by the standard library when your program loads. That's why the kind of error you get when you create a program without a main() function is a linker error -- the linker is trying to resolve the standard library's reference to a main() that you supply, and it's not finding anything.

In face, even in C there are compilation environments where you don't use main(). For example in some Windows programs you would use _tmain() or WinMain() instead.

There's no particular best definition for main(). You can use or omit the standard arguments main( int argc, char* argv[] ) with most compliers, and the body may constitute your entire program (for a simple script) or it may be nearly empty (if you're handing off control to an object oriented architecture).

In a scripting language there may not even be an entry point, your code just starts executing from the top of the file. So there's no common standard, just what the language designer decided.

Eric
STAThread does not indicate the entry point of the program. It it related to COM interop. The compiler always uses the Main method as entry point. In case you have more than one class with a Main method, you specify the correct one through a command line parameter.
Antoine Aubry
right you are; thanks
Eric
A: 

Many scripting languages don't have a main function. They just start executing from the top of the file. For instance, Python doesn't strictly have a main function; it just has a builtin variable called __name__ that's set to __main__ when the script is being run on its own. If you want to test for that and call a function called main (or anything else), that's up to you.

Jim Hunziker
+1  A: 

here it is:

Google is friend of us.

Lukas Šalkauskas
A: 

Are you asking for a survey of what other runtimes (this is not language specifc) use? Or are you asking what is recommended for C specifically?

The default entry point in C is 'main', but the function can have different signatures:

void main()
int main()
int main(int argc, char** argv)
// etc

In that case, the best depends on your usage. Is an exit code part of your normal program flow (i.e. a command-line utility) or exceptional? Do you have arguments to parse? etc.

BTW- Most linkers will let you override the default entry point. For Visual C at least, main is called by the VC runtime and the actual entry point for the application is something like _MainCRTStartup.

Brannon
+1  A: 

In C#, you designate the entry point to a program by marking the appropriate function with a "[STAThread]" attribute

This is not correct. The [STAThread] attribute is related to thread apartments (needed with COM interop). It has nothing to do with entry points and can be applied to any function (but might not have any effect).

On Freund
Damn, why are so many people downvoting correct and helpful answers?
Konrad Rudolph
I'm not sure I understand the context of your question.
On Freund
A: 

In Ruby, there is no distinct main function. The code written without additional "class .. end", "module .. end" enclosures is executed directly, step by step, in context of special "main" object. - Wikipedia

Prakash
+1  A: 

In C# When executing a program written in C#, the CLR searches for a static method marked with the .entrypoint IL directive, which takes either no arguments, or a single argument of type string[], and has a return type of void or int, and executes it - Wikipedia

Prakash
A: 

In Pascal, the main procedure is the only unnamed procedure in the program. Because Pascal programs have the procedures and functions in a more rigorous top-down order than C, C++ or Java programs, the main procedure is usually the last procedure in the program. Pascal does not have a special meaning for the name "main" or any similar name - Wikipedia

Prakash
A: 

I find it helpful to always use

int main(int argc, char *argv[])

as libraries like SDL which hook into your main function look for a specific signature.

Josh Matthews
+2  A: 

I think the most general form is:

int main(int argc, char *argv[], char *env[])

but you could remove some of the parameters (env, env and argv, env and argv and argc).



env is used to access the environmental variables (such as PATH, LIB, TEMP, etc).

argv is used to access the command line parameters (includng the executable filename, in argv[0]).

argc contains the number of the command line parameters (including the executable filename).


Also, proably most compilers will not complain if you would like your main function not to return anything:
void main ...etc
botismarius
+1  A: 

Most OCaml code uses the pattern:

let main () = print_endline "Hello World!"

let () = main ()

but this only simulates a main function - in reality each line of the input file gets executed. The above works the same as:

let _ = print_endline "Hello World!"
Thelema