为什么在C程序中从main()返回0? [重复]

时间:2022-09-10 23:43:59

This question already has an answer here:

这个问题在这里已有答案:

When writing a C/C++ program, specifically with latest compilers, why do we need to return an integer from the main() method? Like int main() and we return "return 0" from it. So what is the exact reason behind this?

在编写C / C ++程序时,特别是使用最新的编译器,为什么我们需要从main()方法返回一个整数?就像int main()一样,我们从它返回“return 0”。那背后的确切原因是什么?

5 个解决方案

#1


3  

It returns the 0 to OS to tell the OS that your program executed successfully.

它返回0到OS,告诉操作系统程序执行成功。

#2


11  

The return value of main() becomes the exit status of the process. Traditionally, an exit status of zero usually means “OK,” while any non-zero value indicates some kind of error. This is analogous with how many system calls likewise return zero or an error code.

main()的返回值成为进程的退出状态。传统上,退出状态为零通常意味着“OK”,而任何非零值都表示某种错误。这类似于有多少系统调用同样返回零或错误代码。

Even more information at J. Leffler's epic answer to this, similar question: What should main() return in C and C++?

更多关于J. Leffler对此的史诗回答的更多信息,类似的问题:main()应该在C和C ++中返回什么?

#3


8  

As you say, main() is declared as int main(). The OS expects an integer back, so it knows what to do next, especially if another program or script invoked your program. 0 means "no error." Anything else means an error occurred.

如你所说,main()被声明为int main()。操作系统需要一个整数,因此它知道下一步该做什么,特别是如果另一个程序或脚本调用了你的程序。 0表示“没有错误”。其他任何意味着发生了错误。

#4


4  

The int value returned by main(), if any, is the program's return value to 'the system'. A zero value from main() indicates success. A nonzero value indicates failure. If no value is returned, the system will receive a value indicating successful completion.

main()返回的int值(如果有)是程序返回“系统”的值。 main()的零值表示成功。非零值表示失败。如果未返回任何值,系统将收到一个表示成功完成的值。

#5


2  

See Why default return value of main is 0 and not EXIT_SUCCESS?.

请参阅为什么main的默认返回值为0而不是EXIT_SUCCESS?。

Returning zero from main() does essentially the same as what you're asking. Returning zero from main() does not have to return zero to the host environment.

从main()返回零基本上与您所要求的相同。从main()返回零不必将零返回到主机环境。

From the C90/C99/C++98 standard document:

从C90 / C99 / C ++ 98标准文件:

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

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

In other words, the specific value indicates success.

换句话说,具体值表示成功。

#1


3  

It returns the 0 to OS to tell the OS that your program executed successfully.

它返回0到OS,告诉操作系统程序执行成功。

#2


11  

The return value of main() becomes the exit status of the process. Traditionally, an exit status of zero usually means “OK,” while any non-zero value indicates some kind of error. This is analogous with how many system calls likewise return zero or an error code.

main()的返回值成为进程的退出状态。传统上,退出状态为零通常意味着“OK”,而任何非零值都表示某种错误。这类似于有多少系统调用同样返回零或错误代码。

Even more information at J. Leffler's epic answer to this, similar question: What should main() return in C and C++?

更多关于J. Leffler对此的史诗回答的更多信息,类似的问题:main()应该在C和C ++中返回什么?

#3


8  

As you say, main() is declared as int main(). The OS expects an integer back, so it knows what to do next, especially if another program or script invoked your program. 0 means "no error." Anything else means an error occurred.

如你所说,main()被声明为int main()。操作系统需要一个整数,因此它知道下一步该做什么,特别是如果另一个程序或脚本调用了你的程序。 0表示“没有错误”。其他任何意味着发生了错误。

#4


4  

The int value returned by main(), if any, is the program's return value to 'the system'. A zero value from main() indicates success. A nonzero value indicates failure. If no value is returned, the system will receive a value indicating successful completion.

main()返回的int值(如果有)是程序返回“系统”的值。 main()的零值表示成功。非零值表示失败。如果未返回任何值,系统将收到一个表示成功完成的值。

#5


2  

See Why default return value of main is 0 and not EXIT_SUCCESS?.

请参阅为什么main的默认返回值为0而不是EXIT_SUCCESS?。

Returning zero from main() does essentially the same as what you're asking. Returning zero from main() does not have to return zero to the host environment.

从main()返回零基本上与您所要求的相同。从main()返回零不必将零返回到主机环境。

From the C90/C99/C++98 standard document:

从C90 / C99 / C ++ 98标准文件:

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

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

In other words, the specific value indicates success.

换句话说,具体值表示成功。