int main()的返回值是什么意思,如何显示?

2022-01-19 00:00:00 return c c++

可能重复:
main() 在 C/C++ 中应该返回什么?

我有这个简单的 C 程序:

I have this simple program in C:

#include <stdio.h>

int main()
{
  return 8;
}

返回值是什么意思,如何显示,或者如何使用?

What's the meaning of the return value, and how to display it, or how to use it?

推荐答案

返回值是什么意思?

用于将main()函数的状态返回给main()的调用者:

It is used to return the status of the main() function to the caller of main():

参考:
C++03 标准:18.3 开始和终止

最后,控制权返回到宿主环境.如果状态为零或 EXIT_SUCCESS,则返回成功终止状态的实现定义形式.如果状态是EXIT_FAILURE,返回状态不成功终止的实现定义形式.否则返回的状态是实现定义的.

Finally, control is returned to the host environment. If status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.

如何展示,或者如何使用?

就像显示或使用任何其他函数的返回值一样.main() 只是 C/C++ 中的另一个函数,在这方面 main 没有什么特别之处.

Just as you would display or use return value of any other function. main() is just another function in C/C++ there's nothing special about main in that regards.

相关文章