exit(n)如何给函数返回值? [英] How can exit(n) give a function the return value?

查看:63
本文介绍了exit(n)如何给函数返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好专家,


以下代码段是否合法?如果是,exit()怎么做

关键字返回一个帮助并给主函数一个返回值?


可以函数调用(或只有这个exit(n))语句提供了

函数调用和C编程语言的返回功能?


/ *标题省略* /

int main(无效)

{

退出(0); / *而不是'return 0;''* /

}


此致,


lovecreatesbeauty

Hello experts,

Is the following code snippet legal? If it is, how can exit() do the
keyword return a favor and give a return value to the main function?

Can a function call (or only this exit(n)) statement provide both
function call and return features of the C programming language?

/* headers omitted */
int main (void)
{
exit(0); /* instead of `return 0;'' */
}

Sincerely,

lovecreatesbeauty

推荐答案

lovecreatesbeauty写道:
lovecreatesbeauty wrote:
你好专家,

以下是代码片段法律?如果是,exit()怎么做
关键字返回一个有利的并给主函数一个返回值?

一个函数调用(或只有这个出口(n))语句提供了C语言的函数调用和返回功能?

/ *标题省略* /
int main(void)
{
出口(0); / *而不是'return 0;''* /
}
Hello experts,

Is the following code snippet legal? If it is, how can exit() do the
keyword return a favor and give a return value to the main function?

Can a function call (or only this exit(n)) statement provide both
function call and return features of the C programming language?

/* headers omitted */
int main (void)
{
exit(0); /* instead of `return 0;'' */
}



我猜_exit(0)更接近返回(0),退出做一些清理<在终止之前处理
处理。


两者都将状态返回给调用者(如果有的话)。究竟如何执行这个魔术将是特定于平台的。


退出函数与返回的不同之处在于它们将终止

程序,无论他们在哪里打电话。


-

Ian Collins。


I guess _exit(0) is closer to return(0), exit does some clean-up
processing before terminating.

Both return the status to the caller (if there is one). Exactly how
this magic is performed would be platform specific.

The exit functions differ form return in that they will terminate the
program no matter where they are called.

--
Ian Collins.


lovecreatesbeauty写道:
lovecreatesbeauty wrote:
你好专家,

下面的代码片段合法吗?如果是,exit()怎么做
关键字返回一个有利的并给主函数一个返回值?

一个函数调用(或只有这个出口(n))语句提供了C语言的函数调用和返回功能?

/ *标题省略* /
int main(void)
{
出口(0); / *而不是'return 0;''* /
}
Hello experts,

Is the following code snippet legal? If it is, how can exit() do the
keyword return a favor and give a return value to the main function?

Can a function call (or only this exit(n)) statement provide both
function call and return features of the C programming language?

/* headers omitted */
int main (void)
{
exit(0); /* instead of `return 0;'' */
}




第一:是的,代码是合法的。但是exit()与`return''关键字没有任何关系,而且它没有为main()函数提供

返回值。当你调用exit()时,

main()根本不返回:exit()终止程序

而不返回。


考虑一个稍微复杂的程序:


#include< stdio.h>

#include< stdlib.h>

static void func(void);

int main(void){

puts(" before func()");

func();

puts(" After func()");

返回EXIT_SUCCESS;

}

static void func(void){

退出(EXIT_FAILURE);

}


显然,退出()不从func()返回值。如果
导致func()返回,main()将在

调用func()之后立即恢复,并继续执行后续的puts() -

我们知道不会发生。此外,func()是一个'void''

函数,根本不能返回任何值!所以一点点

的想法表明exit()和`return''是无关的,即使是

,尽管两者(在适当的环境中)都会导致程序终止。


-

Eric Sosman
es ** ***@acm-dot-org.inva lid



First: Yes, the code is legal. But exit() has nothing
to do with the `return'' keyword, and it does not supply a
return value for the main() function. When you call exit(),
main() does not return at all: exit() terminates the program
without ever returning.

Consider a slightly more complicated program:

#include <stdio.h>
#include <stdlib.h>
static void func(void);
int main(void) {
puts ("Before func()");
func();
puts ("After func()");
return EXIT_SUCCESS;
}
static void func(void) {
exit(EXIT_FAILURE);
}

Clearly, exit() does not return a value from func(). If it
caused func() to return, main() would resume right after the
call to func() and would proceed to the subsequent puts() --
which we know does not occur. Furthermore, func() is a `void''
function and cannot return any value at all! So a little
thought reveals that exit() and `return'' are unrelated, even
though both (in proper context) can cause program termination.

--
Eric Sosman
es*****@acm-dot-org.invalid


" lovecreatesbeauty" <螺*************** @ gmail.com>写道:
"lovecreatesbeauty" <lo***************@gmail.com> writes:
你好专家,

下面的代码片段合法吗?如果是,exit()怎么做
关键字返回一个有利的并给主函数一个返回值?

一个函数调用(或只有这个出口(n))语句提供了C语言的函数调用和返回功能?

/ *标题省略* /
int main(void)
{
出口(0); / *而不是'return 0;''* /
}
Hello experts,

Is the following code snippet legal? If it is, how can exit() do the
keyword return a favor and give a return value to the main function?

Can a function call (or only this exit(n)) statement provide both
function call and return features of the C programming language?

/* headers omitted */
int main (void)
{
exit(0); /* instead of `return 0;'' */
}




只有exit()可以做到这一点。它利用了

的特定知识如何退出并向操作系统提供结果代码;所以

没人可以简单地自己编写(便携)。


exit()主要是为了让你可以退出你的程序
/突然/,可能来自深深嵌套在你的
计划中的某个地方。它可能是最经常遇到的一个响应,即

程序遇到的错误情况。


作为一种风格问题,我永远不会打电话但是,从main返回()

与return关键字没有任何优势。



Only exit() can do this. It takes advantage of specific knowledge of
how to exit and provide a result code to the operating system; so
nobody would be able to simply write their own (portably).

exit() is provided mostly so that you can exit your program
/suddenly/, perhaps from somewhere deeply nested within your
program. It is probably most frequently encountered as a response to
an error condition that is encountered by the program.

As a matter of style, I would never call exit() from main, though: it
would provide no advantages over the return keyword.


这篇关于exit(n)如何给函数返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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