早饱是什么意思| 女性吃大肠有什么好处| 做梦梦到狗是什么征兆| 老年人脚浮肿是什么原因| 冷战什么意思| 打封闭针是什么意思| 电是什么时候发明的| 做完痔疮手术吃什么好| 梦见亲人哭是什么征兆| 大便出血是什么原因引起的| 肝早期硬化身体有什么症状| 阿托品属于什么类药物| 薄荷叶泡水喝有什么功效和作用| 随机血糖是什么意思| 肚子拉稀像水一样是什么情况| 做梦梦见狗是什么意思| 玩世不恭是什么意思| 过敏看什么科| 什么什么的落叶| 齐多夫定片是治什么病的| 西四命是什么意思| 青梅煮酒什么意思| 庚辰五行属什么| 唏嘘是什么意思| 胎盘什么时候形成| 三月七号是什么星座| 汗疱疹涂什么药| 鼠目寸光是什么意思| 胃酸烧心吃什么药可以根治| 精神恍惚是什么意思| 早上起来眼睛肿是什么原因| 如法炮制是什么意思| 关节错缝术是什么意思| 产后漏尿是什么原因| 韩五行属什么的| 山楂和什么泡水喝减肥效果最好| 猫咪取什么名字好听| 气虚吃什么中药| 伟哥是什么药| 晚餐吃什么健康又营养| 月亮的肚子指的是什么| 高血压头晕吃什么药| 梦见大水是什么预兆| 胎儿生物物理评分8分什么意思| 摩羯座女和什么座最配| 什么水果蛋白质含量高| 不割包皮有什么影响吗| 什么水果能马上通便| 其余是什么意思| 艾灸灸出水泡是什么情况| 我靠是什么意思| 是什么样的感觉我不懂是什么歌| 帝王蟹什么季节吃最好| 疤痕增生是什么| 银针白毫是什么茶| 耳朵痒是什么原因引起的| 什么是文科| 梓是什么意思| 散瞳是什么意思| rhd阳性是什么意思| 风湿挂什么科| 莫须有是什么意思| 刘邦和刘备什么关系| 京兆尹是什么官| 文化内涵是什么意思| 蜈蚣吃什么| 中国的国球是什么球| 七月份生日是什么星座| 凤凰指什么生肖| 介入治疗是什么意思| 有什么有什么| 为什么说冬吃萝卜夏吃姜| 小美女是什么意思| 日新月异是什么意思| 啤酒不能和什么一起吃| 检查妇科清洁度三是什么意思| 来月经属于什么期| 卫青为什么被灭九族| 10.16是什么星座| 莲雾什么味道| 护士资格证有什么用| 血压高吃什么好| 胆固醇高挂什么科| 什么习习| 什么的李逵| 吃了避孕药后几天出血是什么原因| 睡觉总是流口水是什么原因| 网约车是什么意思| 1970年是什么命| 大什么大什么| 下葬有什么讲究或忌讳| 人流前需要检查什么项目| 五更泻吃什么药| 什么是转述句| 绿豆与什么食物相克| 睡觉头出汗是什么原因| 题词是什么意思| 10万个为什么| 恐龙什么时候灭绝的| 姜黄粉是什么| 子宫癌筛查做什么检查| 茱萸什么意思| 胆固醇低是什么原因| 朱元璋是什么民族| 晚上8点半是什么时辰| 牛仔裤配什么鞋| 脂蛋白高说明什么问题| 孕妇胆固醇高对胎儿有什么影响| 沙里瓦是什么意思| 布偶猫长什么样| 龙和什么属相最配| 木林森属于什么档次| 荨麻疹擦什么药| 早上起来心慌是什么原因| 金丝玉是什么玉| 空调自动关机是什么原因| 弄虚作假是什么生肖| 澳门的货币叫什么| ntr是什么意思啊| 龋牙是什么意思| 尿道感染有什么现象| 什么叫冷暴力| 葵水是什么意思| 横纹肌溶解症是什么原因造成的| 蚊虫叮咬用什么药| 阎王是什么意思| 什么药不能一起吃| 女人脾胃虚弱吃什么好| 为什么肚子会隐隐作痛| 南枝是什么意思| 一九六三年属什么生肖| 为什么会失眠| 荨麻疹长什么样| 周文王叫什么名字| 阳痿有什么症状| 腿膝盖疼是什么原因| yet是什么意思| 胆囊在什么位置| 风代表什么数字| 什么是好人| blackpink什么意思| 嘴巴里面起泡是什么原因引起的| 肾结石要注意什么| 月经前长痘痘是什么原因| 钙片什么牌子好| 农历6月是什么星座| 紫苏有什么作用| 鼻窦炎用什么药好| 嘴苦是什么原因| 孕妇生气对胎儿有什么影响| 卡罗莱手表是什么档次| 血糖高初期有什么症状| 吃猪腰子有什么好处和坏处| 心脏不好最忌讳吃什么| 凉席什么材质好| 小孩晚上睡觉发梦癫什么原因| 子宫切除后要注意什么| 为什么晚上睡不着| 支气管炎是什么症状| 梅毒单阳性是什么意思| 排恶露吃什么药| 药物流产最佳时间什么时候| 芥末是什么植物| 吃叶酸有什么好处| 地球属于什么星系| 一人吃饱全家不饿是什么生肖| 肝火旺盛是什么原因引起的| 收缩压低是什么原因| 鸭跖草用什么除草剂| 游字五行属什么| 强迫症吃什么药| 验光是什么意思| 肚子疼应该挂什么科| 铁低的原因是什么| 活塞是什么| 甲醇对人体有什么伤害| 什么动物不喝水| 衾怎么读什么意思| 没脑子是什么意思| 走马观花是什么意思| 经常低血糖是什么原因| 男性左下腹痛是什么原因| 去医院测血糖挂什么科| 花指什么生肖| 1月3日是什么星座| 嵌甲去医院挂什么科| 中性粒细胞百分比低是什么原因| 老虎五行属什么| 堃读什么| 生性凉薄什么意思| 丝状疣挂什么科| 日本兵为什么不怕死| 18岁是什么年华| 尿素偏高是什么原因| 脾大是什么病| 放任是什么意思| 晚上胃疼是什么原因| 疥疮是什么原因造成的| 2011是什么年| 包皮发炎用什么药| 头顶是什么穴位| 帝王蟹什么季节吃最好| 车字旁有什么字| 尿白细胞3十什么意思| singing是什么意思| 为什么医生不推荐特立帕肽呢| 蝉为什么会叫| 什么时间种白菜| 唾液酸苷酶阳性什么意思| fnc是什么意思| 人的五官指什么| 年庚是什么意思| 上善若水是什么意思| 理事是什么职位| 转铁蛋白阳性什么意思| 牙发黑是什么原因怎么办| 狗狗为什么会得细小| 头晕目眩是什么原因| 久卧伤气是什么意思| 血糖是什么引起的| 什么是量子力学| 四月四号是什么星座| 三陪是什么| 鼻子痒是什么原因| 裙带菜是什么菜| 痔疮是什么感觉| 早晨8点是什么时辰| 什么的浪花| 什么魂什么魄| bowdor是什么牌子的手表| 乏力是什么症状| 钱丢了预示着什么| 怀璧其罪是什么意思| 12月7号是什么星座| 为什么穿堂风最伤人| 阑是什么意思| 肠道ct能检查什么| 什么时间吃水果比较好| 吉数是什么数字| 心动过缓吃什么药最好| 心肌炎是什么| 30岁是什么之年| 性生活是什么| 番外是什么意思| 庆大霉素治疗鱼什么病| 1207是什么星座| 什么是前庭功能| 高密度脂蛋白偏高是什么意思| 军师是什么意思| 什么是氙气| 木色是什么颜色| 什么是衰老| 做梦梦见离婚是什么意思| 为什么不能摸猫的肚子| 胃胀腹胀吃什么药| 脑供血不足是什么症状| 肠易激综合征吃什么中成药| 未时是什么时辰| 什么是圆房| 血红蛋白低说明什么| 补充b族维生素有什么好处| 股票roe是什么意思| 谷丙转氨酶高挂什么科| 抑郁症有什么症状| 百度Jump to content

