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

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
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 input
	cin>>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 0
		exit(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() 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://www.journaldev.com/36611/exit-function-c-plus-plus

c语言中exit函数

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值