my-server
← Wiki Redirected from Argc

Entry point

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 <code>main</code>; 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 <code>main</code>; in CLI languages such as C# the entry point is a static method named <code>Main</code>.

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 <code>Main()</code> 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 <code>main</code>; in Java it is a static method named <code>main</code> (although the class must be specified at the invocation time), and in C# it is a static method named <code>Main</code>.

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 <code>e_entry</code> field of the ELF header. In the GNU Compiler Collection (gcc), the entry point used by the linker is the <code>_start</code> symbol. Similarly, in the Portable Executable format, used in Microsoft Windows, the entry point is specified by the <code>AddressOfEntryPoint</code> 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 <code>main</code> 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. <code>directory.exe$make</code>.

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 <code>atexit</code> 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 <code>main</code> 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:

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.

If using trailing return types, C++ also supports the following signatures of :

If the signature is (with no /), if command-line arguments are supplied, they will simply be ignored by the program.

The parameters , argument count, and , argument vector, respectively give the number and values of the program's command-line arguments. The names of and 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 ; for example, Unix (though not POSIX.1) and Windows have a third argument giving the program's environment, otherwise accessible through in <code>stdlib.h</code>:

Darwin-based operating systems, such as macOS, have a fourth parameter containing arbitrary OS-supplied information, such as the path to the executing binary:

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: (traditionally ) and . 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 is non-negative and that <code>argv[argc]</code> is a null pointer. By convention, the command-line arguments specified by and include the name of the program as the first element if is greater than 0; if a user types a command of "", the shell will initialise the <code>rm</code> process with and . As <code>argv[0]</code> is the name that processes appear under in <code>ps</code>, <code>top</code> etc., some programs, such as daemons or those running within an 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</code> system call.

On GCC and Clang, it is possible to call (or potentially 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.

The function is special; normally every C and C++ program must define it exactly once.

If declared, must be declared as if it has external linkage; it cannot be declared or .

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 is not otherwise reserved, and may be used for member functions, classes, enumerations, or non-member functions in other namespaces.

In 2017, there was a proposal for a "modern" signature for , more similar to Java and C# (which instead of , these languages have <code>main(String[])</code>, 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.

C#

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

Command-line arguments are passed in <code>args</code>, similar to how it is done in Java. For versions of <code>Main()</code> returning an integer, similar to both C and C++, it is passed back to the environment as the exit status of the process.

Like Java, the entry point of a program typically resides in a named class, like so:

Since C#7.1 there are four more possible signatures of the entry point, which allow asynchronous execution in the <code>Main()</code> Method.

The <code>Task</code> and <code>Task<int></code> types are the asynchronous equivalents of <code>void</code> and <code>int</code> (note that <code>Task<void></code> is invalid). <code>async</code> is required to allow the use of asynchronous calls (the <code>await</code> keyword) inside the method.

Clean

Clean is a functional programming language based on graph rewriting. The initial node is named <code>Start</code> and is of type <code>*World -> *World</code> if it changes the world or some fixed type if the program only prints the result after reducing <code>Start</code>.

Or even simpler

One tells the compiler which option to use to generate the executable file.

Common Lisp

ANSI Common Lisp does not define a main function; instead, the code is read and evaluated from top to bottom in a source file. However, the following code will emulate a main function.

D

In D, the function prototype of the main function looks like one of the following:

Command-line arguments are passed in <code>args</code>, similar to how it is done in C# or Java. For versions of <code>main()</code> returning an integer, similar to both C and C++, it is passed back to the environment as the exit status of the process.

Dart

Dart is a general-purpose programming language that is often used for building web and mobile applications. Like many other programming languages, Dart has an entry point that serves as the starting point for a Dart program. The entry point is the first function that is executed when a program runs. In Dart, the entry point is typically a function named <code>main</code> . When a Dart program is run, the Dart runtime looks for a function named <code>main</code> and executes it. Any Dart code that is intended to be executed when the program starts should be included in the <code>main</code> function. Here is an example of a simple <code>main</code> function in Dart:

In this example, the <code>main</code> function simply prints the text <code>Hello, world!</code> to the console when the program is run. This code will be executed automatically when the Dart program is run.

It is important to note that while the <code>main</code> function is the default entry point for a Dart program, it is possible to specify a different entry point if needed. This can be done using the <code>@pragma("vm:entry-point")</code> annotation in Dart. However, in most cases, the <code>main</code> function is the entry point that should be used for Dart programs.

FORTRAN

FORTRAN does not have a main subroutine or function. Instead a <code>PROGRAM</code> statement as the first line can be used to specify that a program unit is a main program, as shown below. The <code>PROGRAM</code> statement cannot be used for recursive calls.

Some versions of Fortran, such as those on the IBM System/360 and successor mainframes, do not support the PROGRAM statement. Many compilers from other software manufacturers will allow a fortran program to be compiled without a PROGRAM statement. In these cases, whatever module that has any non-comment statement where no SUBROUTINE, FUNCTION or BLOCK DATA statement occurs, is considered to be the Main program.

GNAT

Using GNAT, the programmer is not required to write a function named <code>main</code>; a source file containing a single subprogram can be compiled to an executable. The binder will however create a package <code>ada_main</code>, which will contain and export a C-style main function.

Go

In Go programming language, program execution starts with the <code>main</code> function of the <code>package main</code>

There is no way to access arguments or a return code outside of the standard library in Go. These can be accessed via <code>os.Args</code> and <code>os.Exit</code> respectively, both of which are included in the <code>"os"</code> package.

Haskell

A Haskell program must contain a name <code>main</code> bound to a value of type <code>IO t</code>, for some type <code>t</code>; which is usually <code>IO ()</code>. <code>IO</code> is a monad, which organizes side-effects in terms of purely functional code. The <code>main</code> value represents the side-effects-ful computation done by the program. The result of the computation represented by <code>main</code> is discarded; that is why <code>main</code> usually has type <code>IO ()</code>, which indicates that the type of the result of the computation is <code>()</code>, the unit type, which contains no information.

Command line arguments are not given to <code>main</code>; they must be fetched using another IO action, such as <code>System.Environment.getArgs</code>.

Java

Java programs start executing at the main method of a class, which has one of the following method headings:

Command-line arguments are passed in <code>args</code>. As in C and C++, the name "<code>main()</code>" is special. Java's main methods do not return a value directly, but one can be passed by using the <code>System.exit()</code> method.

Unlike C, the name of the program is not included in <code>args</code>, because it is the name of the class that contains the main method, so it is already known. Also unlike C, the number of arguments need not be included, since arrays in Java have a field that keeps track of how many elements there are.

The main function must be included within a class. This is because in Java everything has to be contained within a class. For instance, a hello world program in Java may look like:

To run this program, one must call <code>java HelloWorld</code> in the directory where the compiled class file <code>HelloWorld.class</code>) exists. Alternatively, executable JAR files use a manifest file to specify the entry point in a manner that is filesystem-independent from the user's perspective.