诺奖得主席勒:美国梦已变买房梦,中国梦或可推动经济成功

From Wikipedia, the free encyclopedia
(Redirected from Main function)
Example of the main function, in C#.
How the 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.[failed verification][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 main; as a result, the entry point is often known as the main function.[3]

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.[4]

Usage

[edit]

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

[edit]

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.[5][6]

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.[7]

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

[edit]

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.[clarification needed]

Exit point

[edit]

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

[edit]

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

[edit]

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++

[edit]

In C and C++, the function prototype of the main function must be equivalent to one of the following:

int main();
int main(void);
int main(int argc, char **argv);

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 parameters argc, argument count, and argv, argument vector,[citation needed] 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;[8] 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:[9]

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 return 0; at the end of the main() 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 "rm file", the shell will initialise the rm process with argc = 2 and argv = {"rm", "file", NULL}. As argv[0] 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[0] would be the name of the host executable), may choose to alter their argv to give a more descriptive argv[0], usually by means of the exec system call.

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

If declared, main() must be declared as if it has external linkage; it cannot be declared static or inline.

In C++, main() must be in the global namespace (i.e. ::main), 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) main() cannot be called recursively and cannot have its address taken.

C#

[edit]

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

static void Main();
static void Main(string[] args);
static int Main();
static int Main(string[] args);

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

