What are the entry points in different languages?
Like in C, main() is the entry point.
What are the entry points in different languages?
Like in C, main() is the entry point.
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'.
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.
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.
here it is:
An external function with the identifier main, which is the first user function -- after exit routines and C++ static object constructors -- to ... www.lnf.infn.it/Calcolo/doc/aixcxx/html/glossary/m.htm
In some programming languages, the main function is where a program starts execution. en.wikipedia.org/wiki/Main_function_(programming)
Google is friend of us.
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.
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).
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
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.
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
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!"