Since Java 25, it is possible to create a "compact source file" which implicitly declares a <code>final</code> class in the unnamed package. This class extends <code>java.lang.Object</code> and does not implement any interfaces, has only a default constructor, and has the fields and methods declared in the compact source file. Furthermore, Java 25 moves the class <code>java.io.IO</code> to the package <code>java.lang</code> (thus implicitly importing it into all source files), based on <code>System.out</code> and <code>System.in</code> rather than <code>java.io.Console</code>. This allows a simplified Hello World program, perhaps more similar to C and C++ where <code>main</code> resides in the global namespace:

JavaScript/TypeScript

In JavaScript and TypeScript, there is no "main" function as code is executed as soon as it is seen. However, it can be emulated like so:

If using Node.js, it is possible to emulate the Python-style pattern of :

Julia

In Julia the entry point (at least for scripts, see below for compiled programs) is the program file you itself you run, i.e. from the very first line so you can simply do (or start with):

Since Julia version 1.11 there's also the possibility of defining a <code>main</code> function that will be called as an entry point, and it can e.g. look like this if using the associated <code>@main</code> macro:

For compatibility with prior Julia versions, such as Julia 1.10 (LTS), the above line can be made to work with one extra line from the documentation, and using this way can help since the new "feature is intended to aid in the unification of compiled and interactive workflows." Other ways documented elsewhere are no longer needed.

The former <code>println</code> above showing the older way for an entry point without defining a <code>main</code> function is still perfectly fine for scripts (and ARGS is available in both cases, there directly from the a global variable; and PROGRAM_FILE in either case). Note if you do both, scripts will still run the first line as usual, and from there onward so this would also call <code>main</code> and print twice. But neither is necessarily the first code run unless you invoke Julia with <code>--startup-file=no</code> since the default Julia startup file is run before anything else (it's empty after install, but can easily be forgotten e.g. when benchmarking, if you've added to it).

The exit status is 0 by default (on success, throwing changes that), and <code>exit(my_exit_code)</code> exits the program with a non-default one.

Kotlin

In Kotlin, the main function is often top-level, like so:

In the Java Virtual Machine, the JVM bytecode will represent this as a <code>static</code> method of a class, as the JVM does not support top-level functions. For example, if the file is named :

LOGO

In FMSLogo, the procedures when loaded do not execute. To make them execute, it is necessary to use this code:

to procname ... ; Startup commands (such as print [Welcome]) end

make "startup [procname]

The variable <code>startup</code> is used for the startup list of actions, but the convention is that this calls a procedure that runs the actions. That procedure may be of any name.

OCaml

OCaml has no <code>main</code> function. Programs are evaluated from top to bottom.

Command-line arguments are available in an array named <code>Sys.argv</code> and the exit status is 0 by default.

