In computer programming, an entry point is the place in a program where the execution of a program begins, and where the program has access to command line arguments.
To start a program's execution, the loader or operating system passes control to its entry point. (During booting, the operating system itself is the program). This marks the transition from load time (and dynamic link time, if present) to run time.
For some operating systems and programming languages, the entry point is in a runtime library, a set of support functions for the language. The library code initializes the program and then passes control to the program proper. In other cases, the program may initialize the runtime library itself.
In simple systems, execution begins at the first statement, which is common in interpreted languages, simple executable formats, and boot loaders. In other cases, the entry point is at some other known memory address which can be an absolute address or relative address (offset).
Alternatively, execution of a program can begin at a named point, either with a conventional name defined by the programming language or operating system or at a caller-specified name. In many C-family languages, this is a function called main; as a result, the entry point is often known as the main function.
In JVM languages, such as Java, the entry point is a static method called main; in CLI languages such as C# the entry point is a static method named Main.
Usage Entry points apply both to source code and to executable files. However, in day-to-day software development, programmers specify the entry points only in source code, which makes them much better known. Entry points in executable files depend on the application binary interface (ABI) of the actual operating system, and are generated by the compiler or linker (if not fixed by the ABI). Other linked object files may also have entry points, which are used later by the linker when generating entry points of an executable file.
Entry points are capable of passing on command arguments, variables, or other information as a local variable used by the Main() method. This way, specific options may be set upon execution of the program, and then interpreted by the program. Many programs use this as an alternative way to configure different settings, or perform a set variety of actions using a single program.
Contemporary In most of today's popular programming languages and operating systems, a computer program usually only has a single entry point.
In C, C++, D, Zig, Rust and Kotlin programs this is a function named main; in Java it is a static method named main (although the class must be specified at the invocation time), and in C# it is a static method named Main.
In many major operating systems, the standard executable format has a single entry point. In the Executable and Linkable Format (ELF), used in Unix and Unix-like systems such as Linux, the entry point is specified in the e_entry field of the ELF header. In the GNU Compiler Collection (gcc), the entry point used by the linker is the _start symbol. Similarly, in the Portable Executable format, used in Microsoft Windows, the entry point is specified by the AddressOfEntryPoint field, which is inherited from COFF. In COM files, the entry point is at the fixed offset of 0100h.
One exception to the single-entry-point paradigm is Android. Android applications do not have a single entry point there is no special main function. Instead, they have essential components (activities and services) which the system can load and run as needed.
An occasionally used technique is the fat binary, which consists of several executables for different targets packaged in a single file. Most commonly, this is implemented by a single overall entry point, which is compatible with all targets and branches to the target-specific entry point. Alternative techniques include storing separate executables in separate forks, each with its own entry point, which is then selected by the operating system.
Historical Historically, and in some contemporary legacy systems, such as VMS and OS/400, computer programs have a multitude of entry points, each corresponding to the different functionalities of the program. The usual way to denote entry points, as used system-wide in VMS and in PL/I and MACRO programs, is to append them at the end of the name of the executable image, delimited by a dollar sign ($), e.g. directory.exe$make.
The Apple I computer also used this to some degree. For example, an alternative entry point in Apple I's BASIC would keep the BASIC program useful when the reset button was accidentally pushed.
Exit point In general, programs can exit at any time by returning to the operating system or crashing. Programs in interpreted languages return control to the interpreter, but programs in compiled languages must return to the operating system, otherwise the processor will simply continue executing beyond the end of the program, resulting in undefined behavior.
Usually, there is not a single exit point specified in a program. However, in other cases runtimes ensure that programs always terminate in a structured way via a single exit point, which is guaranteed unless the runtime itself crashes; this allows cleanup code to be run, such as atexit handlers. This can be done by either requiring that programs terminate by returning from the main function, by calling a specific exit function, or by the runtime catching exceptions or operating system signals.
Programming languages In many programming languages, the main function is where a program starts its execution. It enables high-level organization of the program's functionality, and typically has access to the command arguments given to the program when it was executed.
The main function is generally the first programmer-written function that runs when a program starts, and is invoked directly from the system-specific initialization contained in the runtime environment (crt0 or equivalent). However, some languages can execute user-written functions before main runs, such as the constructors of C++ global objects.
In other languages, notably many interpreted languages, execution begins at the first statement in the program.
A non-exhaustive list of programming languages follows, describing their way of defining the main entry point:
APL In APL, when a workspace is loaded, the contents of "quad LX" (latent expression) variable is interpreted as an APL expression and executed.
C and C++ In C and C++, the function prototype of the main function must be equivalent to one of the following:
int main(); int main(int argc, char* argv[]);
Note that int main(); may equivalently be expressed as int main(void); and int main(int argc, char* argv[]); may equivalently be expressed as int main(int argc, char** argv);; within function parameters, these signatures are identical to the compiler.
The function is special; normally every C and C++ program must define it exactly once.
Prior to C89, C defined all functions without an explicit return type to return int; some main functions could simply be main() { /* ... */ }. Additionally, void main(); is a signature that is accepted on some compilers, but not officially part of the language; in such cases, the value returned to the host environment is unspecified.
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.
With trailing return types, main() may also be equivalently expressed as: auto main() -> int; auto main(int argc, char* argv[]) -> int;
If the signature is (without argc and argv) and command-line arguments are supplied, they will simply be ignored by the program.
The parameters argc (for argument count) and argv (for argument vector) respectively give the number and values of the program's command-line arguments. The names of argc and argv 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 int;Section 3.6.1.2, Standard C++ 2011 edition. for example, Unix (though not POSIX.1) and Windows have a third argument giving the program's environment, otherwise accessible through getenv() in <stdlib.h>:
int main(int argc, char* argv[], char* envp[]);
Darwin-based operating systems, such as macOS, have a fourth parameter containing arbitrary OS-supplied information, such as the path to the executing binary:
int main(int argc, char* argv[], char* envp[], char* apple[]);
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: EXIT_SUCCESS (traditionally 0) and EXIT_FAILURE. The meaning of other possible return values is implementation-defined. In case a return value is not defined by the programmer, an implicit at the end of the function is inserted by the compiler; this behavior is required by the C++ standard.
It is guaranteed that argc is non-negative and that argv[argc] is a null pointer. By convention, the command-line arguments specified by argc and argv include the name of the program as the first element if argc is greater than 0; if a user types a command of "", the shell will initialise the rm process with and . As argv is the name that processes appear under in ps, top etc., some programs, such as daemons or those running within an interpreter or virtual machine (where argv would be the name of the host executable), may choose to alter their argv to give a more descriptive argv, usually by means of the exec system call.
On GCC and Clang, it is possible to call , change its signature, or even select a different function as the entry point by passing the compiler flag and then defining the function . Arguments from command line to can be retrieved using inline assembly, or using system-dependent APIs (such as GetCommandLineW() on Win32, _NSGetArgv() on Darwin (Apple), or directly reading on Linux).
#include <stdio.h> #include <stdlib.h>
void _start() { int exitCode = Main(); exit(exitCode); }
// note the name is Main(), not main() int Main() { printf("No main() function in this program."); return 0; }
If declared, must be declared as if it has external linkage; it cannot be declared static or inline.
In C++, must be in the global namespace (i.e. ), and cannot be overloaded. In C++ (unlike C) cannot be called recursively and cannot have its address taken. If is not defined in the global namespace (for example if it is only defined as a member function of a class), the compiler will not detect it. The name main() is not otherwise reserved, and may be used for member functions, classes, enumerations, or non-member functions in other namespaces.
import std;
using std::span; using std::string_view; using std::vector;
class Main { public: static void main(span<string_view> args) { std::println("Java-style main() function with {} arguments:", args.size());
for (string_view arg : args) { std::println(" {}");
int main(int argc, char* argv[]) { vector<string_view> args(argv, argv + argc); Main::main(args); return 0; }
In 2017, there was a proposal for a modernised signature for , more similar to Java and C# (which instead of , these languages use the signature , taking an array of strings). The suggested signature was (as at the time, was not yet part of the language), however this proposal was rejected. Another proposal for global command-line arguments access through a std::arguments class, similar to Rust's std::env::args() and C#'s System.Environment.GetCommandLineArgs(), was made in 2024.