Exit() function analysis

Introduction to exit() function

Function name: The
header file where exit() is located: stdlib.h (If it is C++, the header file: #include <cstdlib >)
Function: Close all files and terminate the process being executed .
Exit(0) means normal exit,
exit(x) (x is not 0) means abnormal exit . This x is returned to the operating system (including UNIX, Linux, and MS DOS) for use by other programs.
stdlib.h: void exit(int status);//Parameter status, the return value of program exit

The difference between exit() and return

If main() is in a recursive program, exit() will still terminate the program;
but return transfers control to the previous level of the recursion, until the first level, then return will not terminate the program.
Even if exit() is called in a function other than main(), it will terminate the program.
return(); is the end of a function and returns the result.

  1. return returns the value of the function, which is a keyword; exit is a function.

  2. Return is the language level, which represents the return of the call stack; and exit is the system call level, which represents the end of a process.

  3. return is the exit (return) of the function; exit is the exit of the process.

  4. Return is provided by the C language, and exit is provided by the operating system (or given in the function library).

  5. Return is used to end the execution of a function and transfer the execution information of the function to other calling functions; the exit function is to exit the application, delete the memory space used by the process, and return a status of the application to the OS. This status is identified This information is related to the machine and the operating system. Generally, 0 means normal exit, and non-zero means abnormal exit.

  6. The effect of calling return and exit in a non-main function is obvious, but the phenomenon of calling return and exit in the main function is very vague, and the phenomenon is the same in most cases.

Test 1

#include<stdlib.h>
#include<stdio.h>
#include <string.h>
int main(int argc,char*argv[])
{
        int status;
        printf("Enter either 1 or 2\n");
        status=getchar();
        exit(status-'0');
        printf("this line is never reached\n");
        return 0;
}

Insert picture description here
When the exit() function is called to exit, printf("this line is never reached\n"); and return 0; and the two sentences will not be executed.

Test 2

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int output( )
{
   int a[10];
   ofstream outfile("f1.dat",ios::out);//定义文件流对象,打开磁盘文件"f1.dat"
   if(!outfile)                        //如果打开失败,outfile返回值
   {
      cerr<<"open error!"<<endl;
      exit(1);
   }
   cout<<"enter 10 integer numbers:"<<endl;
   for(int i=0;i<10;i++)
   {
      cin>>a[i];
      outfile<<a[i]<<" ";
   }            //向磁盘文件"f1.dat"输出数据
   outfile.close();                   //关闭磁盘文件"f1.dat"
   return 0;
}

int main(){
        output();
        cout<<"end!"<<endl;
        return 0;
}

Insert picture description here
Did not enter the exit(1) branch, execute end normally!
Modify if(!outfile) to if(outfile) to let the program enter the exit(1) branch

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int output( )
{
   int a[10];
   ofstream outfile("f1.dat",ios::out);//定义文件流对象,打开磁盘文件"f1.dat"
   if(outfile)                        //如果打开失败,outfile返回值
   {
      cerr<<"open error!"<<endl;
      exit(1);
   }
   cout<<"enter 10 integer numbers:"<<endl;
   for(int i=0;i<10;i++)
   {
      cin>>a[i];
      outfile<<a[i]<<" ";
   }            //向磁盘文件"f1.dat"输出数据
   outfile.close();                   //关闭磁盘文件"f1.dat"
   return 0;
}


int main(){
        output();
        cout<<"end!"<<endl;
        return 0;
}

Insert picture description here
After the exit(1) function is executed, the entire program ends directly, the code after exit(1) will not be executed, and the output() sub-function will not be executed and returned. The statement after the main function is cout<<"end!"<< endl; will not be executed either. It is not the same as return.

Guess you like

Origin blog.csdn.net/u014470361/article/details/102225994