Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Entry point
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===C and C++=== In [[C (programming language)|C]] and [[C++]], the [[function prototype]] of the main function must be equivalent to one of the following: <syntaxhighlight lang="c"> int main(); int main(void); int main(int argc, char **argv); </syntaxhighlight> The main function is the entry point for application programs written in ISO-standard C or C++. Low-level system programming (such as for a [[bare-metal]] [[embedded system]]) might specify a different entry point (for example via a reset [[interrupt vector]]) using functionality not defined by the language standard. The [[parameter (computer science)|parameters]] <code>argc</code>, ''argument count'', and <code>argv</code>, ''argument vector'',{{cn|date=January 2025}} respectively give the number and values of the program's [[command-line argument]]s. The names of <code>argc</code> and <code>argv</code> may be any valid identifier, but it is common convention to use these names. Other platform-dependent formats are also allowed by the C and C++ standards, except that in C++ the return type must always be <code>int</code>;<ref>Section 3.6.1.2, Standard C++ 2011 edition.</ref> for example, [[Unix]] (though not [[POSIX.1]]) and [[Windows]] have a third argument giving the program's [[environment variable|environment]], otherwise accessible through <code>getenv</code> in <code>[[stdlib.h]]</code>: <syntaxhighlight lang="c"> int main(int argc, char **argv, char **envp); </syntaxhighlight> [[Darwin (operating system)|Darwin]]-based operating systems, such as [[macOS]], have a fourth parameter containing arbitrary OS-supplied information, such as the path to the executing binary:<ref>{{Cite web |url=http://unixjunkie.blogspot.com/2006/02/char-apple-argument-vector.html |title=The <code>char *apple</code> Argument Vector |access-date=2014-05-12 |archive-url=https://web.archive.org/web/20151222173356/http://unixjunkie.blogspot.com/2006/02/char-apple-argument-vector.html |archive-date=2015-12-22 |url-status=dead }}</ref> <syntaxhighlight lang="c"> int main(int argc, char **argv, char **envp, char **apple); </syntaxhighlight> The value returned from the main function becomes the [[exit status]] of the process, though the C standard only ascribes specific meaning to two values: <code>EXIT_SUCCESS</code> (traditionally 0) and <code>EXIT_FAILURE</code>. The meaning of other possible return values is implementation-defined. In case a return value is not defined by the programmer, an implicit <code>return 0;</code> at the end of the <code>main()</code> function is inserted by the compiler; this behavior is required by the C++ standard. It is guaranteed that <code>argc</code> is non-negative and that <code>argv[argc]</code> is a [[null pointer]]. By convention, the command-line arguments specified by <code>argc</code> and <code>argv</code> include the name of the program as the first element if <code>argc</code> is greater than 0; if a user types a command of "<code>rm file</code>", the [[shell (computing)|shell]] will initialise the <code>[[rm (Unix)|rm]]</code> process with <code>argc = 2</code> and <code>argv = {"rm", "file", NULL}</code>. As <code>argv[0]</code> is the name that processes appear under in <code>[[ps (Unix)|ps]]</code>, <code>[[top (software)|top]]</code> etc., some programs, such as [[daemon (computing)|daemons]] or those running within an [[interpreter (computing)|interpreter]] or [[virtual machine]] (where <code>argv[0]</code> would be the name of the host executable), may choose to alter their argv to give a more descriptive <code>argv[0]</code>, usually by means of the <code>[[exec (system call)|exec]]</code> system call. The <code>main()</code> function is special; normally every C and C++ program must define it exactly once. If declared, <code>main()</code> must be declared as if it has external linkage; it cannot be declared <code>static</code> or <code>inline</code>. In C++, <code>main()</code> must be in the global [[namespace]] (i.e. <code>::main</code>), cannot be overloaded, and cannot be a [[member function]], although the name is not otherwise reserved, and may be used for member functions, classes, enumerations, or non-member functions in other namespaces. In C++ (unlike C) <code>main()</code> cannot be called [[recursion (computer science)|recursively]] and cannot have its address taken.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)