c语言中exit函数

介绍 (Introduction)

Today we’ll learn about exit() in C++. We know we can break out of loops using the built-in break function in C++. Similarly, we can also break out of a whole C++ program using the exit() function.

今天,我们将学习C ++中的exit()。 我们知道可以使用C ++中的内置break函数来中断循环。 同样,我们也可以使用exit()函数创建一个完整的C ++程序。

Think of a situation where you are to come to a conclusion in your program. You get the result and conclude something before the whole program has been compiled or evaluated.

想一想在程序中得出结论的情况。 您可以得到结果并在编译或评估整个程序之前得出结论。

How do you terminate the program as soon as you get your required information or result?

获得所需信息或结果后,如何终止程序?

The answer to the above question is using the built-in exit() function in C++. So let us take a closer look at the function and how it works.

上述问题的答案是使用C ++中的内置exit()函数。 因此,让我们仔细看看该功能及其工作方式。

C ++中exit()函数的定义 (Definition of the exit() function in C++ )

Flow Chart Representation Of exit() Function
exit()函数的流程图表示

Theoretically, the exit() function in C++ causes the respective program to terminate as soon as the function is encountered, no matter where it appears in the program listing. The function has been defined under the stdlib.h header file, which must be included while using the exit() function.

从理论上讲,C ++中的exit()函数会导致相应的程序在遇到该函数时立即终止 ,无论它出现在程序列表中的什么位置。 该函数已在stdlib.h头文件下定义,在使用exit()函数时必须将其包括在内。

C ++中exit()函数的语法 (Syntax for the exit() function in C++ )

The syntax for using the exit() function is given below,

下面给出了使用exit()函数的语法,


exit( exit_value );

Here, exit_value is the value passed to the Operating system after the successful termination of the program. This value can be tested in batch files where ERROR LEVEL gives us the return value provided by the exit() function. Generally, the value 0 indicates a successful termination and any other number indicates some error.

此处, exit_value是成功终止程序后传递给操作系统的值。 可以在批处理文件中测试该值,其中ERROR LEVEL为我们提供了exit()函数提供的返回值。 通常,值0表示终止成功,而其他任何数字则表示错误。

C ++中exit()函数的工作 (Working of the exit() function in C++)

Remember, the function exit() never returns any value. It terminates the process and performs the regular cleanup for terminating programs.

请记住,函数exit()从不返回任何值。 它终止进程并执行常规清理以终止程序。

Also, automatic storage objects are not destroyed by calling this function in C++.

同样,通过在C ++中调用此函数也不会破坏自动存储对象。

Look at the example below carefully:

仔细看下面的例子:


#include<iostream>
using namespace std;
int main()
{int i;cout<<"Enter a non-zero value: ";  //user inputcin>>i;if(i)    // checks whether the user input is non-zero or not{cout<<"Valid input.\n";}else{cout<<"ERROR!";  //the program exists if the value is 0exit(0);}cout<<"The input was : "<<i;
}

Output:

输出


Enter a non-zero value: 0
ERROR!
  • Since the user input for the above code provided is zero(0), the else part is executed for the if-else statement. Further where the compiler encounters the exit() function and terminates the program.由于提供的上述代码的用户输入为zero(0) ,因此对if-else语句执行else部分。 在编译器遇到exit()函数并终止程序的地方。
  • Even the print statement below the if-else is not executed since the program has been terminated by the exit() function already.因为该程序已经被exit()函数终止,所以即使if-else下面的print语句也不会执行。

Now let us look at another example where we try to determine whether a number is prime or not.

现在让我们看另一个示例,我们尝试确定数字是否为质数。

在C ++中使用exit()函数 (Using the exit() function in C++)

The program below illustrates the use of exit() function.

下面的程序说明了exit()函数的用法。


#include<iostream>
using namespace std;
int main()
{int i,num;cout<<"Enter the number : ";cin>>num;for(i=2;i<=num/2;i++){if(num%i==0){cout<<"\nNot a prime number!";exit(0);}}cout<<"\nIt is a prime number!";return 0;
}

Output:

输出

exit() Example Output
exit()示例输出

Further for the above code,

对于上述代码,

  • firstly we took a user input for the number. We need to check whether this number,num is prime or not.首先,我们接受用户输入该号码。 我们需要检查此数字num是否为质数
  • After that, we apply a for loop which works from 2 to n/2. This is because we already know that all numbers are divisible by 1 as well as a number is indivisible by numbers above its half.之后,我们应用一个for循环,其作用范围是2n / 2 。 这是因为我们已经知道所有数字都可以被1整除,并且一个数字也可以被其一半以上的数字整除。
  • Inside the for loop, we check whether the number is divisible by the loop iterator at that instant. If so, we can directly conclude that the number is not prime and terminate the program using the exit() function,在for循环中,我们检查该数字是否在该时刻被循环迭代器整除。 如果是这样,我们可以直接得出数字不是质数的结论,并使用exit()函数终止程序,
  • Else, the loop goes on checking. After the execution of the whole loop structure, we declare the number as a prime one.否则,循环继续进行检查。 执行完整个循环结构后,我们将数字声明为素数

