C/C++ 中 void main 和 int main 的区别?

发布于 2024-07-15 10:54:51 字数 50 浏览 6 评论 0 原文

在 C++(或 C)程序中声明 main 函数的方式重要吗?

Does it matter which way I declare the main function in a C++ (or C) program?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

别想她 2024-07-22 10:54:51

区别在于,一个是定义 main 的正确方法,另一个则不是。

是的,这确实很重要。 或

int main(int argc, char** argv)

int main()

根据 C++ 规范对 main 的正确定义。

void main(int argc, char** argv) 过去

和现在都不是,IIRC,是旧版 Microsoft C++ 编译器附带的反常现象。

https://isocpp.org/wiki/faq/newbie#main-returns-int

The difference is one is the correct way to define main, and the other is not.

And yes, it does matter. Either

int main(int argc, char** argv)

or

int main()

are the proper definition of your main per the C++ spec.

void main(int argc, char** argv)

is not and was, IIRC, a perversity that came with older Microsoft's C++ compilers.

https://isocpp.org/wiki/faq/newbie#main-returns-int

站稳脚跟 2024-07-22 10:54:51

Bjarne Stroustrup 说得很清楚:

void main() 的定义不是、也从来不是 C++,甚至也不是 C。

参见 参考

Bjarne Stroustrup made this quite clear:

The definition void main() is not and never has been C++, nor has it even been C.

See reference.

疯到世界奔溃 2024-07-22 10:54:51

您应该使用int main。 C 和 C++ 标准都指定 main 应返回一个值。

You should use int main. Both the C and C++ standards specify that main should return a value.

两仪 2024-07-22 10:54:51

对于 C++,仅允许使用 int。 对于 C,C99 规定只允许使用 int。 先前的标准允许 void 返回。

简而言之,始终是int

For C++, only int is allowed. For C, C99 says only int is allowed. The prior standard allowed for a void return.

In short, always int.

饭团 2024-07-22 10:54:51

关键是,C 程序(和 C++ 相同)总是(应该?)返回成功值或错误代码,因此应该以这种方式声明它们。

The point is, C programs (and C++ the same) always (should?) return a success value or error code, so they should be declared that way.

遗失的美好 2024-07-22 10:54:51

很久以前,我发现 此页面 (void main(void)) 其中包含除了“标准说它无效”的论点之外,还有许多原因。 在特定的操作系统/体系结构上,它可能会导致堆栈损坏和/或发生其他令人讨厌的事情。

A long time ago I found this page (void main(void)) which contained many reasons outside of the "the standard says it is not valid" argument. On particular operating systems/architectures it could cause the stack to become corrupted and or other nasty things to happen.

梦里兽 2024-07-22 10:54:51

在 C++ 中,main() 必须返回 int。 但是,C99 允许 main() 有非 int 返回类型。 以下是 C99 标准的摘录。

5.1.2.2.1 程序启动

程序启动时调用的函数名为 main。 实施声明没有
该函数的原型。 它应该被定义为返回类型 int 并且没有
参数:

int main(void) { /* ... */ }

或带有两个参数(此处称为 argc 和 argv,但可以是任何名称)
使用,因为它们是声明它们的函数的本地函数):

int main(int argc, char *argv[]) { /* ... */ }

或同等水平; 或以其他一些实现定义的方式

另请注意,gcc 确实会编译 void main(),但实际上,它会在遇到右大括号时执行 return 0;

In C++, main() must return int. However, C99 allows main() to have a non-int return type. Here is the excerpt from the C99 standard.

5.1.2.2.1 Program startup

The function called at program startup is named main. The implementation declares no
prototype for this function. It shall be defined with a return type of int and with no
parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be
used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent; or in some other implementation-defined manner.

Also note that gcc does compile void main() although practically, it does a return 0; on encountering a closing brace.

四叶草在未来唯美盛开 2024-07-22 10:54:51

如果您遵循规范,那么您应该始终声明 main 返回 int

但实际上,大多数编译器都会让您选择其中任何一种,因此真正的区别在于您是否想要/需要向 shell 返回一个值。

If you're going by the spec, then you should always declare main returning an int.

In reality, though, most compilers will let you get away with either one, so the real difference is if you want / need to return a value to the shell.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文