GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing en.wikipedia.org — Entry point - Wikipedia (Redirected from Program entry point) For other uses of "Entry Point" and "Point of Entry", see Point of Entry (disambiguation).
Main() might look in C# source code. Different parts are labeled for reference.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.[1] 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.[2] 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 In JVM languages, such as Java, the entry point is a static method called UsageEntry 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 ContemporaryIn 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 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 One exception to the single-entry-point paradigm is Android. Android applications do not have a single entry point – there is no special 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. HistoricalHistorically, 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. 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 pointIn 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 Programming languagesIn many programming languages, the 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: APLIn 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 The Prior to C89, C defined all functions without an explicit return type to return 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, auto main() -> int; auto main(int argc, char* argv[]) -> int; If the signature is The parameters 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:[11] 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: It is guaranteed that On GCC and Clang, it is possible to call #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, In C++, 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
import std; using std::optional; using std::tuple; using std::execution::task; using namespace std::literals; task<void> simulateWork() { std::println("Performing work..."); std::this_thread::read_env(get_scheduler); std::println("Work done!"); co_return; } int main() { optional<tuple<>> result = std::this_thread::sync_wait(simulateWork()); return static_cast<int>(result.has_value()); } As of C++26, the module wikipedia.examples; extern "C++" int main() { // ... } C#When executing a program written in C#, the CLR searches for a static method marked with the static void Main(); static void Main(string[] args); static int Main(); static int Main(string[] args); Command-line arguments are passed in Like Java, the entry point of a program typically resides in a named class, like so: namespace Wikipedia.Examples; using System; public class HelloWorld { static void Main(string[] args) { Console.WriteLine("Hello, world!"); } } Since C#7.1 there are four more possible signatures of the entry point, which allow asynchronous execution in the static async Task Main(); static async Task Main(string[] args); static async Task<int> Main(); static async Task<int> Main(string[] args); The namespace Wikipedia.Examples; using System; using System.Threading.Tasks; public class HelloWorld { static async Task<int> Main(string[] args) { Console.WriteLine("Hello..."); await Task.Delay(1000); Console.WriteLine("...world!"); return 0; } } CleanClean is a functional programming language based on graph rewriting. The initial node is named Start :: *World -> *World Start world = startIO ... Or even simpler Start :: String Start = "Hello, world!" One tells the compiler which option to use to generate the executable file. Common LispANSI 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. (defun hello-main () (format t "Hello World!~%")) (hello-main) DIn D, the function prototype of the main function looks like one of the following: void main(); void main(string[] args); int main(); int main(string[] args); Command-line arguments are passed in DartDart 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 void main() { print("Hello, world!"); } In this example, the While the FORTRANFORTRAN does not have a main subroutine or function. Instead a PROGRAM HELLO PRINT *, "Cint!" END PROGRAM HELLO 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. GNATUsing GNAT, the programmer is not required to write a function named GoIn Go programming language, program execution starts with the package main import "fmt" func main() { fmt.Println("Hello, World!") } There is no way to access arguments or a return code outside of the standard library in Go. These can be accessed via HaskellA Haskell program must contain a name main :: IO () main = putStrLn "Hello, World!" Command line arguments are not given to JavaJava programs start executing at the main method of a class,[23][24][25][26] which has one of the following method headings: // named class-based public static void main(); public static void main(String[] args); public static void main(String... args); // unnamed class-based void main(); void main(String[] args); void main(String... args); Note that in Java, Command-line arguments are passed in Unlike C, the name of the program is not included in 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: package org.wikipedia.examples; public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, world!"); } } To run this program, one must call Since Java 25, it is possible to create a "compact source file" which implicitly declares a void main() { IO.println("Hello, world!"); } JavaScript/TypeScriptIn 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: // Non-async version: function main(): void { console.log("Hello world"); } // Async version: async function main(): Promise<void> { console.log("Hello world"); } // In this pattern, only a call to main() is top-level // A good idea may be to call main() and catch any errors, // then cleanly log them like so main().catch((err) => { console.error(err); process.exit(1); }); If using Node.js, it is possible to emulate the Python-style pattern of function main(): void { console.log("Running as main module"); } if (require.main === module) { main(); } JuliaIn 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): println("Hello, world! Called from $PROGRAM_FILE with following arguments:"); for x in ARGS; println(x); end Since Julia version 1.11 there's also the possibility of defining a (@main)(ARGS) = println("Hello World! Called from main.") 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."[28] Other ways documented elsewhere[29] are no longer needed. The former The exit status is 0 by default (on success, throwing changes that), and KotlinIn Kotlin, the main function is often top-level, like so: fun main() { println("No arguments") } // With arguments: fun main(args: Array<String>) { println("First argument: ${args[0]}") } // concise-style: fun main(args: Array<String>) = println(args.joinToString()) In the Java Virtual Machine, the JVM bytecode will represent this as a public final class MainKt { public static void main(String[] args) { System.out.println("First argument: " + args[0]); } } LOGOIn 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 OCamlOCaml has no Command-line arguments are available in an array named Example: print_endline "Hello World" PascalIn 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 " program Hello(Output); begin writeln('Hello, world!'); end. Command-line arguments are counted in 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. PerlIn Perl, there is no main function. Statements are executed from top to bottom, although statements in a Command-line arguments are available in the special array PHPPHP 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. PikeIn Pike syntax is similar to that of C and C++. The execution begins at Example: int main(int argc, array(string) argv) PythonPython 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 import sys def main(argv: list[str]) -> int: argc: int = len(argv) # get length of argv n: int = int(argv[1]) print(n + 1) return 0 if __name__ == "__main__": sys.exit(main(sys.argv)) In this idiom, the call to the named entry point QB64The QB64 language has no main function, the code that is not within a function, or subroutine is executed first, from top to bottom: print "Hello World! a ="; a = getInteger(1.8d): print a function getInteger(n as double) getInteger = int(n) end function Command line arguments (if any) can be read using the COMMAND$ function: dim shared commandline as string commandline = COMMAND$ 'Several space-separated command line arguments can be read using COMMAND$(n) commandline1 = COMMAND$(2) RubyIn Ruby, there is no distinct main function. Instead, code written outside of any irb(main):001:0> self => main It has the following properties: irb(main):002:0> self.class => Object irb(main):003:0> self.class.ancestors => [Object, Kernel, BasicObject] Methods defined outside of a irb(main):004:0> def foo irb(main):005:1> 42 irb(main):006:1> end => nil irb(main):007:0> foo => 42 irb(main):008:0> [].foo NoMethodError: private method `foo' called for []:Array from (irb):8 from /usr/bin/irb:12:in `<main>' irb(main):009:0> false.foo NoMethodError: private method `foo' called for false:FalseClass from (irb):9 from /usr/bin/irb:12:in `<main>' The number and values of command-line arguments can be determined using the $ irb /dev/tty foo bar tty(main):001:0> ARGV ARGV => ["foo", "bar"] tty(main):002:0> ARGV.size ARGV.size => 2 The first element of Similar to Python, one could use: if __FILE__ == $PROGRAM_NAME # Put "main" code here end to execute some code only if its file was specified in the RustIn Rust, the entry point of a program is a function named // In main.rs fn main() { println!("Hello, World!"); }
use std::io; fn main() -> Result<(), io::Error> { println!("Hello, World!"); Ok(()) // Return a type Result of value Ok with the content (), the unit type. } In the Rust standard library, the following types have implementations for
Rust does not have parameters in the use std::env::{self, Args}; use std::io; fn main() -> Result<(), io::Error> { let args: Args = env::args(); // get the command line args let args_vec: Vec<String> = args.collect(); // collect args into a Vec<String> for arg in args_vec { println!("{}", arg); } Ok(()) } SwiftWhen run in an Xcode Playground,[35] Swift behaves like a scripting language, executing statements from top to bottom; top-level code is allowed. // HelloWorld.playground let hello = "hello" let world = "world" let helloWorld = hello + " " + world print(helloWorld) // hello world Cocoa- and Cocoa Touch-based applications written in Swift are usually initialized with the The following code is the default way to initialize a Cocoa Touch-based iOS app and declare its application delegate. // AppDelegate.swift import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { return true } } Visual BasicIn Visual Basic, when a project contains no forms, the startup object may be the Sub Main() Debug.Print "Hello World!" MsgBox "Arguments if any are: " & Command$ End Sub XojoIn Xojo, there are two different project types, each with a different main entry point. Desktop (GUI) applications start with the See also
References
External links
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |