我什么时候应该用'return 0'结束一个C ++程序? [重复]

时间:2022-09-10 23:26:53

This question already has an answer here:

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

I am new to C++ and I'm reading a book called Big C++. In the book, all of the example programs I've seen so far end with return 0; before the final }. I can apparently make C++ programs run without return 0; at the end, so I'm wondering what purpose it serves. I am familiar with returning something from a method in java, but I don't understand why int main() would need to return 0 in C++. To get more to the point: Should I always end my main() with return 0; in C++? If not, when do I need to and when shouldn't I? What is return 0; telling the program to do?

我是C ++的新手,我正在读一本名为Big C ++的书。在本书中,我到目前为止看到的所有示例程序都以返回0结束;在最后}之前。我显然可以让C ++程序运行而不返回0;最后,所以我想知道它的用途是什么。我熟悉从java中的方法返回一些东西,但我不明白为什么int main()需要在C ++中返回0。为了得到更多要点:我应该总是以返回0结束我的main();在C ++中?如果没有,我什么时候需要,何时不应该?什么是回报0;告诉程序要做什么?

In a related question, I am assuming that the main() in C++ is setting up the main function like the main method in java. Is that correct? Why is the main method being declared as an integer? Is that what is happening in the line int main() {?

在一个相关的问题中,我假设C ++中的main()正在设置main函数,就像java中的main方法一样。那是对的吗?为什么main方法被声明为整数?这是在行main(){?

4 个解决方案

#1


8  

that represents exit code of application ,

表示应用程序的退出代码,

For example if you launch it via some scripts and they want to determine if the program terminated normally exit code should be 0, non zero means some type of errors

例如,如果您通过某些脚本启动它并且他们想要确定程序是否正常终止,则退出代码应为0,非零意味着某种类型的错误

speaking of Java

谈到Java

if you have specified int as return type you must have to return otherwise it won't compile, in case of void type it is zero unless there is an exception thrown out of jvm

如果你已经指定int作为返回类型你必须返回否则它将不会编译,在void类型的情况下它是零,除非从jvm抛出异常

#2


7  

In principle, any function returning something different than void has to be exited using a return statement (unless an exception is thrown): falling off the end of a function returning something else than void results in undefined behavior.

原则上,任何返回与void不同的函数的函数都必须使用return语句退出(除非抛出异常):从函数的结尾处返回除void之外的其他内容会导致未定义的行为。

The beauty of C++ is that there are many exception to general rules: you can fall off the end of main() which behaves identical to using return EXIT_SUCCESS; which in turn is identical to return 0;. The reason for this special rule is something from the distant history of C and C++ as far as I understand (although I don't have any statement I could quote on this). In summary, although you don't need to return anything from main() using a suitable return statement is more consistent. I tend to omit it when putting code on slides for presentations but in real programs I always include a return statement.

C ++的优点在于一般规则有许多例外:你可以从main()的末尾掉下来,它的行为与使用return EXIT_SUCCESS相同;这又与返回0;相同。据我所知,这个特殊规则的原因来自C和C ++的遥远历史(虽然我没有任何声明,我可以引用这个)。总之,尽管您不需要使用合适的return语句从main()返回任何内容,但更加一致。在将代码放在幻灯片上进行演示时,我倾向于省略它,但在实际程序中,我总是包含一个return语句。

BTW, if you want to indicate that a C++ program failed, you should probably return EXIT_FAILURE which is aside from EXIT_SUCCESS the only other value guaranteed to work. In practice other return codes also work but there isn't any guarantee.

顺便说一句,如果你想表明一个C ++程序失败了,你应该返回EXIT_FAILURE,这是EXIT_SUCCESS的唯一保证工作的其他值。在实践中,其他返回代码也有效,但没有任何保证。

#3


4  

In case of C++, return 0; is implied. That is:

在C ++的情况下,返回0;隐含着。那是:

int main()
{
}

is equal to

等于

int main()
{
    return 0;
}

Note that main which serves as an entry point for your app is the only one that allows you to omit the return statement for non-void return type.

请注意,作为应用程序入口点的main是唯一一个允许您省略非void返回类型的return语句的main。

Apart from that, the value itself is used to inform any other process (e.g. parental process) about the execution result, e.g. by reading it through posix waitpid.

除此之外,值本身用于通知任何其他过程(例如父母过程)关于执行结果,例如,通过posix waitpid阅读它。

#4


1  

The main method is being declared as an integer, because you'd want it to return one. If the main method returns 0, it basically means everything went alright.

main方法被声明为整数,因为你希望它返回一个。如果main方法返回0,它基本上意味着一切正常。

#1


8  

that represents exit code of application ,

表示应用程序的退出代码,

For example if you launch it via some scripts and they want to determine if the program terminated normally exit code should be 0, non zero means some type of errors

例如,如果您通过某些脚本启动它并且他们想要确定程序是否正常终止,则退出代码应为0,非零意味着某种类型的错误

speaking of Java

谈到Java

if you have specified int as return type you must have to return otherwise it won't compile, in case of void type it is zero unless there is an exception thrown out of jvm

如果你已经指定int作为返回类型你必须返回否则它将不会编译,在void类型的情况下它是零,除非从jvm抛出异常

#2


7  

In principle, any function returning something different than void has to be exited using a return statement (unless an exception is thrown): falling off the end of a function returning something else than void results in undefined behavior.

原则上,任何返回与void不同的函数的函数都必须使用return语句退出(除非抛出异常):从函数的结尾处返回除void之外的其他内容会导致未定义的行为。

The beauty of C++ is that there are many exception to general rules: you can fall off the end of main() which behaves identical to using return EXIT_SUCCESS; which in turn is identical to return 0;. The reason for this special rule is something from the distant history of C and C++ as far as I understand (although I don't have any statement I could quote on this). In summary, although you don't need to return anything from main() using a suitable return statement is more consistent. I tend to omit it when putting code on slides for presentations but in real programs I always include a return statement.

C ++的优点在于一般规则有许多例外:你可以从main()的末尾掉下来,它的行为与使用return EXIT_SUCCESS相同;这又与返回0;相同。据我所知,这个特殊规则的原因来自C和C ++的遥远历史(虽然我没有任何声明,我可以引用这个)。总之,尽管您不需要使用合适的return语句从main()返回任何内容,但更加一致。在将代码放在幻灯片上进行演示时,我倾向于省略它,但在实际程序中,我总是包含一个return语句。

BTW, if you want to indicate that a C++ program failed, you should probably return EXIT_FAILURE which is aside from EXIT_SUCCESS the only other value guaranteed to work. In practice other return codes also work but there isn't any guarantee.

顺便说一句,如果你想表明一个C ++程序失败了,你应该返回EXIT_FAILURE,这是EXIT_SUCCESS的唯一保证工作的其他值。在实践中,其他返回代码也有效,但没有任何保证。

#3


4  

In case of C++, return 0; is implied. That is:

在C ++的情况下,返回0;隐含着。那是:

int main()
{
}

is equal to

等于

int main()
{
    return 0;
}

Note that main which serves as an entry point for your app is the only one that allows you to omit the return statement for non-void return type.

请注意,作为应用程序入口点的main是唯一一个允许您省略非void返回类型的return语句的main。

Apart from that, the value itself is used to inform any other process (e.g. parental process) about the execution result, e.g. by reading it through posix waitpid.

除此之外,值本身用于通知任何其他过程(例如父母过程)关于执行结果,例如,通过posix waitpid阅读它。

#4


1  

The main method is being declared as an integer, because you'd want it to return one. If the main method returns 0, it basically means everything went alright.

main方法被声明为整数,因为你希望它返回一个。如果main方法返回0,它基本上意味着一切正常。