Since C#7.1 there are four more possible signatures of the entry point, which allow asynchronous execution in the Main() Method.[11]

static async Task Main()
static async Task<int> Main()
static async Task Main(string[])
static async Task<int> Main(string[])

The Task and Task<int> types are the asynchronous equivalents of void and int. async is required to allow the use of asynchrony (the await keyword) inside the method.

Clean

[edit]

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

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 Lisp

[edit]

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.

(defun hello-main ()
  (format t "Hello World!~%"))

(hello-main)

D

[edit]

In 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 args, similar to how it is done in C# or Java. For versions of main() returning an integer, similar to both C and C++, it is passed back to the environment as the exit status of the process.

Dart

[edit]

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 main . When a Dart program is run, the Dart runtime looks for a function named main and executes it. Any Dart code that is intended to be executed when the program starts should be included in the main function. Here is an example of a simple main function in Dart:

void main() {
  print("Hello, world!");
}

In this example, the main function simply prints the text Hello, world! 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 main 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 @pragma("vm:entry-point") annotation in Dart. However, in most cases, the main function is the entry point that should be used for Dart programs.

FORTRAN

[edit]

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

      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.

GNAT

[edit]

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

Go

[edit]

In Go programming language, program execution starts with the main function of the package main

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 os.Args and os.Exit respectively, both of which are included in the "os" package.

Haskell

[edit]

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

main :: IO ()
main = putStrLn "Hello, World!"

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

Java

[edit]

Java programs start executing at the main method of a class,[15][16][17][18] which has one of the following method headings:

public static void main(String[] args)
public static void main(String... args)
public static void main(String args[])
void main()

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

Unlike C, the name of the program is not included in args, 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:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}

To run this program, one must call java HelloWorld in the directory where the compiled class file HelloWorld.class) 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.

[edit]

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 startup 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

[edit]

OCaml has no main function. Programs are evaluated from top to bottom.

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

Example:

print_endline "Hello World"

Pascal

[edit]

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 "main" or any similar name.

program Hello(Output);
begin
  writeln('Hello, world!');
end.

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

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

[edit]

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

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

PHP

[edit]

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

[edit]

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

Example:

 int main(int argc, array(string) argv)

Python

