How a program begins execution in computer programming
What is an entry point?
In computer programming, an entry point is the specific location where the execution of a program begins. When a program is started, the loader or the operating system passes control to this designated entry point. This action marks a transition from the time when the program is being loaded or linked to the time when it is actively running.
The entry point can be located in several places. For some operating systems and programming languages, the entry point resides within a runtime library. This library provides essential support functions for the language and is responsible for initializing the program before passing control to the main program code. In other situations, the program itself is responsible for initializing the runtime library.
How does execution start?
The way execution begins varies depending on the system and the language. In simpler systems, the program may start at the very first statement of the code. This approach is common in interpreted languages, simple executable formats, and boot loaders.
In more complex scenarios, the entry point might be at a specific memory address, which can be an absolute address or a relative offset. Alternatively, execution can begin at a named point. This name might be a conventional name defined by the programming language or the operating system, or it might be a name provided by the caller.
For many programs written in C-family languages, the entry point is a function called main. Because of this convention, the term entry point is often used interchangeably with main function.
How is the entry point used?
Entry points are relevant to both the source code and the final executable files. While programmers typically specify these points in the source code, the entry points in executable files are determined by the application binary interface of the operating system and are generated by the compiler or linker.
The entry point is also a place where command arguments, variables, and other data can be passed into the program as a local variable accessible by the main method. This allows specific options to be set when the program is launched, and the program can then interpret these options to configure settings or perform different actions using a single program.
In modern programming, most popular languages and operating systems feature a single entry point. For example, in C, C++, D, Zig, and Rust, this is a function named main. In Java, it is a static method named main, and in C#, it is a static method named Main.
What about other entry points?
While a single entry point is the norm today, there are exceptions. Android applications, for instance, do not have a single main function. Instead, the system loads and runs essential components like activities and services as needed.
Historically, and in some older systems, programs could have multiple entry points. These multiple points corresponded to different functionalities within the program. The way these points were denoted varied across different systems.
The concept of an exit point is different from an entry point. While a program can exit at any time by returning to the operating system or by crashing, some runtimes ensure a structured termination via a single exit point. This guarantees that cleanup code, such as atexit handlers, can run before the program finishes.
This article was written with AI assistance from multiple news reports and is reviewed by our editors under our editorial principles. Editorial Standards · Tips & Contact
Comments 0