我应该使用exit()还是只是在main()中返回语句?就我个人而言,我更喜欢return语句,因为我觉得它就像阅读任何其他函数一样,当我阅读代码时,流控制是流畅的(在我看来)。即使我想重构main()函数,使用return似乎比exit()更好。

exit()是否做了return没有做的特殊事情?


当前回答

至少有一个偏爱exit的原因:如果你的任何atexit处理程序引用main中的自动存储持续时间数据,或者如果你使用setvbuf或setbuf给一个标准流分配了main中的自动存储持续时间缓冲区,那么从main返回会产生未定义的行为,但调用exit是有效的。

另一种潜在的用法(通常只用于玩具程序)是通过递归调用main退出程序。

其他回答

当你的main函数被多次调用时,在main中exit(0)和return(0)实际上是有区别的。

下面的程序

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv) {
  if (argc == 0)
    return(0);
  printf("%d", main(argc - 1, argv));
}

运行

./program 0 0 0 0

将导致以下输出:

00000

但是这个:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv) {
  if (argc == 0)
    exit(0);
  printf("%d", main(argc - 1, argv));
}

不管参数如何,都不会打印任何东西。

如果你确定没有人会显式地调用你的main,一般来说在技术上没有太大的区别,但是保持更清晰的代码退出会看起来更好。如果你出于某种原因想要调用main -你应该调整它来满足你的需要。

解析:选C。

另一个区别: exit是标准库 函数,所以你需要包含 标头和链接与标准 图书馆。为了说明(在c++中), 这是一个有效的程序:

int main() { return 0; }

但是要使用exit,你需要一个include:

#include <stdlib.h>
int main() { exit(EXIT_SUCCESS); }

Plus this adds an additional assumption: that calling exit from main has the same side effects as returning zero. As others have pointed out, this depends on what kind of executable you're building (i.e., who's calling main). Are you coding an app that uses the C-runtime? A Maya plugin? A Windows service? A driver? Each case will require research to see if exit is equivalent to return. IMHO using exit when you really mean return just makes the code more confusing. OTOH, if you really do mean exit, then by all means use it.

exit()做了什么'return'没有做的特殊事情吗?

对于一些不常见平台的编译器,exit()可能会将其参数转换为程序的退出值,而main()的返回可能只是直接将值传递给宿主环境而不进行任何转换。

The standard requires identical behavior in these cases (specifically, it says returning something that's int-compatible from main() should be equivalent to calling exit() with that value). The problem is that different OSes have different conventions for interpreting the exit values. On many (MANY!) systems, 0 means success and anything else is a failure. But on, say, VMS, odd values mean success and even ones mean failure. If you returned 0 from main(), a VMS user would see a nasty message about an access violation. There wasn't actually an access violation--that was simply the standard message associated with failure code 0.

然后ANSI出现了,并将EXIT_SUCCESS和EXIT_FAILURE作为参数传递给exit()。标准还规定exit(0)的行为应该与exit(EXIT_SUCCESS)相同,因此大多数实现将EXIT_SUCCESS定义为0。

因此,该标准将您置于VMS的约束中,因为它没有留下任何标准方法来返回值恰好为0的故障代码。

因此,20世纪90年代早期的VAX/VMS C编译器不解释main()的返回值,它只是将任何值返回给主机环境。但是如果您使用exit(),它将做标准要求的事情:将EXIT_SUCCESS(或0)转换为成功代码,将EXIT_FAILURE转换为通用的失败代码。要使用EXIT_SUCCESS,你必须将它传递给exit(),你不能从main()返回它。我不知道该编译器的更现代版本是否保留了这种行为。

一个可移植的C程序是这样的:

#include <stdio.h>
#include <stdlib.h>

int main() {
  printf("Hello, World!\n");
  exit(EXIT_SUCCESS);  /* to get good return value to OS */
  /*NOTREACHED*/ /* to silence lint warning */
  return 0;  /* to silence compiler warning */
}

旁白:如果我没记错的话,VMS对出口值的约定比奇数/偶数更微妙。它实际上使用低三位来编码一个严重级别。然而,一般来说,奇数的严重级别表示成功或其他信息,偶数的严重级别表示错误。

我总是使用return,因为main()的标准原型说它确实返回int型。

也就是说,某些版本的标准对main进行了特殊处理,如果没有显式的return语句,则假定它返回0。给定以下代码:

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

g++仅为foo()生成一个警告,并忽略main的缺失返回值:

% g++ -Wall -c foo.cc
foo.cc: In function ‘int foo()’:
foo.cc:1: warning: control reaches end of non-void function

至少有一个偏爱exit的原因:如果你的任何atexit处理程序引用main中的自动存储持续时间数据,或者如果你使用setvbuf或setbuf给一个标准流分配了main中的自动存储持续时间缓冲区,那么从main返回会产生未定义的行为,但调用exit是有效的。

另一种潜在的用法(通常只用于玩具程序)是通过递归调用main退出程序。