[edit]

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 main 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 __name__ being set to __main__ 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:[19][20][21]

import sys

def main(argv):
    n = int(argv[1])
    print(n + 1)

if __name__ == "__main__":
    sys.exit(main(sys.argv))

In this idiom, the call to the named entry point main 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

[edit]

The 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)

Ruby

[edit]

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

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 class or module scope are defined as private methods of the "main" object. Since the class of "main" is Object, such methods become private methods of almost every object:

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 ARGV constant array:

$ 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 ARGV, ARGV[0], contains the first command-line argument, not the name of program executed, as in C. The name of program is available using $0 or $PROGRAM_NAME.[22]

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 ruby invocation.

Rust

[edit]

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

// In `main.rs`
fn main() {
    println!("Hello, World!");
}

Additionally, as of Rust 1.26.0, the main function may return a Result:[23]

fn main() -> Result<(), std::io::Error> {
    println!("Hello, World!");

    Ok(())  // Return a type `Result` of value `Ok` with the content `()`, i.e. an empty tuple.
}

Swift

[edit]

When run in an Xcode Playground,[24] 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 @NSApplicationMain and @UIApplicationMain attributes, respectively. Those attributes are equivalent in their purpose to the main.m file in Objective-C projects: they implicitly declare the main function that calls UIApplicationMain(_:_:_:_:)[25] which creates an instance of UIApplication.[26]

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 Basic

[edit]

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

Sub Main()
    Debug.Print "Hello World!"
    MsgBox "Arguments if any are: " & Command$
End Sub

Xojo

[edit]

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

See also

[edit]

References

[edit]
  1. ^ "In Computing, what is an Entry Point? (with picture)". wiseGEEK. Archived from the original on 2025-08-06. Retrieved 2025-08-06.
  2. ^ Decker, Karsten M.; Rehmann, René M. (1994). Programming Environments for Massively Parallel Distributed Systems: Working Conference of the Ifip Wg 10.3, April 25-29, 1994. Springer Science & Business Media. ISBN 978-3-7643-5090-1.
  3. ^ "Main Method in C#". GeeksforGeeks. 2025-08-06. Archived from the original on 2025-08-06. Retrieved 2025-08-06.
  4. ^ Wagner, Bill (2025-08-06). "Main() / Entry Points (C# Programming Guide) - Microsoft Developer Network". docs.microsoft.com. Archived from the original on 2025-08-06. Retrieved 2025-08-06.
  5. ^ "The main() function". ibm.com. IBM. Archived from the original on 2025-08-06. Retrieved 2025-08-06.
  6. ^ "Main() and Command-Line Arguments (C# Programming Guide)". Msdn.microsoft.com. Archived from the original on 2025-08-06. Retrieved 2025-08-06.
  7. ^ "Application Fundamentals". Android Development. linuxtopia.org. Archived from the original on 2025-08-06. Retrieved 2025-08-06.
  8. ^ Section 3.6.1.2, Standard C++ 2011 edition.
  9. ^ "The char *apple Argument Vector". Archived from the original on 2025-08-06. Retrieved 2025-08-06.
  10. ^ "Console Applications in .NET, or Teaching a New Dog Old Tricks". Msdn.microsoft.com. 2025-08-06. Archived from the original on 2025-08-06. Retrieved 2025-08-06.
  11. ^ "The official repo for the design of the C# programming language: Dotnet/Csharplang". GitHub. 2025-08-06.
  12. ^ XL FORTRAN for AIX. Language Reference. Third Edition, 1994. IBM
  13. ^ "The Haskell 98 Report: Modules". Haskell.org. Archived from the original on 2025-08-06. Retrieved 2025-08-06.
  14. ^ Some Haskell Misconceptions: Idiomatic Code, Purity, Laziness, and IO Archived 2025-08-06 at the Wayback Machine — on Haskell's monadic IO>
  15. ^ "The Java Language Environment". Oracle. Archived from the original on 2025-08-06. Retrieved 2025-08-06. Within the HelloWorld class, we declare a single method called main() which in turn contains a single method invocation to display the string "Hello world!" on the standard output. The statement that prints "Hello world!" does so by invoking the println method of the out object. The out object is a class variable in the System class that performs output operations on files.
  16. ^ Schildt, Herbert (2019). Java : a beginner's guide. New York: McGraw-Hill Education. p. 46. ISBN 978-1-260-44022-5. OCLC 1061561931. A JAVA program begins with a call to main ().
  17. ^ "Hello, World! - Free Interactive Java Tutorial". Learn Java. Retrieved 2025-08-06. In Java, every line of code that can actually run needs to be inside a class. "public class Main {}" declares a class named Main, which is public, that means that any other class can access it.
  18. ^ "Hello, World! - Free Interactive Java Tutorial". Learn Java. Retrieved 2025-08-06. "public static void main(String[] args) {} " is the entry point of our Java program. the main method has to have this exact signature in order to be able to run our program.
  19. ^ Guido van Rossum (May 15, 2003). "Python main() functions". Archived from the original on July 11, 2015. Retrieved June 29, 2015,comments
  20. ^ Code Like a Pythonista: Idiomatic Python Archived 2025-08-06 at the Wayback Machine—on Python scripts used as modules
  21. ^ Ned Batchelder (6 June 2003). "Python main() functions". Archived from the original on 20 September 2015. Retrieved 29 June 2015.
  22. ^ class ARGF — on Ruby ARGV
  23. ^ "Releases.md". GitHub Rust. Archived from the original on 2025-08-06. Retrieved 15 February 2019.
  24. ^ Not to be confused with Swift Playgrounds Archived 2025-08-06 at the Wayback Machine, an Apple-developed iPad app for learning the Swift programming language.
  25. ^ "UIApplicationMain(_:_:_:_:) — UIKit". Apple Developer Documentation. Archived from the original on 2025-08-06. Retrieved 2025-08-06.
  26. ^ "UIApplication — UIKit". Apple Developer Documentation. Archived from the original on 2025-08-06. Retrieved 2025-08-06.