Example:

Pascal

In Pascal, the main procedure is the only unnamed block in the program. Because Pascal programs define procedures and functions in a more rigorous bottom-up order than C, C++ or Java programs, the main procedure is usually the last block in the program. Pascal does not have a special meaning for the name "<code>main</code>" or any similar name.

Command-line arguments are counted in <code>ParamCount</code> and accessible as strings by <code>ParamStr(n)</code>, with n between 0 and <code>ParamCount</code>.

Versions of Pascal that support units or modules may also contain an unnamed block in each, which is used to initialize the module. These blocks are executed before the main program entry point is called.

Perl

In Perl, there is no main function. Statements are executed from top to bottom, although statements in a <code>BEGIN</code> block are executed before normal statements.

Command-line arguments are available in the special array <code>@ARGV</code>. Unlike C, <code>@ARGV</code> does not contain the name of the program, which is <code>$0</code>.

PHP

PHP does not have a "main" function. Starting from the first line of a PHP script, any code not encapsulated by a function header is executed as soon as it is seen.

Pike

In Pike syntax is similar to that of C and C++. The execution begins at <code>main</code>. The "<code>argc</code>" variable keeps the number of arguments passed to the program. The "<code>argv</code>" variable holds the value associated with the arguments passed to the program.

Example:

Python

Python programs are evaluated top-to-bottom, as is usual in scripting languages: the entry point is the start of the source code. Since definitions must precede use, programs are typically structured with definitions at the top and the code to execute at the bottom (unindented), similar to code for a one-pass compiler, such as in Pascal.

Alternatively, a program can be structured with an explicit <code>main</code> function containing the code to be executed when a program is executed directly, but which can also be invoked by importing the program as a module and calling the function. This can be done by the following idiom, which relies on the internal variable <code>__name__</code> being set to <code>__main__</code> when a program is executed, but not when it is imported as a module (in which case it is instead set to the module name); there are many variants of this structure:

In this idiom, the call to the named entry point <code>main</code> is explicit, and the interaction with the operating system (receiving the arguments, calling system exit) are done explicitly by library calls, which are ultimately handled by the Python runtime. This contrasts with C, where these are done implicitly by the runtime, based on convention.

QB64

The QB64 language has no main function, the code that is not within a function, or subroutine is executed first, from top to bottom:

Command line arguments (if any) can be read using the function:

Ruby

In Ruby, there is no distinct main function. Instead, code written outside of any <code>class .. end</code> or <code>module .. end</code> scope is executed in the context of a special "<code>main</code>" object. This object can be accessed using <code>self</code>:

It has the following properties:

Methods defined outside of a <code>class</code> or <code>module</code> scope are defined as private methods of the "<code>main</code>" object. Since the class of "<code>main</code>" is <code>Object</code>, such methods become private methods of almost every object:

The number and values of command-line arguments can be determined using the <code>ARGV</code> constant array:

The first element of <code>ARGV</code>, <code>ARGV[0]</code>, contains the first command-line argument, not the name of program executed, as in C. The name of program is available using <code>$0</code> or <code>$PROGRAM_NAME</code>.

Similar to Python, one could use:

to execute some code only if its file was specified in the <code>ruby</code> invocation.

Rust

In Rust, the entry point of a program is a function named <code>main</code>. By convention, this function is situated in a file called <code>main.rs</code>.

Additionally, as of Rust 1.26.0, the main function may return a <code>Result</code>:

Rust does not have parameters in the <code>main()</code> function like C++ and Java or other C-style languages. Instead, it accesses command-line arguments using <code>std::env::args()</code>, which returns <code>std::env::Args</code> and can then be converted to <code>Vec<String></code> using <code>.collect()</code>.

Swift

When run in an Xcode Playground, Swift behaves like a scripting language, executing statements from top to bottom; top-level code is allowed.

Cocoa- and Cocoa Touch-based applications written in Swift are usually initialized with the <code>@NSApplicationMain</code> and <code>@UIApplicationMain</code> attributes, respectively. Those attributes are equivalent in their purpose to the <code>main.m</code> file in Objective-C projects: they implicitly declare the <code>main</code> function that calls <code>UIApplicationMain(_:_:_:_:)</code> which creates an instance of <code>UIApplication</code>.

The following code is the default way to initialize a Cocoa Touch-based iOS app and declare its application delegate.

Visual Basic

In Visual Basic, when a project contains no forms, the startup object may be the <code>Main()</code> procedure. The <code>Command$</code> function can be optionally used to access the argument portion of the command line used to launch the program:

Xojo

In Xojo, there are two different project types, each with a different main entry point. Desktop (GUI) applications start with the <code>App.Open</code> event of the project's <code>Application</code> object. Console applications start with the <code>App.Run</code> event of the project's <code>ConsoleApplication</code> object. In both instances, the main function is automatically generated, and cannot be removed from the project.

See also

References

External links