本文共 4860 字,大约阅读时间需要 16 分钟。
c语言中exit函数
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()
函数。 因此,让我们仔细看看该功能及其工作方式。
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()函数时必须将其包括在内。
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表示终止成功,而其他任何数字则表示错误。
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:
仔细看下面的例子:
#includeusing 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 : "<
Output:
输出 :
Enter a non-zero value: 0ERROR!
exit()
function and terminates the program. 由于提供的上述代码的用户输入为zero(0) ,因此对if-else语句执行else部分。 在编译器遇到exit()
函数并终止程序的地方。 Now let us look at another example where we try to determine whether a number is prime or not.
现在让我们看另一个示例,我们尝试确定数字是否为质数。
The program below illustrates the use of exit()
function.
下面的程序说明了exit()
函数的用法。
#includeusing 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:
输出 :
Further for the above code,
对于上述代码,
num
is prime or not. 首先,我们接受用户输入该号码。 我们需要检查此数字num
是否为质数 。 exit()
function, 在for循环中,我们检查该数字是否在该时刻被循环迭代器整除。 如果是这样,我们可以直接得出数字不是质数的结论,并使用exit()
函数终止程序, 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.
如有任何疑问,请在下面评论。
翻译自:
c语言中exit函数
转载地址:http://cgozd.baihongyu.com/