[edit]
法式刘海适合什么脸型 感冒了不能吃什么食物 吃什么可以解决便秘 精液长什么样 白巧克力是什么做的
宫颈糜烂是什么原因造成的 拉拉秧学名叫什么 被电击后身体会有什么后遗症 g代表什么 焦糖色是什么
优越感是什么意思 什么叫化疗为什么要化疗 肌肉一跳一跳什么原因 骨髓水肿吃什么消炎药 braf基因v600e突变是什么意思
2002年出生属什么 鸟大了什么林子都有 贝伐珠单抗是什么药 1月18日什么星座 内热是什么原因引起的
鱼子酱是什么东西wuhaiwuya.com 肛周水泡是什么病baiqunet.com 空调的几匹是什么意思hcv9jop8ns1r.cn 澳门为什么叫澳门hcv9jop1ns2r.cn 化疗是什么意思hcv8jop6ns4r.cn
尿突然是红褐色的是什么问题hcv8jop9ns0r.cn 受凉胃疼吃什么药hcv9jop0ns5r.cn 鸡打瞌睡吃什么药hcv8jop1ns1r.cn 白带什么颜色hcv9jop4ns6r.cn 户口本可以干什么坏事hcv8jop8ns4r.cn
肚子胀气吃什么食物好hcv8jop5ns7r.cn speedo是什么牌子liaochangning.com 脉压差小是什么原因hcv9jop6ns7r.cn 什么是聚酯纤维hcv8jop5ns6r.cn 人事是做什么的cl108k.com
吃什么补充胶原蛋白hcv8jop0ns4r.cn 五谷丰收是什么生肖hcv8jop1ns0r.cn 符号是什么hcv7jop9ns6r.cn 盲点是什么意思hcv9jop3ns4r.cn 梦见别人送钱给我是什么意思hcv8jop8ns5r.cn
百度