C++ 澄清返回 0 和 1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18671899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clarifying Return 0 and 1
提问by user2756669
So returning 0in a function (Not main)means falseand returning 1or anything but 0means success.
因此,在函数中返回0 (Not main)表示false并返回1或除0 以外的任何内容表示成功。
Why in the main() function do we put 0, meaning there were no errors when 1in other functions mean it ran successfully, thanks.
为什么在 main() 函数中我们放0,这意味着当其他函数中的1表示它运行成功时没有错误,谢谢。
回答by Daniel Frey
0
and 1
(or any non-zero number) convert to the boolean values false
and true
in a context where a conversion to bool
happens, e.g. in if(
here
) ...
. This is used withinyour program.
0
and 1
(或任何非零数字)转换为布尔值false
并true
在bool
发生转换的上下文中,例如在. 这在您的程序中使用。if(
here
) ...
The return value from main
is used in a different way, it is returned to the shell that called the program. In the shell's context the value is interpreted differently. There, 0
traditionally means "no error" while values larger than zero indicate errors and the value itself contains some hints as to what kind of error occurred. Take this snippet from man grep
:
from 的返回值main
以不同的方式使用,它返回到调用程序的 shell。在 shell 的上下文中,该值的解释不同。在那里,0
传统上意味着“没有错误”,而大于零的值表示错误,并且值本身包含一些关于发生何种错误的提示。从以下代码片段中获取man grep
:
EXIT STATUS
The grep utility exits with one of the following values:
0 One or more lines were selected.
1 No lines were selected.
>1 An error occurred.
回答by juergen d
Returning a certain number does not mean anything by default.
默认情况下,返回某个数字并不意味着什么。
The developer decides what a method returns and what the returned value means.
开发人员决定方法返回什么以及返回值的含义。
main
returns an exit code and 0
for no errorwhich is kind of straight forward and there are a lot more possible return values that just 0
and 1
.
main
返回退出代码,并0
为没有错误这是直截了当的种类和有很多更可能的返回值,只是0
和1
。
回答by HymanyZhu
There is no standard on which value represents failure and which represents success. However, traditionlly, C/C++/Unix choose to use 0 as success and non-zero for failure, because the non-zero value can be used as error code to represent a variety of failure reasons.
哪个值代表失败,哪个代表成功,没有标准。但是,传统上,C/C++/Unix 选择使用 0 表示成功,非零表示失败,因为非零值可以作为错误代码来表示各种失败原因。
回答by cmaster - reinstate monica
Think of it this way:
可以这样想:
By tradition, no function ever returns a boolean indicating success. Instead, the return type is either some error number or some object pointer.
按照传统,没有函数会返回指示成功的布尔值。相反,返回类型是某个错误号或某个对象指针。
Now, if an error code is returned and no error occurs, naturally no error is returned. This is the meaning of zero when main()
returns. Consequently, if(main())
means something like if(error)
.
现在,如果返回错误码并且没有发生错误,自然不会返回错误。这就是main()
返回时为零的含义。因此,if(main())
意味着类似于if(error)
。
Likewise, if an object pointer is returned, and an error occurs, no object can be returned, which again is indicated by zero (= a null pointer). So, if(getObject())
means something like if(/*getObject() actually returned something*/)
.
同样,如果返回一个对象指针,并且发生错误,则不能返回任何对象,这再次由零表示(= 空指针)。所以,if(getObject())
意味着类似if(/*getObject() actually returned something*/)
.
So, even though it looks the wrong way round, it is not. It's just that nothing ever directly returns a success boolean.
因此,即使它看起来是错误的,但事实并非如此。只是没有任何东西直接返回成功布尔值。
回答by ST3
Returned value for itself means nothing, developers decide if that value means error or not.
返回值本身没有任何意义,开发人员决定该值是否意味着错误。
But take a look at error codes and their handling.
但请看一下错误代码及其处理方式。
int MyFunctionWhichCanFail(...)
{
/*some code*/
return ERROR_CODE;
}
int FunctionWhichWorksWithFunctionWhichCanFail()
{
if (MyFunctionWhichCanFail(...))
{
//some error handling
}
//other stuff
}
In this case it is possible to occur many different errors, so error code is very good option, in cases there possible result failure/success is better use bool
.
在这种情况下,可能会出现许多不同的错误,因此错误代码是一个很好的选择,在可能导致失败/成功的情况下最好使用bool
。
Also I want to note, that in most systems zero is defined as ERROR_SUCCESS
(or some other keyword which means success).
我还想指出,在大多数系统中,零被定义为ERROR_SUCCESS
(或其他一些意味着成功的关键字)。