为什么我们要使用'return 0'? [英] why do we have to use 'return 0' ?

查看:831
本文介绍了为什么我们要使用'return 0'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们编写的每个程序中几乎都有

Almost in every program we code there is mostly

return 0 ;

main 函数的末尾。我知道这意味着一切都在进行中。但是这是什么意思?究竟有什么背景呢?谢谢。

in the end of main function. I know This means all things are going on way. But what does it mean? What exactly are there at the background of this ? Thanks.

推荐答案

在每个 C 程序中,你必须使用return return 0; (或返回-1; ,或其他......),因为 main 函数签名需要它。

C ++ 程序中,该语句是可选的:编译器会自动添加 return 0; 如果你没有明确地返回一个值。



返回值是退出代码你的程序,shell(或运行它的任何其他应用程序)可以读取和使用它。

0 退出代码是'确定程序执行成功'的广泛接受的约定。
In every C program you have to use return return 0; (or return -1;, or whatever... ), because the main function signature requires it.
In a C++ program the statement is optional: the compiler automatically adds a return 0; if you don't explicitely return a value.

The return value is the exit code of your program, the shell (or any other application that ran it) can read and use it.
The 0 exit code is a widely accepted convention for 'OK the program execution was successfull'.


在C和C ++程序中 main 函数的类型为 int ,因此它应返回一个整数值。 main 函数的返回值被视为应用程序的退出状态。



请参阅: http://en.wikipedia.org/wiki/ Exit_status [ ^ ]



在大多数返回0的操作系统上,成功状态就像是说程序工作正常。在C ++中,可以选择在 main 函数的末尾键入 return 0; ,编译器包含它自动。



stdlib.h EXIT_SUCCESS EXIT_FAILURE 的定义如下:

In C and C++ programs the main function is of type int and therefore it should return an integer value. The return value of the main function is considered the "Exit Status" of the application.

See : http://en.wikipedia.org/wiki/Exit_status[^]

On most operating systems returning 0 is a success status like saying "The program worked fine". In C++ it is optional to type "return 0;" at the end of the main function and the compiler includes it automatically.

In stdlib.h the macros EXIT_SUCCESS and EXIT_FAILURE are defined like this :
#define EXIT_SUCCESS    0
#define EXIT_FAILURE    1





这两个宏可以用作 stdlib.h 中声明的 exit 函数的参数,也可以使用它们作为 main 函数的返回值。



These 2 macros can be used as the argument to the exit function declared in stdlib.h and they can also be used as the return value of the main function.


返回值对于检查应用程序退出时的状态非常有用。

返回0表示没有错误。
the return value is useful to check the status when the application exit.
return 0 means no error.


这篇关于为什么我们要使用'return 0'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