exit c+

exit() is a library function in C/C++ programming language, it is used to terminate the calling process (function) immediately i.e. we can say it is used for the normal program termination and also perform the several cleanup steps.

exit()是C / C ++编程语言中的一个库函数,它用于立即终止调用进程(函数),即可以说它用于正常程序终止并执行几个清理步骤。

Syntax:

句法:

void exit(int status);

Parameter(s): status – defines the exit status.

参数: status –定义退出状态。

There are two status that we can define,

我们可以定义两种状态,

Value Macro Description
0 EXIT_SUCCESS It defines that the termination is on the success of the program.
1 EXIT_FAILURE It defines that the termination is on the failure of the program.
巨集 描述
0 EXIT_SUCCESS 它定义了程序成功的终止。
1个 EXIT_FAILURE 它定义终止是在程序失败时进行的。

Return value: The return type of the function is void, it returns nothing.

返回值:函数的返回类型为void ,不返回任何内容。

1) exit(0) or exit(EXIT_SUCCESS)

1)退出(0)或退出(EXIT_SUCCESS)

exit(0) is used to terminate the program by specifying the successful execution of the program.

exit(0)用于通过指定程序的成功执行来终止程序。

Syntax:

句法:

exit(0);

2) exit(1) or exit(EXIT_FAILURE)

2)退出(1)或退出(EXIT_FAILURE)

exit(1) is used to terminate the program by specifying the successful execution of the program.

exit(1)用于通过指定程序的成功执行来终止程序。

Syntax:

句法:

exit(1);

Example 1:

范例1:

Here, we are reading two numbers and then dividing them, if the second number is 0 then exiting the program using exit(1) and if the second number is not 0 then performing the division operation and exiting the program using exit(0).

在这里,我们正在读取两个数字,然后将它们相除,如果第二个数字为0,则使用exit(1)退出程序;如果第二个数字不为0,则执行除法运算,然后使用exit(0)退出程序。

// C++ program to demonstrate the
// example of exit(0) and exit(1)
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
cout << "Enter divisor  (value of a): ";
cin >> a;
cout << "Enter dividend (value of b): ";
cin >> b;
cout << "a: " << a << endl;
cout << "b: " << b << endl;
if (b == 0) {
cout << "Dividend must not be 0..." << endl;
// EXIT_FAILURE
exit(1);
}
cout << a << "/" << b << " : " << (float)a / (float)b << endl;
// EXIT_SUCCESS
exit(0);
}

Output:

输出:

RUN 1:
Enter divisor  (value of a): 10
Enter dividend (value of b): 3
a: 10
b: 3
10/3 : 3.33333
RUN 2:
Enter divisor  (value of a): 10
Enter dividend (value of b): 0
a: 10
b: 0
Dividend must not be 0...

Example 2:

范例2:

Here, we are opening a file in read-only mode, if the file doe not exit then exiting the program using exit(1), and file exists then closing it and exiting the program using exit(0).

在这里,我们以只读模式打开文件,如果文件没有退出,则使用exit(1)退出程序,并且文件存在,然后将其关闭并使用exit(0)退出程序。

// C++ program to demonstrate the
// example of exit(0) and exit(1)
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE* fPtr;
// opening the file
fPtr = fopen("hello1.txt", "r");
// checking whether file exists or not
if (fPtr == NULL) {
printf("oops, file does not exist...");
// EXIT_FAILURE
exit(1);
}
printf("File opened successfully...");
// clsoing the file
fclose(fPtr);
// EXIT_SUCCESS
exit(0);
}

Output:

输出:

oops, file does not exist...

出口(0)和出口(1)之间的区别 (Difference between exit(0) and exit(1))

exit(0) exit(1)
It is used to terminate the program normally by specifying that operation is successful. It is also used to terminate the program normally by specifying that operation is not successful.
The syntax is,

exit(0);

The syntax is,
0 is the value of EXIT_SUCCESS. Thus, we can also use exit(EXIT_SUCCESS) instead of exit(0). 1 is the value of EXIT_FAILURE. Thus, we can also use exit(EXIT_FAILURE) instead of exit(1).
exit(0) is fully portable. exit(1) is not fully portable.
exit(0) defines the clean exit without any error. exit(1) defines that there was an error in the program and the program is terminated.
退出(0) 出口(1)
通过指定操作成功,它通常用于终止程序。 通过指定操作不成功,它也可用于正常终止程序。
语法是

exit(0);

语法是
0EXIT_SUCCESS的值。 因此,我们也可以使用exit(EXIT_SUCCESS)代替exit(0) 1EXIT_FAILURE的值。 因此,我们也可以使用exit(EXIT_FAILURE)代替exit(1)
exit(0)是完全可移植的。 exit(1)并非完全可移植。
exit(0)定义干净出口,没有任何错误。 exit(1)定义程序中存在错误,并且程序终止。