结论 (Conclusion)

In this tutorial, we discussed the working as well as the usage of the built-in exit() function in C++. It is widely used to terminate the execution of a program.

在本教程中,我们讨论了C ++中内置exit()函数的工作方式和用法。 它广泛用于终止程序的执行。

For any questions comment below.

如有任何疑问,请在下面评论。

参考资料 (References)

  • https://stackoverflow.com/questions/461449/return-statement-vs-exit-in-mainhttps://stackoverflow.com/questions/461449/return-statement-vs-exit-in-main
  • https://www.journaldev.com/c-plus-plushttps://www.journaldev.com/c-plus-plus

翻译自: https://www.journaldev.com/36611/exit-function-c-plus-plus

c语言中exit函数

c语言中exit函数_C ++中的exit()函数相关推荐

  1. c语言中math的库函数,C语言中math.h库中的常用函数

    C语言中math.h库中的常用函数 int abs(int i) 返回整型参数i的绝对值 double cabs(struct complex znum) 返回复数znum的绝对值 double fa ...

  2. scala 函数中嵌套函数_Scala中的嵌套函数 用法和示例

    scala 函数中嵌套函数 Scala中的嵌套函数 (Nested functions in Scala) A nested function is defined as a function whi ...

  3. oracle中转换函数,Oracle中的转换函数

    Oracle中的转换函数有三个,分别为to_char(),to_date(),to_number() 1.to_char()的用法 格式化当前的日期时间 select sysdate,to_char( ...

  4. c语言中swap函数_C ++中的swap()函数

    c语言中swap函数 介绍 (Introduction) In this tutorial, we are going to learn the swap() function in C++ prog ...

  5. c语言中struct和c++中class实例对比

    前言 实现游戏中简单的打怪升级的功能 c语言中的struct #include <stdio.h>typedef void(*Train)(struct player*, int); ty ...

  6. c语言中fflush_在C中使用fflush()

    c语言中fflush In this article, we'll take a look at how we can use the fflush() function in C. 在本文中,我们将 ...

  7. C语言中sizeof测量形参中数组的长度

    在C语言中,若我们在主函数中定义了一个数组,并给数组赋予了初值,在之后,若有函数需要调用,且需要用到该数组的长度,该怎么来操作. 在下面的代码中定义了一个数组,在main函数中用sizeof函数来计算 ...

  8. python语言中mod_mod在python中怎么用

    MOD是取模运算符. 语法 MOD ( a, b) 通常情况下取模运算(mod)和求余(rem)运算被混为一谈,因为在大多数的编程语言里,都用'%'符号表示取模或者求余运算.在这里要提醒大家要十分注意 ...

  9. c语言中getenv的作用,C语言putenv()函数和getenv()函数的使用详解

    C语言putenv()函数和getenv()函数的使用详解 C语言putenv()函数:改变或增加环境变量头文件: #include4 定义函数: int putenv(const char * st ...

最新文章

  1. 如何访问固定的内存位置?
  2. Levenshtein distance最小编辑距离算法实现
  3. 如何用SAP编制现金流量表
  4. 2apt-get命令,deb包安装,源码安装
  5. (基础)HTML文档结构知识点讲解
  6. java 堆内存分析_JVM内存堆布局图解分析
  7. 第二十节:一个缺失已久的特性 — module模块
  8. 对Python装饰器的个人理解方法
  9. 基于VMware vSphere的虚拟化平台,内存分配是如何实现的?
  10. firefox 插件可能用得上的Firefox插件及下载
  11. Linux操作系统实践
  12. 如何在SSD和HDD VPS主机之间进行选择
  13. 【RedisTemplate】Set数据类型的常用操作
  14. 【云原生-K8s】k8s常用命令大全-持续更新【kubectl】
  15. ESPIDF开发ESP32学习笔记【SPI与片外FLASH基础】
  16. 糖果将推翻译手机php切,全球首款翻译手机糖果S20发布 或将终结翻译机
  17. Java 支付宝手机网站支付下单 支付回调 及订单查询实现
  18. 油猴Tampermonkey及其脚本的安装
  19. matlab floor函数_MATLAB图像处理:08:在交通视频中检测汽车
  20. 软开关移相全桥PS-FB DCDC变换器的各模态分析与计算

热门文章

  1. 国产无线蓝牙耳机哪个好?2023国产无线蓝牙耳机排行
  2. 短视频上热门必须要学会这100个爆款标题模版
  3. java replace和replaceAll的区别以及用法
  4. oracle查找某一天的数据,oracle统计时间段内每一天的数据(推荐)
  5. linux rm和rmdir区别,Linux命令rmdir和rm的区别
  6. article-并联机械手爪运动学分析
  7. java ear包_简单介绍Java 的JAR包、EAR包、WAR包区别
  8. scikit learn、tensorflow、keras区别
  9. mysql数据库黑马
  10. 用Java做一个跳一跳辅助