c - 程序终止成功或失败的主要功能

时间:2021-10-24 09:35:25

As far as I understand if the main function returns 0 this indicates always successful program termination. Even if success is indicated by another int value.

据我所知,如果main函数返回0,则表示程序终止成功。即使成功由另一个int值指示。

If main returns a non-zero value it is implementation specific if this stands for unsuccessful program termination or another error code

如果main返回非零值,则它是特定于实现的,如果这代表不成功的程序终止或其他错误代码

If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If the value of status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.)

如果status的值为零或EXIT_SUCCESS,则返回状态成功终止的实现定义形式。如果status的值为EXIT_FAILURE,则返回状态为不成功终止的实现定义形式。否则返回的状态是实现定义的。)

So only success is defined in the C standard (return 0) and not how a non-zero int value is interpreted, right? E.g. if in a certain system 1 stands for "success" return 0 would be deliver 1 as well? How is this done?

因此,只有成功在C标准中定义(返回0)而不是如何解释非零int值,对吧?例如。如果在某个系统中1代表“成功”,那么返回0也会提供1个?这是怎么做到的?

1 个解决方案

#1


2  

The main function is the entry point to your code, it is not the entry point to the executable. The executable contains an operating-system defined entry point, that runs some startup code before calling main.

主要功能是代码的入口点,它不是可执行文件的入口点。可执行文件包含操作系统定义的入口点,在调用main之前运行一些启动代码。

main is called from the startup code just like a normal function. The return value from main is received by the startup code, which can perform any translations necessary to conform to the requirements of the operating system.

main就像普通函数一样从启动代码调用。 main的返回值由启动代码接收,启动代码可以执行符合操作系统要求所需的任何转换。

The startup code is specific to each operating system. Operating systems have requirements concerning the operation and environment of executables. The C language has requirements concerning the environment that the C code runs in (specifically the arguments to main and the return value from main). It's the responsibility of the startup code to bridge the gaps between those two sets of requirements.

启动代码特定于每个操作系统。操作系统具有关于可执行文件的操作和环境的要求。 C语言有关于C代码运行的环境的要求(特别是main的参数和main的返回值)。启动代码负责弥合这两组要求之间的差距。

The startup code is delivered as an object file, commonly called "crt.o", short for "C runtime". That file is included in the executable by the linker. You can find the actual name of that file by examining the linker command line. The startup file is typically the first file on the linker command line.

启动代码作为目标文件提供,通常称为“crt.o”,是“C runtime”的缩写。该文件由链接器包含在可执行文件中。您可以通过检查链接器命令行来查找该文件的实际名称。启动文件通常是链接器命令行上的第一个文件。

#1


2  

The main function is the entry point to your code, it is not the entry point to the executable. The executable contains an operating-system defined entry point, that runs some startup code before calling main.

主要功能是代码的入口点,它不是可执行文件的入口点。可执行文件包含操作系统定义的入口点,在调用main之前运行一些启动代码。

main is called from the startup code just like a normal function. The return value from main is received by the startup code, which can perform any translations necessary to conform to the requirements of the operating system.

main就像普通函数一样从启动代码调用。 main的返回值由启动代码接收,启动代码可以执行符合操作系统要求所需的任何转换。

The startup code is specific to each operating system. Operating systems have requirements concerning the operation and environment of executables. The C language has requirements concerning the environment that the C code runs in (specifically the arguments to main and the return value from main). It's the responsibility of the startup code to bridge the gaps between those two sets of requirements.

启动代码特定于每个操作系统。操作系统具有关于可执行文件的操作和环境的要求。 C语言有关于C代码运行的环境的要求(特别是main的参数和main的返回值)。启动代码负责弥合这两组要求之间的差距。

The startup code is delivered as an object file, commonly called "crt.o", short for "C runtime". That file is included in the executable by the linker. You can find the actual name of that file by examining the linker command line. The startup file is typically the first file on the linker command line.

启动代码作为目标文件提供,通常称为“crt.o”,是“C runtime”的缩写。该文件由链接器包含在可执行文件中。您可以通过检查链接器命令行来查找该文件的实际名称。启动文件通常是链接器命令行上的第一个文件。