翻译自: https://www.includehelp.com/cpp-tutorial/exit-0-vs-exit-1-in-c-cpp-with-examples.aspx

exit c+

exit c+_C / C ++中的exit(0)vs exit(1)与示例相关推荐

  1. python exit 0_python中 os._exit() 和 sys.exit(), exit(0)的用法和区别

    os._exit() 和 sys.exit() os._exit() vs sys.exit() 概述 Python的程序有两中退出方式:os._exit(), sys.exit().本文介绍这两种方 ...

  2. c语言中exit函数_C ++中的exit()函数

    c语言中exit函数 介绍 (Introduction) Today we'll learn about exit() in C++. We know we can break out of loop ...

  3. Shell中的exit 0 和 exit 1是做什么的 ?

    对于初学者来说, 经常会看到类似这样的脚本: 但是exit 0 和 exit 1是做什么的呢 , 我来解释一下: exit 0 代表正常运行程序并退出程序, exit 1 代表非正常运行导致退出程序 ...

  4. exit的用法python_python 中exit,sys.exit,os._exit用法

    exit exit() 可以退出某个程序,余下语句不执行,而其中的数字参数则用来表示程序是否是碰到错误而中断. exit(1) 表示程序错误退出 exit(0) 表示程序正常退出 test.py: # ...

  5. linux中进程退出函数:exit()和_exit()的区别

    linux中进程退出函数:exit()和_exit()的区别 (1)_exit()执行后立即返回给内核,而exit()要先执行一些清除操作,然后将控制权交给内核. (2)调用_exit函数时,其会关闭 ...

  6. python结束循环_python中break、continue 、exit() 、pass终止循环的区别

    python中break.continue .exit() .pass区分 1.break:跳出循环,不再执行 Python break语句,就像在C语言中,打破了最小封闭for或while循环. b ...

  7. python中break、continue 、exit() 、pass终止循环的区别

    python中break.continue .exit() .pass区分 1.break:跳出循环,不再执行 Python break语句,就像在C语言中,打破了最小封闭for或while循环. b ...

  8. exit(0)和exit(1)解释

    exit(0)和exit(1) exit好象在"stdlib.h"头文件里面,所以要有包含头文件   return是返回函数调用,如果返回的是main函数,则为退出程序   exi ...

  9. php exit 和die,PHP中的die()和exit()有什么区别?

    PHP中的die()和exit()函数有什么区别? 我认为两者都有相同的功能,但我怀疑两者有不同之处-它是什么? die()和exit()在其他语言中是不同的,但在php中,只需在.org/users ...

最新文章

  1. boost log 能不能循环覆盖_前端基础进阶(十四):深入核心,详解事件循环机制...
  2. SQL Server 事务日志
  3. 3、Swing布局管理器
  4. axure 如何设置选项联动_Axure教程|用中继器做信息流,高仿人人都是产品经理网...
  5. ubuntu 拷贝文件
  6. python中字符串文件如何打开_python-字符串·文件·集合操作
  7. jvm系列(九):如何优化Java GC
  8. OpenCV stereo matching 代码
  9. 给input设置css样式,input能改变css样式吗
  10. 【第68期】智能时代下的计算机系统能力培养
  11. Python考试题库(含答案)
  12. AMS分析 -- 启动过程
  13. 腾讯微云下载慢解决办法
  14. 搜搜移动业务大厅项目类的结构_2020年SEM小搜投放指南:竞价小渠道如何把效果做到极致...
  15. DDL、DML和DCL的理解(1、总述)
  16. 红米Note9Pro和红米Note9ProMax哪个好-区别是什么
  17. VirtualBox中win7系统无法安装增强功能
  18. 2022年11月PMP难考吗?
  19. 【操作系统安装】Vmware 安装Ubuntu 20.04
  20. 推荐个开源在线文档,助道友领悟 Django 之“道”

热门文章

  1. 嵌入式Linux安装Python环境,linux环境下安装python 3
  2. helm安装postgres_Helm 入门介绍 Kubernetes 上的包管理软件
  3. python tts 保存_Python 文件和目录操作学习
  4. buck电路上下管_推荐 | 学好电路设计与仿真?你不能错过这两本书籍 ~
  5. mysql多线程使用一个链接_探索多线程使用同一个数据库connection的后果
  6. java自定义错误码类_如何编写和应用Java的自定义异常类
  7. map与weakmap,ES6 Map和WeakMap有什么区别?
  8. JDK源码解析之 Java.lang.StringBuffer
  9. 代码实现——MapReduce统计单词出现次数
  10. 算法导论2nd 10.1-7