C语言 使用exit()函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2425167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:49:33  来源:igfitidea点击:

Use of exit() function

c

提问by Kraken

I want to know how and when can I use the exit()function like the program in my book:

我想知道如何以及何时可以exit()像我书中的程序一样使用该功能:

#include<stdio.h>

void main()
{
    int goals;
    printf("enter number of goals scored");
    scanf("%d",&goals);

    if(goals<=5)
        goto sos;
    else
    {
        printf("hehe");
        exit( );
    }
    sos:
    printf("to err is human");
}

When I run it, it shows ERROR: call to undefined function exit().

当我运行它时,它显示 ERROR: call to undefined function exit()

Also, I want to know how I can create an option to close the window in which the program runs? For example, I made a menu-driven program which had several options and one of them was "exit the menu". How can I make this exit the program (i.e. close the window)?

另外,我想知道如何创建一个选项来关闭程序运行的窗口?例如,我制作了一个菜单驱动的程序,它有几个选项,其中之一是“退出菜单”。我怎样才能让它退出程序(即关闭窗口)?

回答by Klaus Byskov Pedersen

Try using exit(0);instead. The exitfunction expects an integer parameter. And don't forget to #include <stdlib.h>.

尝试使用exit(0);。该exit函数需要一个整数参数。并且不要忘记#include <stdlib.h>

回答by Tyler McHenry

The exitfunction is declared in the stdlib header, so you need to have

exit函数在 stdlib 标头中声明,因此您需要具有

#include <stdlib.h>

at the top of your program to be able to use exit.

在您的程序顶部,以便能够使用exit.

Note also that exittakes an integer argument, so you can't call it like exit(), you have to call as exit(0)or exit(42). 0 usually means your program completed successfully, and nonzero values are used as error codes.

还要注意,它exit接受一个整数参数,所以你不能像 那样调用它exit(),你必须调用 asexit(0)exit(42)。0 通常表示您的程序成功完成,非零值用作错误代码。

There are also predefined macros EXIT_SUCCESSand EXIT_FAILURE, e.g. exit(EXIT_SUCCESS);

还有预定义的宏EXIT_SUCCESSEXIT_FAILURE,例如exit(EXIT_SUCCESS);

回答by Morfildur

exit(int code);is declared in stdlib.hso you need an

exit(int code);被声明在stdlib.h所以你需要一个

#include <stdlib.h>

Also:
- You have no parameter for the exit(), it requires an intso provide one.
- Burn this book, it uses gotowhich is (for everyone but linux kernel hackers) bad, very, very, VERY bad.

另外:
- 你没有参数exit(),它需要一个int所以提供一个。
- 烧毁这本书,它使用的goto是(对于除 linux 内核黑客之外的所有人)糟糕的、非常、非常、非常糟糕的。

Edit:
Oh, and

编辑:
哦,还有

void main()

is bad, too, it's:

也很糟糕,它是:

int main(int argc, char *argv[])

回答by Bertrand Marron

Try man exit.

尝试man exit



Oh, and:

哦,还有:

#include <stdlib.h>

int main(void) {
  /*  ...  */
  if (error_occured) {
    return (EXIT_FAILURE);
  }
  /*  ...  */
  return (EXIT_SUCCESS);
}

回答by Nataraj Raja

The exit()function is a type of function with a return type without an argument. It's defined by the stdlib header file.

exit()功能是不带参数的类型与返回类型的功能。它由 stdlib 头文件定义。

You need to use ( exit(0) or exit(EXIT_SUCCESS))or (exit(non-zero)or exit(EXIT_FAILURE) ).

您需要使用( exit(0) or exit(EXIT_SUCCESS))(exit(non-zero)exit(EXIT_FAILURE) )

回答by Martin max

The following example shows the usage of the exit()function.

下面的例子展示了该exit()函数的用法。

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

int main(void) {
    printf("Start of the program....\n");
    printf("Exiting the program....\n");
    exit(0);
    printf("End of the program....\n");
    return 0;
}

Output

输出

Start of the program....
Exiting the program....

程序开始....
退出程序....

回答by user4508201

You must add a line with #include <stdlib.h>to include that header file and exitmust return a value so assign some integer in exit(any_integer).

您必须添加一行#include <stdlib.h>以包含该头文件,并且exit必须返回一个值,以便在exit(any_integer).

回答by ultimate cause

In addition to return an exit code to parent process -

除了将退出代码返回给父进程之外 -

In UNIX, an important aspect that I think has been left out is, that exit() at first calls (in reverse order) all those functions, which were registered by atexit() call.

在 UNIX 中,我认为被忽略的一个重要方面是,exit() 首先调用(以相反的顺序)所有由 atexit() 调用注册的函数。

Please refer to SUSv4 for details.

详情请参考SUSv4。

回答by filvarga

on unix like operating systems exit belongs to group of system calls. system calls are special calls which enable user code (your code) to call kernel code. so exit call makes some OS specific clean-up actions before returning control to OS, it terminates the program.

在像操作系统这样的 unix 上,exit 属于系统调用组。系统调用是特殊调用,它使用户代码(您的代码)能够调用内核代码。所以退出调用在将控制权返回给操作系统之前会执行一些操作系统特定的清理操作,它会终止程序。

#include <stdlib.h>

// example 1
int main(int argc, char *argv){
  exit(EXIT_SUCCESS);
}

// example 2
int main(int argc, char *argv){
  return 0;
}

Some compilers will give you the same opcode from both of these examples but some won't. For example opcode from first function will not include any kind of stack positioning opcode which will be included in the second example like for any other function. You could compile both examples and disassemble them and you will see the difference.

有些编译器会从这两个示例中为您提供相同的操作码,但有些则不会。例如,来自第一个函数的操作码将不包括任何类型的堆栈定位操作码,这些操作码将像任何其他函数一样包含在第二个示例中。您可以编译这两个示例并反汇编它们,您将看到不同之处。

You can use exit from any part of your code and be sure that process terminates. Don't forget to include integer parameter.

您可以在代码的任何部分使用 exit 并确保该进程终止。不要忘记包含整数参数。

回答by Azeem Alvi

Write header file #include<process.h>and replace exit();with exit(0);. This will definitely work in Turbo C; for other compilers I don't know.

写入头文件#include<process.h>并替换exit();exit(0);. 这肯定会在 Turbo C 中工作;对于我不知道的其他编译器。