什么是退出并返回的区别? [英] What is the difference between exit and return?

查看:199
本文介绍了什么是退出并返回的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C程序从任何地方调用时是什么在C编程回报和退出声明的区别?

请注意:有期待的退出和主返回差异的另一个问题一>,但它是关于收益和退出之间的差异的问题,专家从main()函数(调用C全局对象++程序,某些操作系统的特殊行为的析构函数)调用时。


解决方案

  • 收益是从一个函数调用返回的语言的指令。

  • 退出是一个系统调用(而不是语言的语句)终止当前进程。

的唯一情况时都这样做(几乎)同样的事情是在的main()功能,从主返回执行退出()

与实例收益

 的#include<&stdio.h中GT;空隙f()的{
    的printf(执行˚F\\ n);
    返回;
}诠释主(){
    F();
    的printf(回到从步骤f \\ n);
}

如果你执行这个程序将打印:


 执行˚F
返回从步骤f


又如出口()

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;空隙f()的{
    的printf(执行˚F\\ n);
    出口(0);
}诠释主(){
    F();
    的printf(回到从步骤f \\ n);
}

如果你执行这个程序将打印:


 执行˚F


您永远不会回到由F。还要注意的#include<文件stdlib.h方式> 需要调用库函数退出()

另请注意,退出的参数()是一个整数(它的过程,发射过程中可以得到的返回状态,成功的常规用法是0或错误的任何其他值)。

return语句的参数是什么函数的返回类型为。如果该函数返回void,可以省略在函数结束时返回。

最后一点,退出()有两种形式 _exit()退出()。形式之间的区别是,退出()(从主返回)调用函数中使用注册的atexit() ON_EXIT()之前真正终止进程,而 _exit()(从的#include< unistd.h中> ,或的#include&LT的代名词_Exit; stdlib.h中> )立即终止进程。

即使问题关于C特别要求,我会加最后一块约C ++的有用信息。

C ++当它从功能退出(收益 -ing)执行比C更多的工作。具体来说它调用本地对象走出去的范围析构函数。在大多数情况下,程序员会不会太在乎一个程序的状态的突停止后,因此它不会作出太大的差别:分配的内存将被释放,文件的ressource关闭等。但是如果你的析构函数执行的IO它可能关系。例如自动C ++ 的ostream 本地创建不会在通话刷新退出,你可能会失去一些未刷新的数据(从另一方面静态的ostream 将被刷新)。

如果您使用的是良好的旧的C 这不会发生FILE * 流。这些将在退出被刷新()。事实上,规则是一样的,对注册的​​退出函数, FILE * 将在一切正常终止,其中包括退出()刷新,但不能调用 _exit()或中止()。

What is difference between return and exit statement in C programming when called from anywhere in a C program ?

Note: There is another question looking similar difference between exit and return in main but it is an expert question about differences between return and exit when called from the main() function (calling destructors of global objects in C++ programs, special behaviors of some OS).

解决方案

  • return is an instruction of the language that returns from a function call.
  • exit is a system call (not a language statement) that terminates the current process.

The only case when both do (nearly) the same thing is in the main() function, as a return from main performs an exit().

Example with return:

#include <stdio.h>

void f(){
    printf("Executing f\n");
    return;
}

int main(){
    f();
    printf("Back from f\n");
}

If you execute this program it prints:

Executing f
Back from f

Another example for exit():

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

void f(){
    printf("Executing f\n");
    exit(0);
}

int main(){
    f();
    printf("Back from f\n");
}

If you execute this program it prints:

Executing f

You never get "Back from f". Also notice the #include <stdlib.h> necessary to call the library function exit().

Also notice that the parameter of exit() is an integer (it's the return status of the process that the launcher process can get; the conventional usage is 0 for success or any other value for an error).

The parameter of the return statement is whatever the return type of the function is. If the the function returns void, you can omit the return at the end of the function.

Last point, exit() come in two flavors _exit() and exit(). The difference between the forms is that exit() (and return from main) calls functions registered using atexit() or on_exit() before really terminating the process while _exit() (from #include <unistd.h>, or its synonymous _Exit from #include <stdlib.h>) terminates the process immediately.

Even if the question ask specifically about C, I will add a last piece of useful information about C++.

C++ performs much more work than C when it is exiting from functions (return-ing). Specifically it calls destructors of local objects going out of scope. In most cases programmers won't care much of the state of a program after the processus stopped, hence it wouldn't make much difference: allocated memory will be freed, file ressource closed and so on. But it may matter if your destructor performs IOs. For instance automatic C++ OStream locally created won't be flushed on a call to exit and you may lose some unflushed data (on the other hand static OStream will be flushed).

This won't happen if you are using the good old C FILE* streams. These will be flushed on exit(). Actually, the rule is the same that for registered exit functions, FILE* will be flushed on all normal terminations, which includes exit(), but not calls to _exit() or abort().

这篇关于什么是退出并返回的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